root/trunk/patForms/Rule/ValidValues.php

Revision 339, 3.3 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 ValidValues
4  *
5  * A simple rule, that checks for a required minimum length of a field.
6  * Required for integration with Propel but probably useful otherwise.
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 ValidValues
19  *
20  * A simple rule, that checks for a required minimum length of a field.
21  * Required for integration with Propel but probably useful otherwise.
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_ValidValues 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/ValidValue.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_ValidValue('[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 = 'ValidValue';
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 one of the following values: [VALUES].",
79         ),
80         "de" =>    array(
81             1    =>    "Bitte geben Sie einen der folgenden Werte ein: [VALUES].",
82         ),
83         "fr" =>    array(
84             1    =>    "Veuillez n'entrer que l'une des valeurs suivantes: [VALUES].",
85         )
86     );
87
88     /**
89     * possible values
90     * @access    private
91     * @var        array
92     */
93     var $_values;
94
95     /**
96     * field id that is used
97     * @access    private
98     * @var        string
99     */
100     var $_field;
101
102     public function __construct($params)
103     {
104         parent::__construct();
105
106         extract($params);
107         $this->_values = explode('|', $value);
108     }
109
110     /**
111     * prepare the rule
112     *
113     * @access    public
114     * @param    object patForms
115     */
116     function prepareRule(&$container)
117     {
118         patForms_Rule::prepareRule($container);
119
120         $onChange = $container->getAttribute('onchange');
121         $newHandler = sprintf('pfr_%s.validate();', $this->_id);
122         $container->setAttribute('onchange', $newHandler . $onChange);
123
124         return true;
125     }
126
127     /**
128     * method called by patForms or any patForms_Element to validate the
129     * element or the form.
130     *
131     * @access    public
132     * @param    object patForms    form object
133     */
134     function applyRule(&$element, $type = PATFORMS_RULE_AFTER_VALIDATION)
135     {
136         if(in_array($element->getValue(), $this->_values)) {
137             return    true;
138         }
139
140         $this->addValidationError(1, array('values' => implode(', ', $this->_values)));
141         return false;
142     }
143
144     /**
145     * Registers scripts form this rule to the form
146     *
147     * @access    public
148     * @param    object patForms    form object
149     */
150     function registerJavascripts(&$form)
151     {
152         parent::registerJavascripts($form);
153
154         foreach ($this->_values as $value) {
155             $values[] = "'$value'";
156         }
157         $script = sprintf("pfr_%s.setValues(new Array(%s));\n", $this->_id, implode(', ', $values));
158         $form->registerInstanceJavascript($script);
159     }
160 }
161 ?>
Note: See TracBrowser for help on using the browser.