root/trunk/patForms/Rule/MinValue.php

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

some fixes to propel and definition related stuff:

- changed Creator_Definition to use patForms::createDatasource() and patForms::createRule() instead of instantiating these directly
- changed Datasource_Propel to now use a new method init() for initialization instead of construct()
- changed Definition_Propel to

  • (temporarily) use PATFORMS_INCLUDE_PATH for inclusion of base class
  • define validator types as 'MinLength?' etc. instead of using the complete patForms classname here
  • better utilize Propel api for foreign key lookup etc.
  • "lazy" construct() protocol for usage with a classname string only (instead of an assoc array)

- changed several Rule classes to

  • not include their base classes any more (will be done by patForms::createRule())
  • now use a method setValue() for initialization instead of construct()
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2 /**
3  * patForms Rule MinValue
4  *
5  * This rule simply checks for a required minimum value (number) of a field
6  *
7  * $Id$
8  *
9  * @author        Sven Fuchs <svenfuchs@artweb-design.de>
10  * @package        patForms
11  * @subpackage    Rules
12  * @license        LGPL
13  * @copyright    PHP Application Tools <http://www.php-tools.net>
14  */
15
16 /**
17  * patForms Rule MinValue
18  *
19  * This rule simply checks for a required minimum value (number) of a field
20  *
21  * @author        Sven Fuchs <svenfuchs@artweb-design.de>
22  * @package        patForms
23  * @subpackage    Rules
24  * @license        LGPL
25  * @copyright    PHP Application Tools <http://www.php-tools.net>
26  */
27 class patForms_Rule_MinValue extends patForms_Rule
28 {
29     /**
30     * script that will be displayed only once
31     *
32     * @access    private
33     * @var        array
34     */
35
36     var $globalScript = array(
37         'html'    =>    'Html/Rule/MinValue.js'
38     );
39
40     /**
41     * javascript that will be displayed once per instance
42     *
43     * @access    private
44     * @var        array
45     */
46     var $instanceScript    = array(
47         'html'    =>    "var pfr_[RULE::ID] = new pFRC_MinValue('[CONTAINER::NAME]');\n"
48     );
49
50     /**
51     * properties that have to be replaced in the instance script.
52     *
53     * @access    private
54     * @var        array
55     */
56     var $scriptPlaceholders    = array(
57         'RULE::SOURCE'    =>    '_source',
58     );
59
60     /**
61     * name of the rule
62     *
63     * @abstract
64     * @access    private
65     */
66     var    $ruleName = 'MinValue';
67
68     /**
69     * define error codes and messages for the rule
70     *
71     * @access    private
72     * @var        array    $validatorErrorCodes
73     * @todo        translate error messages
74     */
75     var    $validatorErrorCodes = array(
76         "C"    =>    array(
77             1    =>    "Please enter a number that is greater or equal to [VALUE].",
78         ),
79         "de" =>    array(
80             1    =>    "Bitte geben Sie eine Zahl größer oder gleich [VALUE] ein.",
81         ),
82         "fr" =>    array(
83             1    =>    "Veuillez entrer une valeur d'au moins [VALUE].",
84         )
85     );
86
87     /**
88     * the value to compare with
89     * @access    private
90     * @var        string
91     */
92     var $_value;
93
94     /**
95     * field id that is used
96     * @access    private
97     * @var        string
98     */
99     var $_field;
100
101     /**
102     * set the value to compare with
103     *
104     * @access    public
105     * @param    object patForms
106     */
107
108     function setValue($value)
109     {
110         $this->_value = $value;
111     }
112
113     /**
114     * prepare the rule
115     *
116     * @access    public
117     * @param    object patForms
118     */
119     function prepareRule(&$container)
120     {
121         patForms_Rule::prepareRule($container);
122
123         $onChange = $container->getAttribute('onchange');
124         $newHandler = sprintf('pfr_%s.validate();', $this->_id);
125         $container->setAttribute('onchange', $newHandler . $onChange);
126
127         return true;
128     }
129
130     /**
131     * method called by patForms or any patForms_Element to validate the
132     * element or the form.
133     *
134     * @access    public
135     * @param    object patForms    form object
136     */
137     function applyRule(&$element, $type = PATFORMS_RULE_AFTER_VALIDATION)
138     {
139         if (intval($element->getValue()) >= intval($this->_value)){
140             return    true;
141         }
142
143         $this->addValidationError(1, array('value' => $this->_value));
144         return false;
145     }
146
147     /**
148     * Registers scripts form this rule to the form
149     *
150     * @access    public
151     * @param    object patForms    form object
152     */
153     function registerJavascripts(&$form)
154     {
155         parent::registerJavascripts($form);
156
157         $script = sprintf("pfr_%s.setMinValue(%s);\n", $this->_id, $this->_value);
158         $form->registerInstanceJavascript($script);
159     }
160 }
161 ?>
Note: See TracBrowser for help on using the browser.