root/trunk/patForms/Element/Radio.php

Revision 353, 9.5 kB (checked in by schst, 3 years ago)

Radio: fixed bug #203: problems with Radio/RadioGroup and value '0'

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2 /**
3  * simple radiobutton patForms element that builds and validates radio buttons, with the
4  * particularity that it does not generate a fully serialized element, but an array with
5  * serialized subelements.
6  *
7  * $Id$
8  *
9  * @access        protected
10  * @package        patForms
11  * @subpackage    Element
12  */
13
14 /**
15  * Error: no default value is available for the element.
16  */
17  define( 'PATFORMS_ERROR_RADIO_NO_DEFAULT_VALUE_AVAILABLE', 7001 );
18
19 /**
20  * Warning: the clicklabel attribute needs the id attribute to be set.
21  */
22  define( 'PATFORMS_WARNING_RADIO_CLICKLABEL_NEEDS_ID', 7002 );
23
24 /**
25  * Warning: the clicklabel attribute needs the label attribute to be set.
26  */
27  define( 'PATFORMS_WARNING_RADIO_CLICKLABEL_NEEDS_LABEL', 7003 );
28
29 /**
30  * simple radiobutton patForms element that builds and validates radio buttons, with the
31  * particularity that it does not generate a fully serialized element, but an array with
32  * serialized subelements.
33  *
34  * $Id$
35  *
36  * @access        protected
37  * @package        patForms
38  * @subpackage    Element
39  * @author        Sebastian Mordziol <argh@php-tools.net>
40  * @license        LGPL, see license.txt for details
41  */
42 class patForms_Element_Radio extends patForms_Element
43 {
44    /**
45     * Stores the name of the element - this is used mainly by the patForms
46     * error management and should be set in every element class.
47     * @access    public
48     */
49     var $elementName    =    'Radio';
50
51    /**
52     * the type of the element - set this to the type of element you are creating
53     * if you want to use the {@link patForms_Element::element2html()} method to
54     * create the final HTML tag for your element.
55     *
56     * @access    public
57     * @see        patForms_Element::element2html()
58     */
59     var $elementType    =    array(    "html"    =>    "input",
60                                 );
61
62    /**
63     * The radio element uses a renderer to serialize its content, so we set the flag
64     * to true here
65     *
66     * @access    private
67     * @var        boolean
68     */
69     var $usesRenderer    =    false;
70
71    /**
72     * set here which attributes you want to include in the element if you want to use
73     * the {@link patForms_Element::convertDefinition2Attributes()} method to automatically
74     * convert the values from your element definition into element attributes.
75     *
76     * @access    protected
77     * @see        patForms_Element::convertDefinition2Attribute()
78     */
79     var    $attributeDefinition    =    array(
80
81             "id"            =>    array(    "required"        =>    false,
82                                         "format"        =>    "string",
83                                         "outputFormats"    =>    array( "html" ),
84                                     ),
85             "name"            =>    array(    "required"        =>    true,
86                                         "format"        =>    "string",
87                                         "outputFormats"    =>    array( "html" ),
88                                     ),
89             "type"            =>    array(    "required"        =>    true,
90                                         "format"        =>    "string",
91                                         "outputFormats"    =>    array( "html" ),
92                                     ),
93             "title"            =>    array(    "required"        =>    false,
94                                         "format"        =>    "string",
95                                         "outputFormats"    =>    array( "html" ),
96                                         "modifiers"        =>    array( "insertSpecials" => array() ),
97                                     ),
98             "description"    =>    array(    "required"        =>    false,
99                                         "format"        =>    "string",
100                                         "outputFormats"    =>    array(),
101                                         "modifiers"        =>    array( "insertSpecials" => array() ),
102                                     ),
103             "default"        =>    array(    "required"        =>    false,
104                                         "format"        =>    "string",
105                                         "outputFormats"    =>    array(),
106                                     ),
107             "label"            =>    array(    "required"        =>    false,
108                                         "format"        =>    "string",
109                                         "outputFormats"    =>    array(),
110                                     ),
111             "display"        =>    array(    "required"        =>    false,
112                                         "format"        =>    "string",
113                                         "default"        =>    "yes",
114                                         "outputFormats"    =>    array(),
115                                     ),
116             "edit"            =>    array(    "required"        =>    false,
117                                         "format"        =>    "string",
118                                         "default"        =>    "yes",
119                                         "outputFormats"    =>    array(),
120                                     ),
121             "required"        =>    array(    "required"        =>    false,
122                                         "format"        =>    "string",
123                                         "default"        =>    "yes",
124                                         "outputFormats"    =>    array(),
125                                     ),
126             "value"            =>    array(    "required"        =>    false,
127                                         "format"        =>    "string",
128                                         "outputFormats"    =>    array( 'html' ),
129                                     ),
130             "style"            =>    array(    "required"        =>    false,
131                                         "outputFormats"    =>    array( "html" ),
132                                         "format"        =>    "string",
133                                     ),
134             "class"            =>    array(    "required"        =>    false,
135                                         "outputFormats"    =>    array( "html" ),
136                                         "format"        =>    "string",
137                                     ),
138             "onchange"        =>    array(    "required"        =>    false,
139                                         "format"        =>    "string",
140                                         "outputFormats"    =>    array( "html" ),
141                                         "modifiers"        =>    array( "insertSpecials" => array() ),
142                                     ),
143             "onclick"        =>    array(    "required"        =>    false,
144                                         "format"        =>    "string",
145                                         "outputFormats"    =>    array( "html" ),
146                                         "modifiers"        =>    array( "insertSpecials" => array() ),
147                                     ),
148             "onfocus"        =>    array(    "required"        =>    false,
149                                         "format"        =>    "string",
150                                         "outputFormats"    =>    array( "html" ),
151                                         "modifiers"        =>    array( "insertSpecials" => array() ),
152                                     ),
153             "onmouseover"    =>    array(    "required"        =>    false,
154                                         "format"        =>    "string",
155                                         "outputFormats"    =>    array( "html" ),
156                                         "modifiers"        =>    array( "insertSpecials" => array() ),
157                                     ),
158             "onmouseout"    =>    array(    "required"        =>    false,
159                                         "format"        =>    "string",
160                                         "outputFormats"    =>    array( "html" ),
161                                         "modifiers"        =>    array( "insertSpecials" => array() ),
162                                     ),
163             "onblur"        =>    array(    "required"        =>    false,
164                                         "format"        =>    "string",
165                                         "outputFormats"    =>    array( "html" ),
166                                         "modifiers"        =>    array( "insertSpecials" => array() ),
167                                     ),
168             "position"        =>    array(    "required"        =>    false,
169                                         "format"        =>    "int",
170                                         "outputFormats"    =>    array(),
171                                     ),
172             "values"        =>    array(    "required"        =>    false,
173                                         "format"        =>    "values",
174                                         "outputFormats"    =>    array(),
175                                     ),
176             "disabled"        =>    array(    "required"        =>    false,
177                                         "format"        =>    "string",
178                                         "default"        =>    "no",
179                                         "outputFormats"    =>    array( "html" ),
180                                     ),
181             "clicklabel"    =>    array(    "required"        =>    false,
182                                         "format"        =>    "string",
183                                         "outputFormats"    =>    array(),
184                                     ),
185             "checked"        =>    array(    "required"        =>    false,
186                                         "format"        =>    "string",
187                                         "outputFormats"    =>    array( 'html' ),
188                                     ),
189         );
190
191     /**
192      *    define error codes an messages for each form element
193      *
194      *  @access private
195      *  @var    array    $validatorErrorCodes
196      */
197     var    $validatorErrorCodes  =   array(
198         "C"    =>    array(
199             1    =>    "This field is has to be checked.",
200         ),
201         "de" =>    array(
202             1    =>    "Diese Option muss gewählt werden.",
203         ),
204         "fr" =>    array(
205             1    =>    "Cette option doit être sélectionnée.",
206         )
207     );
208
209    /**
210     * element creation method for the 'HTML' format in the 'default' form mode.
211     *
212     * @access    public
213     * @param    mixed    value of the element
214     * @return    mixed    $element    The element, or false if failed.
215     */
216     function serializeHtmlDefault( $value )
217     {
218         $this->attributes["type"]    =    "radio";
219
220         if( $this->attributes['display'] == 'no' )
221         {
222             return $this->createDisplaylessTag( $value );
223         }
224
225         if( $value == $this->attributes["value"] )
226         {
227             $this->attributes['checked'] = 'checked';
228         }
229
230         if( isset( $this->attributes["edit"] ) && $this->attributes["edit"] == "no" )
231         {
232             $this->attributes['disabled']    =    'yes';
233         }
234
235         $namespace = $this->getNamespace();
236         if( isset( $this->attributes['id'] ) && $namespace ) {
237             if (strpos($this->attributes['id'], $namespace . '_') !== 0) {
238                 $this->attributes['id'] = $namespace . '_' . $this->attributes['id'];
239             }
240         }
241
242         // clicklabel attribute
243         if( isset( $this->attributes['clicklabel'] ) && $this->attributes['clicklabel'] == 'yes' )
244         {
245             // call this to initialize all attributes
246             $this->getAttributesFor( $this->getFormat() );
247
248             if( !isset( $this->attributes['id'] ) )
249             {
250                 patErrorManager::raiseWarning(
251                     PATFORMS_WARNING_RADIO_CLICKLABEL_NEEDS_ID,
252                     'The "clicklabel" attribute needs the "id" attribute to be set.'
253                 );
254             }
255             else if( !isset( $this->attributes['label'] ) )
256             {
257                 patErrorManager::raiseWarning(
258                     PATFORMS_WARNING_RADIO_CLICKLABEL_NEEDS_LABEL,
259                     'The "clicklabel" attribute needs the "label" attribute to be set.'
260                 );
261             }
262             else
263             {
264                 $this->setAttribute( 'label', $this->createTag( 'label', 'full', array( 'for' => $this->getAttribute( 'id' ), 'title' => $this->getAttribute( 'title' ) ), $this->getAttribute( 'label' ) ) );
265             }
266         }
267
268         // create element
269         return $this->toHtml();
270     }
271
272    /**
273     * element creation method for the 'HTML' format in the 'readonly' form mode.
274     *
275     * @access    public
276     * @param    mixed    value of the element
277     * @return    string    $value    The element's value
278     */
279     function serializeHtmlReadonly( $value )
280     {
281         $tag = $this->createDisplaylessTag( $value );
282
283         if( $this->attributes['display'] == 'no' )
284         {
285             return $tag;
286         }
287
288         return $this->attributes['label'].$tag;
289     }
290
291    /**
292     * Retrieves the default value to display in the element's readonly mode if the
293     * user has not selected any entry, according to the selected locale
294     *
295     * @access    public
296     * @return    string    $defaultValue    The default readonly value in the needed locale
297     */
298     function getReadonlyDefaultValue()
299     {
300         $lang    =    $this->locale;
301
302         if( !isset( $this->defaultReadonlyValue[$lang] ) )
303         {
304             patErrorManager::raiseWarning(
305                 PATFORMS_ERROR_RADIO_NO_DEFAULT_VALUE_AVAILABLE,
306                 'There is no default readonly value available for the locale "'.$lang.'", using default locale "C" instead.'
307             );
308             return $this->defaultReadonlyValue['C'];
309         }
310
311         return $this->defaultReadonlyValue[$lang];
312     }
313
314    /**
315     * validates the element.
316     *
317     * @access    public
318     * @param    mixed    value of the element
319     * @return    bool    $isValid    True if element could be validated, false otherwise.
320     */
321     function validateElement( $value )
322     {
323         if ($this->attributes['required'] == 'yes' && strlen($value) === 0) {
324             $this->addValidationError( 1 );
325             return false;
326         }
327         return true;
328     }
329 }
330 ?>
Note: See TracBrowser for help on using the browser.