root/trunk/patForms/Rule/Enum.php

Revision 251, 3.6 kB (checked in by argh, 3 years ago)

Added french translations

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2 /**
3  * patForms Rule Enum
4  *
5  * $Id$
6  *
7  * @package        patForms
8  * @subpackage    Rules
9  */
10
11 /**
12  * patForms Rule Enum
13  *
14  * This is just a simple rule, that makes any field behave
15  * like an Enum field.
16  *
17  * @package        patForms
18  * @subpackage    Rules
19  * @author        Stephan Schmidt <schst@php-tools.net>
20  * @license        LGPL, see license.txt for details
21  * @link        http://www.php-tools.net
22  */
23 class patForms_Rule_Enum extends patForms_Rule
24 {
25    /**
26     * script that will be displayed only once
27     *
28     * @access    private
29     * @var        array
30     */
31     var $globalScript    =    array(
32                                         'html'    =>    "/* patForms::Rule::Enum */
33 function pFRC_Enum( field )
34 {
35     this.field = eval( 'pfe_' + field );
36     this.values = new Array();
37 }
38
39 pFRC_Enum.prototype.validate    = function()
40 {
41     value    =    this.field.getValue();
42     for( var i = 0; i < this.values.length; i++ )
43     {
44         if( this.values[i] == value )
45             return true;
46     }
47     alert( 'Ungültiger Wert!' );
48 }
49
50 pFRC_Enum.prototype.setValues    = function( values )
51 {
52     this.values    =    values;
53 }
54
55 /* END: patForms::Rule::Enum */
56 "
57                                     );
58
59    /**
60     * javascript that will be displayed once per instance
61     *
62     * @access    private
63     * @var        array
64     */
65     var $instanceScript    =    array(
66                                         'html'    =>    "var pfr_[RULE::ID] = new pFRC_Enum( '[CONTAINER::NAME]' );\n"
67                                     );
68
69    /**
70     * properties that have to be replaced in the instance script.
71     *
72     * @access    private
73     * @var        array
74     */
75     var $scriptPlaceholders    =    array(
76                                     'RULE::SOURCE'    =>    '_source',
77                                 );
78
79    /**
80     * name of the rule
81     *
82     * @abstract
83     * @access    private
84     */
85     var    $ruleName = 'Enum';
86
87    /**
88     * define error codes and messages for the rule
89     *
90     * @access    private
91     * @var        array    $validatorErrorCodes
92     * @todo     translate error messages
93     */
94     var    $validatorErrorCodes  =   array(
95         "C"    =>    array(
96             1    =>    "Please enter only one of the following values: [ENUM_VALUES]",
97         ),
98         "de" =>    array(
99             1    =>    "Bitte geben Sie einen der folgenden Werte ein: [ENUM_VALUES]",
100         ),
101         "fr" =>    array(
102             1    =>    "Veuillez n'entrer que l'une des valeurs suivantes: [ENUM_VALUES]",
103         )
104     );
105
106    /**
107     * possible values
108     * @access    private
109     * @var        array
110     */
111     var $_values;
112
113    /**
114     * field id that is used
115     * @access    private
116     * @var        string
117     */
118     var $_field;
119
120    /**
121     * prepare the rule
122     *
123     * @access    public
124     * @param    object patForms
125     */
126     function prepareRule( &$container )
127     {
128         patForms_Rule::prepareRule( $container );
129         
130         $onChange    =    $container->getAttribute( 'onchange' );
131         
132         $newHandler    =    sprintf( 'pfr_%s.validate();', $this->_id );
133         
134         $container->setAttribute( 'onchange', $newHandler . $onChange );   
135         
136         return true;
137     }
138
139    /**
140     * set the values
141     *
142     * @access    public
143     * @param    array    values
144     */
145     function setValues( $values )
146     {
147         $this->_values    =    $values;
148     }
149
150    /**
151     * method called by patForms or any patForms_Element to validate the
152     * element or the form.
153     *
154     * @access    public
155     * @param    object patForms    form object
156     */
157     function applyRule( &$element, $type = PATFORMS_RULE_BEFORE_VALIDATION )
158     {
159         if( in_array( $element->getValue(), $this->_values ) )
160             return    true;
161     
162         $vars    =    array(
163                             'enum_values'    =>    implode( ', ', $this->_values )
164                         );
165         
166         $this->addValidationError( 1, $vars );
167         return false;   
168     }
169
170    /**
171     * get the instance javascript of the rule
172     *
173     * @access    public
174     * @return    string
175     */
176     function getInstanceJavascript()
177     {
178         $script    =    patForms_Rule::getInstanceJavascript();
179
180         if( $script === false )
181         {
182             return false;
183         }
184         
185         $list    =    array();
186         foreach( $this->_values as $value )
187         {
188             array_push( $list, "'$value'" );
189         }
190         $script    .=    sprintf( "pfr_%s.setValues( new Array( %s ) );\n", $this->_id, implode( ',', $list ) );
191             
192         return $script;
193     }
194 }
195 ?>
Note: See TracBrowser for help on using the browser.