root/trunk/patForms/Rule/ConditionalRequired.php

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  * patForms Rule ConditionalRequired
4  *
5  * $Id$
6  *
7  * @package        patForms
8  * @subpackage    Rules
9  */
10
11 /**
12  * patForms Rule ConditionalRequired
13  *
14  * This rule can be used to set the status of
15  * some elements to required depending on the value
16  * of another element.
17  *
18  * It has to be applied prior to validating the form.
19  *
20  * @package        patForms
21  * @subpackage    Rules
22  * @author        Stephan Schmidt <schst@php-tools.net>
23  * @license        LGPL, see license.txt for details
24  * @link        http://www.php-tools.net
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 ?>
Note: See TracBrowser for help on using the browser.