| 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 |
class patForms_Rule_AnyRequired extends patForms_Rule |
|---|
| 26 |
{ |
|---|
| 27 |
|
|---|
| 28 |
* define error codes and messages for the rule |
|---|
| 29 |
* |
|---|
| 30 |
* @access private |
|---|
| 31 |
* @var array $validatorErrorCodes |
|---|
| 32 |
* @todo translate error message to french |
|---|
| 33 |
*/ |
|---|
| 34 |
var $validatorErrorCodes = array( |
|---|
| 35 |
'C' => array( |
|---|
| 36 |
1 => 'Please fill out at least one of these fields: [FIELD_LABELS]', |
|---|
| 37 |
), |
|---|
| 38 |
'de' => array( |
|---|
| 39 |
1 => 'Bitte füllen Sie zumindest eines dieser Felder aus: [FIELD_LABELS]', |
|---|
| 40 |
), |
|---|
| 41 |
'fr' => array( |
|---|
| 42 |
1 => 'Veuillez remplir au moins un des champs suivants: [FIELD_LABELS]', |
|---|
| 43 |
) |
|---|
| 44 |
); |
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
* list of fields |
|---|
| 48 |
* |
|---|
| 49 |
* @access private |
|---|
| 50 |
* @var array |
|---|
| 51 |
*/ |
|---|
| 52 |
var $_fieldNames = array(); |
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
* set the names of the fields that will be required |
|---|
| 56 |
* |
|---|
| 57 |
* @access public |
|---|
| 58 |
* @param array required fields |
|---|
| 59 |
*/ |
|---|
| 60 |
function setFieldNames($fields) |
|---|
| 61 |
{ |
|---|
| 62 |
$this->_fieldNames = $fields; |
|---|
| 63 |
} |
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
* method called by patForms or any patForms_Element to validate the |
|---|
| 67 |
* element or the form. |
|---|
| 68 |
* |
|---|
| 69 |
* @access public |
|---|
| 70 |
* @param object patForms form object |
|---|
| 71 |
*/ |
|---|
| 72 |
function applyRule(&$form, $type = PATFORMS_RULE_AFTER_VALIDATION) |
|---|
| 73 |
{ |
|---|
| 74 |
$labels = array(); |
|---|
| 75 |
foreach ($this->_fieldNames as $fieldName) { |
|---|
| 76 |
$el = &$form->getElementByName($fieldName); |
|---|
| 77 |
if (patErrorManager::isError($el)) { |
|---|
| 78 |
continue; |
|---|
| 79 |
} |
|---|
| 80 |
if ((string)$el->getValue() === '') { |
|---|
| 81 |
array_push($labels, $el->getAttribute('label')); |
|---|
| 82 |
continue; |
|---|
| 83 |
} |
|---|
| 84 |
return true; |
|---|
| 85 |
} |
|---|
| 86 |
|
|---|
| 87 |
$vars = array( |
|---|
| 88 |
'field_labels' => implode(', ', $labels) |
|---|
| 89 |
); |
|---|
| 90 |
$this->addValidationError(1, $vars); |
|---|
| 91 |
return false; |
|---|
| 92 |
} |
|---|
| 93 |
} |
|---|
| 94 |
?> |
|---|