Root/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 | <?php /* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* # ***** BEGIN LICENSE BLOCK ***** # This file is part of Plume Framework, a simple PHP Application Framework. # Copyright (C) 2001-2007 Loic d'Anterroches and contributors. # # Plume Framework is free software; you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation; either version 2.1 of the License, or # (at your option) any later version. # # Plume Framework is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # # ***** END LICENSE BLOCK ***** */ /** * Form validation class. * * This class is used to generate a form. You basically build it the * same way you build a model. * * The form handling is heavily inspired by the Django form handling. * */ class Pluf_Form implements Iterator, ArrayAccess { /** * The fields of the form. * * They are the fully populated Pluf_Form_Field_* of the form. You * define them in the initFields method. */ public $fields = array (); /** * Prefix for the names of the fields. */ public $prefix = '' ; public $id_fields = 'id_%s' ; public $data = array (); public $cleaned_data = array (); public $errors = array (); public $is_bound = false; public $f = null; public $label_suffix = ':' ; protected $is_valid = null; function __construct( $data =null, $extra = array (), $label_suffix =null) { if ( $data !== null) { $this ->data = $data ; $this ->is_bound = true; } if ( $label_suffix !== null) $this ->label_suffix = $label_suffix ; $this ->initFields( $extra ); $this ->f = new Pluf_Form_FieldProxy( $this ); } function initFields( $extra = array ()) { throw new Exception( 'Definition of the fields not implemented.' ); } /** * Add the prefix to the form names. * * @param string Field name. * @return string Field name or field name with form prefix. */ function addPrefix( $field_name ) { if ( '' !== $this ->prefix) { return $this ->prefix. '-' . $field_name ; } return $field_name ; } /** * Check if the form is valid. * * It is also encoding the data in the form to be then saved. It * is very simple as it leaves the work to the field. It means * that you can easily extend this form class to have a more * complex validation procedure like checking if a field is equals * to another in the form (like for password confirmation) etc. * * @param array Associative array of the request * @return array Array of errors */ function isValid() { if ( $this ->is_valid !== null) { return $this ->is_valid; } $this ->cleaned_data = array (); $this ->errors = array (); $form_methods = get_class_methods( $this ); foreach ( $this ->fields as $name => $field ) { $value = $field ->widget->valueFromFormData( $this ->addPrefix( $name ), $this ->data); try { $value = $field ->clean( $value ); $this ->cleaned_data[ $name ] = $value ; if (in_array( 'clean_' . $name , $form_methods )) { $m = 'clean_' . $name ; $value = $this -> $m (); $this ->cleaned_data[ $name ] = $value ; } } catch (Pluf_Form_Invalid $e ) { if (!isset( $this ->errors[ $name ])) $this ->errors[ $name ] = array (); $this ->errors[ $name ][] = $e ->getMessage(); if (isset( $this ->cleaned_data[ $name ])) { unset( $this ->cleaned_data[ $name ]); } } } if ( empty ( $this ->errors)) { try { $this ->cleaned_data = $this ->clean(); } catch (Pluf_Form_Invalid $e ) { if (!isset( $this ->errors[ '__all__' ])) $this ->errors[ '__all__' ] = array (); $this ->errors[ '__all__' ][] = $e ->getMessage(); } } if ( empty ( $this ->errors)) { $this ->is_valid = true; return true; } // as some errors, we do not have cleaned data available. $this ->failed(); $this ->cleaned_data = array (); $this ->is_valid = false; return false; } /** * Form wide cleaning function. That way you can check that if an * input is given, then another one somewhere is also given, * etc. If the cleaning is not ok, your method must throw a * Pluf_Form_Invalid exception. * * @return array Cleaned data. */ public function clean() { return $this ->cleaned_data; } /** * Method just called after the validation if the validation * failed. This can be used to remove uploaded * files. $this->['cleaned_data'] will be available but of course * not fully populated and with possible garbage due to the error. * */ public function failed() { } /** * Get initial data for a given field. * * @param string Field name. * @return string Initial data or '' of not defined. */ public function initial( $name ) { if (isset( $this ->fields[ $name ])) { return $this ->fields[ $name ]->initial; } return '' ; } /** * Get the top errors. */ public function render_top_errors() { $top_errors = (isset( $this ->errors[ '__all__' ])) ? $this ->errors[ '__all__' ] : array (); array_walk ( $top_errors , 'Pluf_Form_htmlspecialcharsArray' ); return new Pluf_Template_SafeString(Pluf_Form_renderErrorsAsHTML( $top_errors ), true); } /** * Get the top errors. */ public function get_top_errors() { return (isset( $this ->errors[ '__all__' ])) ? $this ->errors[ '__all__' ] : array (); } /** * Helper function to render the form. * * See render_p() for a usage example. * * @credit Django Project (http://www.djangoproject.com/) * @param string Normal row. * @param string Error row. * @param string Row ender. * @param string Help text HTML. * @param bool Should we display errors on a separate row. * @return string HTML of the form. */ protected function htmlOutput( $normal_row , $error_row , $row_ender , $help_text_html , $errors_on_separate_row ) { $top_errors = (isset( $this ->errors[ '__all__' ])) ? $this ->errors[ '__all__' ] : array (); array_walk ( $top_errors , 'Pluf_Form_htmlspecialcharsArray' ); $output = array (); $hidden_fields = array (); foreach ( $this ->fields as $name => $field ) { $bf = new Pluf_Form_BoundField( $this , $field , $name ); $bf_errors = $bf ->errors; array_walk ( $bf_errors , 'Pluf_Form_htmlspecialcharsArray' ); if ( $field ->widget->is_hidden) { foreach ( $bf_errors as $_e ) { $top_errors [] = sprintf(__( '(Hidden field %1$s) %2$s' ), $name , $_e ); } $hidden_fields [] = $bf ; // Not rendered } else { if ( $errors_on_separate_row and count ( $bf_errors )) { $output [] = sprintf( $error_row , Pluf_Form_renderErrorsAsHTML( $bf_errors )); } if ( strlen ( $bf ->label) > 0) { $label = htmlspecialchars( $bf ->label, ENT_COMPAT, 'UTF-8' ); if ( $this ->label_suffix) { if (!in_array(mb_substr( $label , -1, 1), array ( ':' , '?' , '.' , '!' ))) { $label .= $this ->label_suffix; } } $label = $bf ->labelTag( $label ); } else { $label = '' ; } if ( $bf ->help_text) { // $bf->help_text can contains HTML and is not // escaped. $help_text = sprintf( $help_text_html , $bf ->help_text); } else { $help_text = '' ; } $errors = '' ; if (! $errors_on_separate_row and count ( $bf_errors )) { $errors = Pluf_Form_renderErrorsAsHTML( $bf_errors ); } $output [] = sprintf( $normal_row , $errors , $label , $bf ->render_w(), $help_text ); } } if ( count ( $top_errors )) { $errors = sprintf( $error_row , Pluf_Form_renderErrorsAsHTML( $top_errors )); array_unshift ( $output , $errors ); } if ( count ( $hidden_fields )) { $_tmp = '' ; foreach ( $hidden_fields as $hd ) { $_tmp .= $hd ->render_w(); } if ( count ( $output )) { $last_row = array_pop ( $output ); $last_row = substr ( $last_row , 0, - strlen ( $row_ender )). $_tmp . $row_ender ; $output [] = $last_row ; } else { $output [] = $_tmp ; } } return new Pluf_Template_SafeString(implode( "\n" , $output ), true); } /** * Render the form as a list of paragraphs. */ public function render_p() { return $this ->htmlOutput( '<p>%1$s%2$s %3$s%4$s</p>' , '%s' , '</p>' , ' %s' , true); } /** * Render the form as a list without the <ul></ul>. */ public function render_ul() { return $this ->htmlOutput( '<li>%1$s%2$s %3$s%4$s</li>' , '<li>%s</li>' , '</li>' , ' %s' , false); } /** * Render the form as a table without <table></table>. */ public function render_table() { return $this ->htmlOutput( '<tr><th>%2$s</th><td>%1$s%3$s%4$s</td></tr>' , '<tr><td colspan="2">%s</td></tr>' , '</td></tr>' , '<br /><span class="helptext">%s</span>' , false); } /** * Overloading of the get method. * * The overloading is to be able to use property call in the * templates. */ function __get( $prop ) { if (!in_array( $prop , array ( 'render_p' , 'render_ul' , 'render_table' , 'render_top_errors' , 'get_top_errors' ))) { return $this -> $prop ; } return $this -> $prop (); } /** * Get a given field by key. */ public function field( $key ) { return new Pluf_Form_BoundField( $this , $this ->fields[ $key ], $key ); } /** * Iterator method to iterate over the fields. * * Get the current item. */ public function current() { $field = current( $this ->fields); $name = key( $this ->fields); return new Pluf_Form_BoundField( $this , $field , $name ); } public function key() { return key( $this ->fields); } public function next() { next( $this ->fields); } public function rewind () { reset( $this ->fields); } public function valid() { // We know that the boolean false will not be stored as a // field, so we can test against false to check if valid or // not. return (false !== current( $this ->fields)); } public function offsetUnset( $index ) { unset( $this ->fields[ $index ]); } public function offsetSet( $index , $value ) { $this ->fields[ $index ] = $value ; } public function offsetGet( $index ) { if (!isset( $this ->fields[ $index ])) { throw new Exception( 'Undefined index: ' . $index ); } return $this ->fields[ $index ]; } public function offsetExists( $index ) { return (isset( $this ->fields[ $index ])); } } function Pluf_Form_htmlspecialcharsArray(& $item , $key ) { $item = htmlspecialchars( $item , ENT_COMPAT, 'UTF-8' ); } function Pluf_Form_renderErrorsAsHTML( $errors ) { $tmp = array (); foreach ( $errors as $err ) { $tmp [] = '<li>' . $err . '</li>' ; } return '<ul class="errorlist">' .implode( "\n" , $tmp ). '</ul>' ; } |