| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
class patForms_Rule_ValidValues extends patForms_Rule |
|---|
| 30 |
{ |
|---|
| 31 |
|
|---|
| 32 |
* script that will be displayed only once |
|---|
| 33 |
* |
|---|
| 34 |
* @access private |
|---|
| 35 |
* @var array |
|---|
| 36 |
*/ |
|---|
| 37 |
var $globalScript = array( |
|---|
| 38 |
'html' => 'Html/Rule/ValidValue.js' |
|---|
| 39 |
); |
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
* javascript that will be displayed once per instance |
|---|
| 43 |
* |
|---|
| 44 |
* @access private |
|---|
| 45 |
* @var array |
|---|
| 46 |
*/ |
|---|
| 47 |
var $instanceScript = array( |
|---|
| 48 |
'html' => "var pfr_[RULE::ID] = new pFRC_ValidValue('[CONTAINER::NAME]');\n" |
|---|
| 49 |
); |
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
* properties that have to be replaced in the instance script. |
|---|
| 53 |
* |
|---|
| 54 |
* @access private |
|---|
| 55 |
* @var array |
|---|
| 56 |
*/ |
|---|
| 57 |
var $scriptPlaceholders = array( |
|---|
| 58 |
'RULE::SOURCE' => '_source', |
|---|
| 59 |
); |
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
* name of the rule |
|---|
| 63 |
* |
|---|
| 64 |
* @abstract |
|---|
| 65 |
* @access private |
|---|
| 66 |
*/ |
|---|
| 67 |
var $ruleName = 'ValidValue'; |
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
* define error codes and messages for the rule |
|---|
| 71 |
* |
|---|
| 72 |
* @access private |
|---|
| 73 |
* @var array $validatorErrorCodes |
|---|
| 74 |
* @todo translate error messages |
|---|
| 75 |
*/ |
|---|
| 76 |
var $validatorErrorCodes = array( |
|---|
| 77 |
"C" => array( |
|---|
| 78 |
1 => "Please enter one of the following values: [VALUES].", |
|---|
| 79 |
), |
|---|
| 80 |
"de" => array( |
|---|
| 81 |
1 => "Bitte geben Sie einen der folgenden Werte ein: [VALUES].", |
|---|
| 82 |
), |
|---|
| 83 |
"fr" => array( |
|---|
| 84 |
1 => "Veuillez n'entrer que l'une des valeurs suivantes: [VALUES].", |
|---|
| 85 |
) |
|---|
| 86 |
); |
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
* possible values |
|---|
| 90 |
* @access private |
|---|
| 91 |
* @var array |
|---|
| 92 |
*/ |
|---|
| 93 |
var $_values; |
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
* field id that is used |
|---|
| 97 |
* @access private |
|---|
| 98 |
* @var string |
|---|
| 99 |
*/ |
|---|
| 100 |
var $_field; |
|---|
| 101 |
|
|---|
| 102 |
public function __construct($params) |
|---|
| 103 |
{ |
|---|
| 104 |
parent::__construct(); |
|---|
| 105 |
|
|---|
| 106 |
extract($params); |
|---|
| 107 |
$this->_values = explode('|', $value); |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
* prepare the rule |
|---|
| 112 |
* |
|---|
| 113 |
* @access public |
|---|
| 114 |
* @param object patForms |
|---|
| 115 |
*/ |
|---|
| 116 |
function prepareRule(&$container) |
|---|
| 117 |
{ |
|---|
| 118 |
patForms_Rule::prepareRule($container); |
|---|
| 119 |
|
|---|
| 120 |
$onChange = $container->getAttribute('onchange'); |
|---|
| 121 |
$newHandler = sprintf('pfr_%s.validate();', $this->_id); |
|---|
| 122 |
$container->setAttribute('onchange', $newHandler . $onChange); |
|---|
| 123 |
|
|---|
| 124 |
return true; |
|---|
| 125 |
} |
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
* method called by patForms or any patForms_Element to validate the |
|---|
| 129 |
* element or the form. |
|---|
| 130 |
* |
|---|
| 131 |
* @access public |
|---|
| 132 |
* @param object patForms form object |
|---|
| 133 |
*/ |
|---|
| 134 |
function applyRule(&$element, $type = PATFORMS_RULE_AFTER_VALIDATION) |
|---|
| 135 |
{ |
|---|
| 136 |
if(in_array($element->getValue(), $this->_values)) { |
|---|
| 137 |
return true; |
|---|
| 138 |
} |
|---|
| 139 |
|
|---|
| 140 |
$this->addValidationError(1, array('values' => implode(', ', $this->_values))); |
|---|
| 141 |
return false; |
|---|
| 142 |
} |
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 |
* Registers scripts form this rule to the form |
|---|
| 146 |
* |
|---|
| 147 |
* @access public |
|---|
| 148 |
* @param object patForms form object |
|---|
| 149 |
*/ |
|---|
| 150 |
function registerJavascripts(&$form) |
|---|
| 151 |
{ |
|---|
| 152 |
parent::registerJavascripts($form); |
|---|
| 153 |
|
|---|
| 154 |
foreach ($this->_values as $value) { |
|---|
| 155 |
$values[] = "'$value'"; |
|---|
| 156 |
} |
|---|
| 157 |
$script = sprintf("pfr_%s.setValues(new Array(%s));\n", $this->_id, implode(', ', $values)); |
|---|
| 158 |
$form->registerInstanceJavascript($script); |
|---|
| 159 |
} |
|---|
| 160 |
} |
|---|
| 161 |
?> |
|---|