Guest User

Untitled

a guest
Feb 21st, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. class form extends form_core {
  2.  
  3. public static $errors = array();
  4. public static $form_data = array();
  5. public static $lang_key;
  6.  
  7. public static function open($attr = array(), $lang_key = '', $errors = array(), $form_data = array(), $hidden = NULL)
  8. {
  9. is_string($attr) AND $attr = array('action' => $attr);
  10.  
  11. self::$errors = $errors;
  12. self::$lang_key = $lang_key;
  13. self::$form_data = $form_data;
  14.  
  15. $action = $attr['action'];
  16. unset($attr['action']);
  17.  
  18. return parent::open($action, $attr, $hidden);
  19. }
  20.  
  21.  
  22. public static function errors($title = 'Oops there was an error!')
  23. {
  24. if (empty(self::$errors) OR ! is_array(self::$errors))
  25. return '';
  26.  
  27. $output = '<div class="form_errors"><h3>'.$title.'</h3><ul>';
  28.  
  29. foreach(self::$errors AS $key => $error)
  30. {
  31. $error = Kohana::lang(self::$lang_key.'.'.$key.'.'.$error);
  32. $output .= '<li>'.html::anchor('#'.$key, $error).'</li>';
  33. }
  34.  
  35. return $output.'</ul></div>';
  36. }
  37.  
  38. public static function label($text = '', $data = '', $extra = '')
  39. {
  40. // Do we need to call Kohana::lang()?
  41. $text = (strpos($text, '.') === 0) ? Kohana::lang(self::$lang_key.$text) : $text;
  42.  
  43. if ( ! is_string($data))
  44. {
  45. isset($data['for']) AND $field_name = $data['for'];
  46. // Use the name if we have one
  47. isset($data['name']) AND $field_name = $data['name'];
  48. }
  49. else
  50. {
  51. $field_name = $data;
  52. }
  53.  
  54. // Display the errors
  55. if (isset($field_name) AND ! empty(self::$errors[$field_name]))
  56. $text = '<a name="'.$field_name.'">'.$text.'</a> - <span class="error">'.Kohana::lang(self::$lang_key.'.'.$field_name.'.'.self::$errors[$field_name]).'</span>';
  57. return parent::label($data, $text, $extra);
  58. }
  59.  
  60. /**
  61. * Creates an HTML form input tag. Defaults to a text type.
  62. *
  63. * @param string|array input name or an array of HTML attributes
  64. * @param string input value, when using a name
  65. * @param array array of error messages
  66. * @return string
  67. */
  68. public static function input($data, $value = NULL)
  69. {
  70. if ( ! is_array($data))
  71. {
  72. $data = array('name' => $data);
  73. }
  74.  
  75. // Set the automatic value if there is one
  76. $data['value'] = (isset(self::$form_data[$data['name']]) AND $value === NULL) ? self::$form_data[$data['name']] : $value;
  77.  
  78. // Default class
  79. ! isset($data['class']) AND $data['class'] = 'long';
  80.  
  81. isset(self::$errors[$data['name']]) AND $data['class'] = $data['class'].' error';
  82.  
  83. return parent::input($data, $value);
  84. }
  85.  
  86. public static function password($data, $value = '')
  87. {
  88. if ( ! is_array($data))
  89. {
  90. $data = array('name' => $data);
  91. }
  92.  
  93. $data['type'] = 'password';
  94.  
  95. return self::input($data, $value);
  96. }
  97.  
  98. }
Add Comment
Please, Sign In to add comment