Welcome to Paster, Anonymous Friend!
Added by anonymous on 2008-10-22 19:23:11 Download/View
  1. class ujform extends form {
  2.         public static $errors = array();
  3.         public static $lang_key = 't';
  4.         public static function open($attr = array(), $lang_key = 't', $errors = array(), $hidden = NULL)
  5.         {
  6.                 is_string($attr) AND $attr = array('action' => $attr);
  7.                 self::$errors = $errors;
  8.                 self::$lang_key = $lang_key;
  9.                 $action = $attr['action'];
  10.                 unset($attr['action']);
  11.                 return parent::open($action, $attr, $hidden);
  12.         }
  13.         public static function errors($title = 'Oops there was an error!')
  14.         {
  15.                 if (empty(self::$errors) OR ! is_array(self::$errors))
  16.                         return '';
  17.                 $output = '<div class="form_errors"><h3>'.$title.'</h3><ul>';
  18.                 foreach(self::$errors AS $key => $error)
  19.                 {
  20.                         $error = Kohana::lang(self::$lang_key.'.'.$key.'.'.$error);
  21.                         $output .= '<li>'.html::anchor(Router::$complete_uri.'#'.$key, $error).'</li>';
  22.                 }
  23.                 return $output.'</ul></div>';
  24.         }
  25.         public static function label($text = '', $data = '', $extra = '')
  26.         {
  27.                 // Do we need to call Kohana::lang()?
  28.                 $text = (strpos($text, '.') === 0) ? Kohana::lang(self::$lang_key.$text) : $text;
  29.                 if ( ! is_string($data))
  30.                 {
  31.                         isset($data['for']) AND $field_name = $data['for'];
  32.                         // Use the name if we have one
  33.                         isset($data['name']) AND $field_name = $data['name'];
  34.                 }
  35.                 else
  36.                 {
  37.                         $field_name = $data;
  38.                 }
  39.                 // Display the errors
  40.                 if (isset($field_name) AND ! empty(self::$errors[$field_name]))
  41.                         $text = '<a name="'.$field_name.'">'.$text.'</a> - <span class="error">'.Kohana::lang(self::$lang_key.'.'.$field_name.'.'.self::$errors[$field_name]).'</span>';
  42.                 return parent::label($data, $text, $extra);
  43.         }
  44. }
  45.