| [ Index ] |
|
Code source de CakePHP 1.1.13.4450 |
1 <?php 2 /* SVN FILE: $Id: form.php 4409 2007-02-02 13:20:59Z phpnut $ */ 3 /** 4 * Automatic generation of HTML FORMs from given data. 5 * 6 * Used for scaffolding. 7 * 8 * PHP versions 4 and 5 9 * 10 * CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/> 11 * Copyright 2005-2007, Cake Software Foundation, Inc. 12 * 1785 E. Sahara Avenue, Suite 490-204 13 * Las Vegas, Nevada 89104 14 * 15 * Licensed under The MIT License 16 * Redistributions of files must retain the above copyright notice. 17 * 18 * @filesource 19 * @copyright Copyright 2005-2007, Cake Software Foundation, Inc. 20 * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project 21 * @package cake 22 * @subpackage cake.cake.libs.view.helpers 23 * @since CakePHP(tm) v 0.10.0.1076 24 * @version $Revision: 4409 $ 25 * @modifiedby $LastChangedBy: phpnut $ 26 * @lastmodified $Date: 2007-02-02 07:20:59 -0600 (Fri, 02 Feb 2007) $ 27 * @license http://www.opensource.org/licenses/mit-license.php The MIT License 28 */ 29 /** 30 * Tag template for a div with a class attribute. 31 */ 32 define('TAG_DIV', '<div class="%s">%s</div>'); 33 /** 34 * Tag template for a paragraph with a class attribute. 35 */ 36 define('TAG_P_CLASS', '<p class="%s">%s</p>'); 37 /** 38 * Tag template for a label with a for attribute. 39 */ 40 define('TAG_LABEL', '<label for="%s">%s</label>'); 41 /** 42 * Tag template for a fieldset with a legend tag inside. 43 */ 44 define('TAG_FIELDSET', '<fieldset><legend>%s</legend>%s</label>'); 45 /** 46 * Form helper library. 47 * 48 * Automatic generation of HTML FORMs from given data. 49 * 50 * @package cake 51 * @subpackage cake.cake.libs.view.helpers 52 */ 53 class FormHelper extends Helper{ 54 /** 55 * Included helpers. 56 * 57 * @var array 58 * @access public 59 */ 60 var $helpers = array('Html'); 61 /** 62 * Returns a formatted error message for given FORM field, NULL if no errors. 63 * 64 * @param string $field This should be "Modelname/fieldname" 65 * @return bool If there are errors this method returns true, else false. 66 * @access public 67 */ 68 function isFieldError($field) { 69 $error=1; 70 $this->Html->setFormTag($field); 71 72 if ($error == $this->Html->tagIsInvalid($this->Html->model, $this->Html->field)) { 73 return true; 74 } else { 75 return false; 76 } 77 } 78 /** 79 * Returns a formatted LABEL element for HTML FORMs. 80 * 81 * @param string $tagName This should be "Modelname/fieldname" 82 * @param string $text Text that will appear in the label field. 83 * @return string The formatted LABEL element 84 * @access public 85 */ 86 function labelTag($tagName, $text) { 87 return sprintf(TAG_LABEL, Inflector::camelize(str_replace('/', '_', $tagName)), $text); 88 } 89 /** 90 * Returns a formatted DIV tag for HTML FORMs. 91 * 92 * @param string $class CSS class name of the div element. 93 * @param string $text String content that will appear inside the div element. 94 * @return string The formatted DIV element 95 * @access public 96 */ 97 function divTag($class, $text) { 98 return sprintf(TAG_DIV, $class, $text); 99 } 100 /** 101 * Returns a formatted P tag with class for HTML FORMs. 102 * 103 * @param string $class CSS class name of the p element. 104 * @param string $text Text that will appear inside the p element. 105 * @return string The formatted P element 106 * @access public 107 */ 108 function pTag($class, $text) { 109 return sprintf(TAG_P_CLASS, $class, $text); 110 } 111 /** 112 * Returns a formatted INPUT tag for HTML FORMs. 113 * 114 * @param string $tagName This should be "Modelname/fieldname" 115 * @param string $prompt Text that will appear in the label field. 116 * @param bool $required True if this field is a required field. 117 * @param string $errorMsg Text that will appear if an error has occurred. 118 * @param int $size Size attribute for INPUT element 119 * @param array $htmlOptions HTML options array. 120 * @return string The formatted INPUT element, with a label and wrapped in a div. 121 * @access public 122 */ 123 function generateInputDiv($tagName, $prompt, $required = false, $errorMsg = null, $size = 20, $htmlOptions = null) { 124 $htmlAttributes = $htmlOptions; 125 $htmlAttributes['size'] = $size; 126 $str = $this->Html->input($tagName, $htmlAttributes); 127 $strLabel = $this->labelTag($tagName, $prompt); 128 $divClass = "optional"; 129 if ($required) { 130 $divClass = "required"; 131 } 132 $strError = ""; 133 134 if ($this->isFieldError($tagName)) { 135 $strError = $this->pTag('error', $errorMsg); 136 $divClass = sprintf("%s error", $divClass); 137 } 138 $divTagInside = sprintf("%s %s %s", $strError, $strLabel, $str); 139 return $this->divTag($divClass, $divTagInside); 140 } 141 /** 142 * Returns a formatted CHECKBOX tag inside a DIV for HTML FORMs. 143 * 144 * @param string $tagName This should be "Modelname/fieldname" 145 * @param string $prompt Text that will appear in the label field. 146 * @param bool $required True if this field is a required field. 147 * @param string $errorMsg Text that will appear if an error has occurred. 148 * @param array $htmlOptions HTML options array. 149 * @return string The formatted checkbox div 150 * @access public 151 */ 152 function generateCheckboxDiv($tagName, $prompt, $required = false, $errorMsg = null, $htmlOptions = null) { 153 $htmlOptions['class'] = "inputCheckbox"; 154 $str = $this->Html->checkbox($tagName, null, $htmlOptions); 155 $strLabel = $this->labelTag($tagName, $prompt); 156 $divClass = "optional"; 157 if ($required) { 158 $divClass = "required"; 159 } 160 $strError = ""; 161 162 if ($this->isFieldError($tagName)) { 163 $strError = $this->pTag('error', $errorMsg); 164 $divClass = sprintf("%s error", $divClass); 165 } 166 $divTagInside = sprintf("%s %s %s", $strError, $strLabel, $str); 167 return $this->divTag($divClass, $divTagInside); 168 } 169 /** 170 * Returns a formatted date option element for HTML FORMs. 171 * 172 * @param string $tagName This should be "Modelname/fieldname" 173 * @param string $prompt Text that will appear in the label field. 174 * @param bool $required True if this field is a required field. 175 * @param string $errorMsg Text that will appear if an error has occurred. 176 * @param int $size Not used. 177 * @param array $htmlOptions HTML options array 178 * @return string Date option wrapped in a div. 179 * @todo Remove the $size parameter from this method. 180 * @access public 181 */ 182 function generateDate($tagName, $prompt, $required = false, $errorMsg = null, $size = 20, $htmlOptions = null, $selected = null) { 183 $str = $this->Html->dateTimeOptionTag($tagName, 'MDY', 'NONE', $selected, $htmlOptions); 184 $strLabel = $this->labelTag($tagName, $prompt); 185 $divClass = "optional"; 186 if ($required) { 187 $divClass = "required"; 188 } 189 $strError = ""; 190 191 if ($this->isFieldError($tagName)) { 192 $strError = $this->pTag('error', $errorMsg); 193 $divClass = sprintf("%s error", $divClass); 194 } 195 $divTagInside = sprintf("%s %s %s", $strError, $strLabel, $str); 196 $requiredDiv = $this->divTag($divClass, $divTagInside); 197 return $this->divTag("date", $requiredDiv); 198 } 199 /** 200 * Returns a formatted date option element for HTML FORMs. 201 * 202 * @param string $tagName This should be "Modelname/fieldname" 203 * @param string $prompt Text that will appear in the label field. 204 * @param bool $required True if this field is a required field. 205 * @param string $errorMsg Text that will appear if an error has occurred. 206 * @param int $size Not used. 207 * @param array $htmlOptions HTML options array 208 * @return string Date option wrapped in a div. 209 * @todo Remove the $size parameter from this method. 210 * @access public 211 */ 212 function generateTime($tagName, $prompt, $required = false, $errorMsg = null, $size = 20, $htmlOptions = null, $selected = null) { 213 $str = $this->Html->dateTimeOptionTag($tagName, 'NONE', '24', $selected, $htmlOptions); 214 $strLabel = $this->labelTag($tagName, $prompt); 215 $divClass = "optional"; 216 if ($required) { 217 $divClass = "required"; 218 } 219 $strError = ""; 220 221 if ($this->isFieldError($tagName)) { 222 $strError = $this->pTag('error', $errorMsg); 223 $divClass = sprintf("%s error", $divClass); 224 } 225 $divTagInside = sprintf("%s %s %s", $strError, $strLabel, $str); 226 $requiredDiv = $this->divTag($divClass, $divTagInside); 227 return $this->divTag("time", $requiredDiv); 228 } 229 /** 230 * Returns a formatted year option element for HTML FORMs. 231 * 232 * @param string $tagName This should be "Modelname/fieldname" 233 * @param string $prompt Text that will appear in the label field. 234 * @param bool $required True if this field is a required field. 235 * @param string $errorMsg Text that will appear if an error has occurred. 236 * @param int $size Not used. 237 * @param array $htmlOptions HTML options array 238 * @return string Date option wrapped in a div. 239 * @todo Remove the $size parameter from this method. 240 * @access public 241 */ 242 function generateYear($tagName, $prompt, $required = false, $errorMsg = null, $size = 20, $htmlOptions = null, $selected = null) { 243 $str = $this->Html->dateTimeOptionTag($tagName, 'Y', 'NONE', $selected, $htmlOptions); 244 $strLabel = $this->labelTag($tagName, $prompt); 245 $divClass = "optional"; 246 if ($required) { 247 $divClass = "required"; 248 } 249 $strError = ""; 250 251 if ($this->isFieldError($tagName)) { 252 $strError = $this->pTag('error', $errorMsg); 253 $divClass = sprintf("%s error", $divClass); 254 } 255 $divTagInside = sprintf("%s %s %s", $strError, $strLabel, $str); 256 $requiredDiv = $this->divTag($divClass, $divTagInside); 257 return $this->divTag("year", $requiredDiv); 258 } 259 /** 260 * Returns a formatted datetime option element for HTML FORMs. 261 * 262 * @param string $tagName This should be "Modelname/fieldname" 263 * @param string $prompt Text that will appear in the label field. 264 * @param bool $required True if this field is required. 265 * @param string $errorMsg Text that will appear if an error has occurred. 266 * @param int $size Not used. 267 * @param array $htmlOptions HTML options array 268 * @param array $selected Selected index in the dateTimeOption tag. 269 * @return string The formatted datetime option element wrapped in a div. 270 * @todo Remove the $size parameter from this method. 271 * @access public 272 */ 273 function generateDateTime($tagName, $prompt, $required = false, $errorMsg = null, $size = 20, $htmlOptions = null, $selected = null) { 274 $str = $this->Html->dateTimeOptionTag($tagName, 'MDY', '12', $selected, $htmlOptions); 275 $strLabel = $this->labelTag($tagName, $prompt); 276 $divClass = "optional"; 277 if ($required) { 278 $divClass = "required"; 279 } 280 $strError = ""; 281 282 if ($this->isFieldError($tagName)) { 283 $strError = $this->pTag('error', $errorMsg); 284 $divClass = sprintf("%s error", $divClass); 285 } 286 $divTagInside = sprintf("%s %s %s", $strError, $strLabel, $str); 287 $requiredDiv = $this->divTag($divClass, $divTagInside); 288 return $this->divTag("date", $requiredDiv); 289 } 290 /** 291 * Returns a formatted TEXTAREA inside a DIV for use with HTML forms. 292 * 293 * @param string $tagName This should be "Modelname/fieldname" 294 * @param string $prompt Text that will appear in the label field. 295 * @param boolean $required True if this field is required. 296 * @param string $errorMsg ext that will appear if an error has occurred. 297 * @param integer $cols Number of columns. 298 * @param integer $rows Number of rows. 299 * @param array $htmlOptions HTML options array. 300 * @return string The formatted TEXTAREA element, wrapped in a div. 301 * @access public 302 */ 303 function generateAreaDiv($tagName, $prompt, $required = false, $errorMsg = null, $cols = 60, $rows = 10, $htmlOptions = null) { 304 $htmlAttributes = $htmlOptions; 305 $htmlAttributes['cols'] = $cols; 306 $htmlAttributes['rows'] = $rows; 307 $str = $this->Html->textarea($tagName, $htmlAttributes); 308 $strLabel = $this->labelTag($tagName, $prompt); 309 $divClass = "optional"; 310 311 if ($required) { 312 $divClass="required"; 313 } 314 $strError = ""; 315 316 if ($this->isFieldError($tagName)) { 317 $strError = $this->pTag('error', $errorMsg); 318 $divClass = sprintf("%s error", $divClass); 319 } 320 $divTagInside = sprintf("%s %s %s", $strError, $strLabel, $str); 321 return $this->divTag($divClass, $divTagInside); 322 } 323 /** 324 * Returns a formatted SELECT tag for HTML FORMs. 325 * 326 * @param string $tagName This should be "Modelname/fieldname" 327 * @param string $prompt Text that will appear in the label field 328 * @param array $options Options to be contained in SELECT element 329 * @param string $selected Currently selected item 330 * @param array $selectAttr Array of HTML attributes for the SELECT element 331 * @param array $optionAttr Array of HTML attributes for the OPTION elements 332 * @param bool $required True if this field is required 333 * @param string $errorMsg Text that will appear if an error has occurred 334 * @return string The formatted INPUT element, wrapped in a div 335 * @access public 336 */ 337 function generateSelectDiv($tagName, $prompt, $options, $selected = null, $selectAttr = null, $optionAttr = null, $required = false, $errorMsg = null) { 338 $str = $this->Html->selectTag($tagName, $options, $selected, $selectAttr, $optionAttr); 339 $strLabel = $this->labelTag($tagName, $prompt); 340 $divClass = "optional"; 341 342 if ($required) { 343 $divClass = "required"; 344 } 345 $strError = ""; 346 347 if ($this->isFieldError($tagName)) { 348 $strError=$this->pTag('error', $errorMsg); 349 $divClass=sprintf("%s error", $divClass); 350 } 351 $divTagInside = sprintf("%s %s %s", $strError, $strLabel, $str); 352 return $this->divTag($divClass, $divTagInside); 353 } 354 /** 355 * Returns a formatted submit widget for HTML FORMs. 356 * 357 * @param string $displayText Text that will appear on the widget 358 * @param array $htmlOptions HTML options array 359 * @return string The formatted submit widget 360 * @access public 361 */ 362 function generateSubmitDiv($displayText, $htmlOptions = null) { 363 return $this->divTag('submit', $this->Html->submit($displayText, $htmlOptions)); 364 } 365 /** 366 * Generates a form to go onto a HtmlHelper object. 367 * 368 * @param array $fields An array of form field definitions 369 * @param boolean $readOnly True if the form should be rendered as READONLY 370 * @return string The completed form specified by the $fields parameter 371 * @access public 372 */ 373 function generateFields($fields, $readOnly = false) { 374 $strFormFields = ''; 375 376 foreach($fields as $field) { 377 if (isset($field['type'])) { 378 379 if (!isset($field['required'])) { 380 $field['required'] = false; 381 } 382 383 if (!isset($field['errorMsg'])) { 384 $field['errorMsg'] = null; 385 } 386 387 if (!isset($field['htmlOptions'])) { 388 $field['htmlOptions'] = array(); 389 } 390 391 if ($readOnly) { 392 $field['htmlOptions']['READONLY'] = "readonly"; 393 } 394 395 switch($field['type']) { 396 case "input": 397 if (!isset($field['size'])) { 398 $field['size'] = 40; 399 } 400 $strFormFields = $strFormFields . $this->generateInputDiv($field['tagName'], $field['prompt'], 401 $field['required'], $field['errorMsg'], $field['size'], $field['htmlOptions']); 402 break; 403 case "checkbox": 404 $strFormFields = $strFormFields . $this->generateCheckboxDiv($field['tagName'], $field['prompt'], 405 $field['required'], $field['errorMsg'], $field['htmlOptions']); 406 break; 407 case "select": 408 case "selectMultiple": 409 if ("selectMultiple" == $field['type']) { 410 $field['selectAttr']['multiple'] = 'multiple'; 411 $field['selectAttr']['class'] = 'selectMultiple'; 412 } 413 414 if (!isset($field['selected'])) { 415 $field['selected'] = null; 416 } 417 418 if (!isset($field['selectAttr'])) { 419 $field['selectAttr'] = null; 420 } 421 422 if (!isset($field['optionsAttr'])) { 423 $field['optionsAttr'] = null; 424 } 425 426 if ($readOnly) { 427 $field['selectAttr']['DISABLED'] = true; 428 } 429 430 if (!isset($field['options'])) { 431 $field['options'] = null; 432 } 433 $strFormFields = $strFormFields . $this->generateSelectDiv($field['tagName'], $field['prompt'], $field['options'], 434 $field['selected'], $field['selectAttr'], $field['optionsAttr'], $field['required'], $field['errorMsg']); 435 break; 436 case "area": 437 if (!isset($field['rows'])) { 438 $field['rows'] = 10; 439 } 440 441 if (!isset($field['cols'])) { 442 $field['cols'] = 60; 443 } 444 $strFormFields = $strFormFields . $this->generateAreaDiv($field['tagName'], $field['prompt'], 445 $field['required'], $field['errorMsg'], $field['cols'], $field['rows'], $field['htmlOptions']); 446 break; 447 case "fieldset": 448 $strFieldsetFields = $this->generateFields($field['fields']); 449 $strFieldSet = sprintf(' <fieldset><legend>%s</legend><div class="notes"><h4>%s</h4><p class="last">%s</p></div>%s</fieldset>', 450 $field['legend'], $field['noteHeading'], $field['note'], $strFieldsetFields); 451 $strFormFields = $strFormFields . $strFieldSet; 452 break; 453 case "hidden": 454 if(!isset($field['value'])){ 455 $field['value'] = null; 456 } 457 $strFormFields = $strFormFields . $this->Html->hidden($field['tagName'], $field['value']); 458 break; 459 case "date": 460 if (!isset($field['selected'])) { 461 $field['selected'] = null; 462 } 463 $strFormFields = $strFormFields . $this->generateDate($field['tagName'], $field['prompt'], $field['required'], $field['errorMsg'], null, $field['htmlOptions'], $field['selected']); 464 break; 465 case "datetime": 466 if (!isset($field['selected'])) { 467 $field['selected'] = null; 468 } 469 $strFormFields = $strFormFields . $this->generateDateTime($field['tagName'], $field['prompt'], $field['required'], $field['errorMsg'], null, $field['htmlOptions'], $field['selected']); 470 break; 471 case "time": 472 if (!isset($field['selected'])) { 473 $field['selected'] = null; 474 } 475 $strFormFields = $strFormFields . $this->generateTime($field['tagName'], $field['prompt'], $field['required'], $field['errorMsg'], null, $field['htmlOptions'], $field['selected']); 476 break; 477 case "year": 478 if (!isset($field['selected'])) { 479 $field['selected'] = null; 480 } 481 $strFormFields = $strFormFields . $this->generateYear($field['tagName'], $field['prompt'], $field['required'], $field['errorMsg'], null, $field['htmlOptions'], $field['selected']); 482 break; 483 default: 484 break; 485 } 486 } 487 } 488 return $strFormFields; 489 } 490 } 491 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Sun Feb 25 19:27:47 2007 | par Balluche grâce à PHPXref 0.7 |