- Timestamp:
- 02/23/06 20:09:13 (6 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
trunk/trax/vendor/trax/action_view/helpers/form_helper.php
r132 r138 1 1 <?php 2 # $Id$ 3 # 4 # Copyright (c) 2005 John Peterson 5 # 6 # Permission is hereby granted, free of charge, to any person obtaining 7 # a copy of this software and associated documentation files (the 8 # "Software"), to deal in the Software without restriction, including 9 # without limitation the rights to use, copy, modify, merge, publish, 10 # distribute, sublicense, and/or sell copies of the Software, and to 11 # permit persons to whom the Software is furnished to do so, subject to 12 # the following conditions: 13 # 14 # The above copyright notice and this permission notice shall be 15 # included in all copies or substantial portions of the Software. 16 # 17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 2 /** 3 * File containing the FormHelper class 4 * 5 * (PHP 5) 6 * 7 * @package PHPonTrax 8 * @version $Id$ 9 * @copyright (c) 2005 John Peterson 10 * 11 * Permission is hereby granted, free of charge, to any person obtaining 12 * a copy of this software and associated documentation files (the 13 * "Software"), to deal in the Software without restriction, including 14 * without limitation the rights to use, copy, modify, merge, publish, 15 * distribute, sublicense, and/or sell copies of the Software, and to 16 * permit persons to whom the Software is furnished to do so, subject to 17 * the following conditions: 18 * 19 * The above copyright notice and this permission notice shall be 20 * included in all copies or substantial portions of the Software. 21 * 22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 23 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 25 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 26 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 27 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 28 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 29 */ 30 31 /** 32 * 33 * @package PHPonTrax 34 */ 25 35 class FormHelper extends Helpers { 26 36 37 /** 38 * 39 */ 27 40 function __construct($object_name, $attribute_name) { 28 41 parent::__construct(); … … 35 48 } 36 49 50 /** 51 * 52 */ 37 53 function value() { 38 54 if(!$value = $_REQUEST[$this->object_name][$this->attribute_name]) { … … 45 61 } 46 62 63 /** 64 * 65 */ 47 66 function object($object_name = null) { 48 67 $object_name = $object_name ? $object_name : $this->object_name; … … 50 69 } 51 70 71 /** 72 * 73 */ 52 74 function tag_name() { 53 75 return "{$this->object_name}[{$this->attribute_name}]"; 54 76 } 55 77 78 /** 79 * 80 */ 56 81 function tag_name_with_index($index) { 57 82 return "{$this->object_name}[{$index}][{$this->attribute_name}]"; 58 83 } 59 84 85 /** 86 * 87 */ 60 88 function tag_id() { 61 89 return "{$this->object_name}_{$this->attribute_name}"; 62 90 } 63 91 92 /** 93 * 94 */ 64 95 function tag_id_with_index($index) { 65 96 return "{$this->object_name}_{$index}_{$this->attribute_name}"; 66 97 } 67 98 99 /** 100 * 101 */ 68 102 function add_default_name_and_id($options) { 69 103 if(array_key_exists("index", $options)) { … … 81 115 } 82 116 117 /** 118 * 119 */ 83 120 function to_input_field_tag($field_type, $options = array()) { 84 121 $default_size = $options["maxlength"] ? $options["maxlength"] : $this->default_field_options['size']; … … 96 133 } 97 134 135 /** 136 * 137 */ 98 138 function to_radio_button_tag($tag_value, $options = array()) { 99 139 $options = array_merge($this->default_radio_options, $options); … … 111 151 } 112 152 153 /** 154 * 155 */ 113 156 function to_text_area_tag($options = array()) { 114 157 if ($options["size"]) { … … 123 166 } 124 167 168 /** 169 * 170 */ 125 171 function to_check_box_tag($options = array(), $checked_value = "1", $unchecked_value = "0") { 126 172 $options["type"] = "checkbox"; … … 153 199 } 154 200 201 /** 202 * 203 */ 155 204 function to_boolean_select_tag($options = array()) { 156 205 $options = $this->add_default_name_and_id($options); … … 179 228 180 229 181 ################################################################################################ 182 ##Avialble functions for use in views183 ################################################################################################ 184 # Example: text_field("post", "title"); 185 # Result: <input type="text" id="post_title" name="post[title]" value="$post->title" /> 230 /** 231 * Avialble functions for use in views 232 * Example: text_field("post", "title"); 233 * Result: <input type="text" id="post_title" name="post[title]" value="$post->title" /> 234 */ 186 235 function text_field($object, $field, $options = array()) { 187 236 $form = new FormHelper($object, $field); … … 189 238 } 190 239 191 # Works just like text_field, but returns a input tag of the "password" type instead. 192 # Example: password_field("user", "password"); 193 # Result: <input type="password" id="user_password" name="user[password]" value="$user->password" /> 240 /** 241 * Works just like text_field, but returns a input tag of the "password" type instead. 242 * Example: password_field("user", "password"); 243 * Result: <input type="password" id="user_password" name="user[password]" value="$user->password" /> 244 */ 194 245 function password_field($object, $field, $options = array()) { 195 246 $form = new FormHelper($object, $field); … … 197 248 } 198 249 199 # Works just like text_field, but returns a input tag of the "hidden" type instead. 200 # Example: hidden_field("post", "title"); 201 # Result: <input type="hidden" id="post_title" name="post[title]" value="$post->title" /> 250 /** 251 * Works just like text_field, but returns a input tag of the "hidden" type instead. 252 * Example: hidden_field("post", "title"); 253 * Result: <input type="hidden" id="post_title" name="post[title]" value="$post->title" /> 254 */ 202 255 function hidden_field($object, $field, $options = array()) { 203 256 $form = new FormHelper($object, $field); … … 205 258 } 206 259 207 # Works just like text_field, but returns a input tag of the "file" type instead, which won't have any default value. 260 /** 261 * Works just like text_field, but returns a input tag of the "file" type instead, which won't have any default value. 262 */ 208 263 function file_field($object, $field, $options = array()) { 209 264 $form = new FormHelper($object, $field); … … 211 266 } 212 267 213 # Example: text_area("post", "body", array("cols" => 20, "rows" => 40)); 214 # Result: <textarea cols="20" rows="40" id="post_body" name="post[body]">$post->body</textarea> 268 /** 269 * Example: text_area("post", "body", array("cols" => 20, "rows" => 40)); 270 * Result: <textarea cols="20" rows="40" id="post_body" name="post[body]">$post->body</textarea> 271 */ 215 272 function text_area($object, $field, $options = array()) { 216 273 $form = new FormHelper($object, $field); … … 218 275 } 219 276 220 # Returns a checkbox tag tailored for accessing a specified attribute (identified by $field) on an object 221 # assigned to the template (identified by $object). It's intended that $field returns an integer and if that 222 # integer is above zero, then the checkbox is checked. Additional $options on the input tag can be passed as an 223 # array with $options. The $checked_value defaults to 1 while the default $unchecked_value 224 # is set to 0 which is convenient for boolean values. Usually unchecked checkboxes don't post anything. 225 # We work around this problem by adding a hidden value with the same name as the checkbox. 277 /** 278 * Returns a checkbox tag tailored for accessing a specified attribute (identified by $field) on an object 279 * assigned to the template (identified by $object). It's intended that $field returns an integer and if that 280 * integer is above zero, then the checkbox is checked. Additional $options on the input tag can be passed as an 281 * array with $options. The $checked_value defaults to 1 while the default $unchecked_value 282 * is set to 0 which is convenient for boolean values. Usually unchecked checkboxes don't post anything. 283 * We work around this problem by adding a hidden value with the same name as the checkbox. 226 284 # 227 #Example: Imagine that $post->validated is 1:228 #check_box("post", "validated");229 #Result:230 #<input type="checkbox" id="post_validate" name="post[validated] value="1" checked="checked" />231 #<input name="post[validated]" type="hidden" value="0" />285 * Example: Imagine that $post->validated is 1: 286 * check_box("post", "validated"); 287 * Result: 288 * <input type="checkbox" id="post_validate" name="post[validated] value="1" checked="checked" /> 289 * <input name="post[validated]" type="hidden" value="0" /> 232 290 # 233 # Example: Imagine that $puppy->gooddog is no: 234 # check_box("puppy", "gooddog", array(), "yes", "no"); 235 # Result: 236 # <input type="checkbox" id="puppy_gooddog" name="puppy[gooddog] value="yes" /> 237 # <input name="puppy[gooddog]" type="hidden" value="no" /> 291 * Example: Imagine that $puppy->gooddog is no: 292 * check_box("puppy", "gooddog", array(), "yes", "no"); 293 * Result: 294 * <input type="checkbox" id="puppy_gooddog" name="puppy[gooddog] value="yes" /> 295 * <input name="puppy[gooddog]" type="hidden" value="no" /> 296 */ 238 297 function check_box($object, $field, $options = array(), $checked_value = "1", $unchecked_value = "0") { 239 298 $form = new FormHelper($object, $field); … … 241 300 } 242 301 243 # Returns a radio button tag for accessing a specified attribute (identified by $field) on an object 244 # assigned to the template (identified by $object). If the current value of $field is $tag_value the 245 # radio button will be checked. Additional $options on the input tag can be passed as a 246 # hash with $options. 247 # Example: Imagine that $post->category is "trax": 248 # radio_button("post", "category", "trax"); 249 # radio_button("post", "category", "java"); 250 # Result: 251 # <input type="radio" id="post_category" name="post[category] value="trax" checked="checked" /> 252 # <input type="radio" id="post_category" name="post[category] value="java" /> 302 /** 303 * Returns a radio button tag for accessing a specified attribute (identified by $field) on an object 304 * assigned to the template (identified by $object). If the current value of $field is $tag_value the 305 * radio button will be checked. Additional $options on the input tag can be passed as a 306 * hash with $options. 307 * Example: Imagine that $post->category is "trax": 308 * radio_button("post", "category", "trax"); 309 * radio_button("post", "category", "java"); 310 * Result: 311 * <input type="radio" id="post_category" name="post[category] value="trax" checked="checked" /> 312 * <input type="radio" id="post_category" name="post[category] value="java" /> 313 */ 253 314 function radio_button($object, $field, $tag_value, $options = array()) { 254 315 $form = new FormHelper($object, $field); … … 256 317 } 257 318 319 /** 320 * 321 */ 258 322 function boolean_select($object, $field, $options = array()) { 259 323 $form = new FormHelper($object, $field);
