root/trunk/patForms/Rule/AnyRequired.php

Revision 234, 2.2 kB (checked in by schst, 3 years ago)

added new rule: AnyRequired? allows you to specify a list of fields where the user needs to enter data in at least one of them

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2 /**
3  * patForms Rule AnyRequired
4  *
5  * $Id$
6  *
7  * @package        patForms
8  * @subpackage    Rules
9  */
10
11 /**
12  * patForms Rule AnyRequired
13  *
14  * This rule allows you to set a list of elements which
15  * will be checked, if any of them has been set.
16  *
17  * It has to be applied after validating the form.
18  *
19  * @package        patForms
20  * @subpackage    Rules
21  * @author        Stephan Schmidt <schst@php-tools.net>
22  * @license        LGPL, see license.txt for details
23  * @link        http://www.php-tools.net
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 ?>
Note: See TracBrowser for help on using the browser.