root/trunk/patForms/Rule/Match.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 Match
4  *
5  * This rule matches the field value against a regEx pattern. It successfully
6  * validates the passed value if the value *does* match the pattern.
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 Match
19  *
20  * This rule matches the field value against a regEx pattern. It successfully
21  * validates the passed value if the value *does* match the pattern.
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_Match extends patForms_Rule
30 {
31     /**
32     * script that will be displayed only once
33     *
34     * @access    private
35     * @var        array
36     */
37
38     var $globalScript = array(
39         'html'    =>    'Html/Rule/Match.js'
40     );
41
42     /**
43     * javascript that will be displayed once per instance
44     *
45     * @access    private
46     * @var        array
47     */
48     var $instanceScript    = array(
49         'html'    =>    "var pfr_[RULE::ID] = new pFRC_Match('[CONTAINER::NAME]');\n"
50     );
51
52     /**
53     * properties that have to be replaced in the instance script.
54     *
55     * @access    private
56     * @var        array
57     */
58     var $scriptPlaceholders    = array(
59         'RULE::SOURCE'    =>    '_source',
60     );
61
62     /**
63     * name of the rule
64     *
65     * @abstract
66     * @access    private
67     */
68     var    $ruleName = 'Match';
69
70     /**
71     * define error codes and messages for the rule
72     *
73     * @access    private
74     * @var        array    $validatorErrorCodes
75     * @todo        translate error messages
76     */
77     var    $validatorErrorCodes = array(
78         "C"    =>    array(
79             1    =>    "This is an invalid value.",
80         ),
81         "de" =>    array(
82             1    =>    "Dies ist ein ungültiger Wert.",
83         ),
84         "fr" =>    array(
85             1    =>    "Valeur incorrecte.",
86         )
87     );
88
89     /**
90     * the value to compare with, a regEx pattern in this case
91     * @access    private
92     * @var        string
93     */
94     var $_value;
95
96     /**
97     * field id that is used
98     * @access    private
99     * @var        string
100     */
101     var $_field;
102
103     /**
104     * set the value to compare with, a regEx pattern in this case
105     *
106     * @access    public
107     * @param    object patForms
108     */
109
110     function setValue($value)
111     {
112         $this->_value = $value;
113     }
114
115     /**
116     * prepare the rule
117     *
118     * @access    public
119     * @param    object patForms
120     */
121     function prepareRule(&$container)
122     {
123         patForms_Rule::prepareRule($container);
124
125         $onChange = $container->getAttribute('onchange');
126         $newHandler = sprintf('pfr_%s.validate();', $this->_id);
127         $container->setAttribute('onchange', $newHandler . $onChange);
128
129         return true;
130     }
131
132     /**
133     * method called by patForms or any patForms_Element to validate the
134     * element or the form.
135     *
136     * @access    public
137     * @param    object patForms    form object
138     */
139     function applyRule(&$element, $type = PATFORMS_RULE_AFTER_VALIDATION)
140     {
141         if (preg_match($this->_value, $element->getValue()) != 0){
142             return    true;
143         }
144
145         $this->addValidationError(1);
146         return false;
147     }
148
149     /**
150     * Registers scripts form this rule to the form
151     *
152     * @access    public
153     * @param    object patForms    form object
154     */
155     function registerJavascripts(&$form)
156     {
157         parent::registerJavascripts($form);
158
159         $script = sprintf("pfr_%s.setValue(%s);\n", $this->_id, $this->_value);
160         $form->registerInstanceJavascript($script);
161     }
162 }
163 ?>
Note: See TracBrowser for help on using the browser.