root/trunk/patForms/Rule/MaxLength.php

Revision 342, 3.1 kB (checked in by sfuchs, 3 years ago)

fixed patForms_Creator_Definition reference problem with adding rules (seems to result from php5.1 reference behaviour)
fixed typos in patForms_Rule_MaxLength
fixed patForms_Storage_Propel to write results back to the form after saving (especially useful for retrieving primary key values for added entries)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2 /**
3  * patForms Rule MaxLength
4  *
5  * This is just a simple rule, that checks for a required minimum length of
6  * a field
7  *
8  * $Id$
9  *
10  * @author        Sven Fuchs <svenfuchs@artweb-design.de>
11  * @package        patForms
12  * @subpackage    Rules
13  * @license        LGPL
14  * @copyright    PHP Application Tools <http://www.php-tools.net>
15  */
16
17 /**
18  * patForms Rule MaxLength
19  *
20  * This is just a simple rule, that checks for a required minimum length of
21  * a field
22  *
23  * @author        Sven Fuchs <svenfuchs@artweb-design.de>
24  * @package        patForms
25  * @subpackage    Rules
26  * @license        LGPL
27  * @copyright    PHP Application Tools <http://www.php-tools.net>
28  */
29 class patForms_Rule_MaxLength 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/MaxLength.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_MaxLength('[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 = 'MaxLength';
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 a value that is max. [VALUE] characters long.",
79         ),
80         "de" =>    array(
81             1    =>    "Bitte geben Sie einen max. [VALUE] Zeichen langen Wert ein.",
82         ),
83         "fr" =>    array(
84             1    =>    "Veuillez entrer une valeur n'excĂ©dant pas [VALUE] caracères.",
85         )
86     );
87
88     /**
89     * the value to compare with
90     * @access    private
91     * @var        array
92     */
93     var $_value;
94
95     /**
96     * field id that is used
97     * @access    private
98     * @var        string
99     */
100     var $_field;
101
102     /**
103     * set the value to compare with
104     *
105     * @access    public
106     * @param    object patForms
107     */
108
109     function setValue($value)
110     {
111         $this->_value = $value;
112     }
113
114     /**
115     * prepare the rule
116     *
117     * @access    public
118     * @param    object patForms
119     */
120     function prepareRule(&$container)
121     {
122         patForms_Rule::prepareRule($container);
123
124         $onChange = $container->getAttribute('onchange');
125         $newHandler = sprintf('pfr_%s.validate();', $this->_id);
126         $container->setAttribute('onchange', $newHandler . $onChange);
127
128         return true;
129     }
130
131     /**
132     * method called by patForms or any patForms_Element to validate the
133     * element or the form.
134     *
135     * @access    public
136     * @param    object patForms    form object
137     */
138     function applyRule(&$element, $type = PATFORMS_RULE_AFTER_VALIDATION)
139     {
140         if(strlen($element->getValue()) <= $this->_value) {
141             return    true;
142         }
143
144         $this->addValidationError(1, array('value' => $this->_value));
145         return false;
146     }
147
148     /**
149     * Registers scripts form this rule to the form
150     *
151     * @access    public
152     * @param    object patForms    form object
153     */
154     function registerJavascripts(&$form)
155     {
156         parent::registerJavascripts($form);
157
158         $script = sprintf("pfr_%s.setValue(%s);\n", $this->_id, $this->_value);
159         $form->registerInstanceJavascript($script);
160     }
161 }
162 ?>
Note: See TracBrowser for help on using the browser.