|
Revision 167, 1.8 kB
(checked in by schst, 4 years ago)
|
fixed double spacing
|
- Property svn:eol-style set to
native
- Property svn:keywords set to
Author Date Id Revision
|
| Line | |
|---|
| 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 |
class patForms_Rule_ConditionalRequired extends patForms_Rule |
|---|
| 27 |
{ |
|---|
| 28 |
|
|---|
| 29 |
* fields that will be required |
|---|
| 30 |
* @access private |
|---|
| 31 |
* @var array |
|---|
| 32 |
*/ |
|---|
| 33 |
var $_requiredFields = array(); |
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
* conditions |
|---|
| 37 |
* @access private |
|---|
| 38 |
* @var array |
|---|
| 39 |
*/ |
|---|
| 40 |
var $_conditions = array(); |
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
* set the names of the fields that will be required |
|---|
| 44 |
* |
|---|
| 45 |
* @access public |
|---|
| 46 |
* @param array required fields |
|---|
| 47 |
*/ |
|---|
| 48 |
function setRequiredFields( $fields ) |
|---|
| 49 |
{ |
|---|
| 50 |
$this->_requiredFields = $fields; |
|---|
| 51 |
} |
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
* add a condition |
|---|
| 55 |
* |
|---|
| 56 |
* @access public |
|---|
| 57 |
* @param string condition field name |
|---|
| 58 |
* @param mixed condition value |
|---|
| 59 |
*/ |
|---|
| 60 |
function addCondition( $field, $value ) |
|---|
| 61 |
{ |
|---|
| 62 |
$this->_conditions[$field] = $value; |
|---|
| 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_BEFORE_VALIDATION ) |
|---|
| 73 |
{ |
|---|
| 74 |
$required = 'yes'; |
|---|
| 75 |
foreach( $this->_conditions as $field => $value ) |
|---|
| 76 |
{ |
|---|
| 77 |
$el = &$form->getElement( $field ); |
|---|
| 78 |
$val = $el->getValue(); |
|---|
| 79 |
if( $val != $value ) |
|---|
| 80 |
{ |
|---|
| 81 |
$required = 'no'; |
|---|
| 82 |
break; |
|---|
| 83 |
} |
|---|
| 84 |
} |
|---|
| 85 |
|
|---|
| 86 |
foreach( $this->_requiredFields as $field ) |
|---|
| 87 |
{ |
|---|
| 88 |
$el = &$form->getElement( $field ); |
|---|
| 89 |
$el->setAttribute( 'required', $required ); |
|---|
| 90 |
} |
|---|
| 91 |
|
|---|
| 92 |
return true; |
|---|
| 93 |
} |
|---|
| 94 |
} |
|---|
| 95 |
?> |
|---|