示例#1
0
 /**
  * Wrapper to parent errors, set default translation file
  * @param   string  $file Translation File as set in application/messages/file
  * @param   bool    $translate
  * @return  array   messages
  */
 public function errors($file = NULL, $translate = TRUE)
 {
     if ($file === NULL) {
         $file = self::$defaultTranslation;
     }
     return parent::errors($file, $translate);
 }
示例#2
0
 /**
  * Class constructor
  *
  * @param array $array Array to validate
  */
 public function __construct(array $array)
 {
     parent::__construct($array);
     // Add labels
     $this->labels($this->_labels());
     // Add rules
     foreach ($this->_rules() as $field => $rules) {
         $this->rules($field, $rules);
     }
 }
 /**
  * Form::valid()
  * check if the form is
  * - submitted
  * - valid
  * 
  * then:
  * - get errors
  * - distribute errors over elements
  * 
  * @return boolean
  */
 public function valid()
 {
     $this->init();
     // set valid to false
     $valid = FALSE;
     // check if submitted
     if ($this->submitted()) {
         // get parsed valeus
         $values = $this->values();
         /**
          * collect values
          */
         // values to validate
         $validateValues = array();
         foreach ($this->_fields as $key => $field) {
             // set the whole field value (group or not)
             // to test rules set on form
             $validateValues[$key] = $values[$key];
             // set element values separately,
             // to test for rules set on element
             for ($i = 0; $i < count($field['elements']); $i++) {
                 $validateValues[$key . '__element__' . $i] = $field['elements'][$i]->value();
             }
         }
         /**
          * create validator
          */
         $validator = Kohana_Validation::factory($validateValues);
         /**
          * collect rules
          */
         foreach ($this->_fields as $key => $field) {
             // add rules to the whole field, group or not
             foreach ($field['rules'] as $rule) {
                 $validator->rule($key, $rule['rule'], $rule['params']);
             }
             // add element rules separately
             // for the separate values declared earlier
             for ($i = 0; $i < count($field['elements']); $i++) {
                 $rules = $field['elements'][$i]->rules();
                 foreach ($rules as $rule) {
                     $validator->rule($key . '__element__' . $i, $rule['rule'], $rule['params']);
                 }
             }
         }
         /**
          * validate
          */
         $valid = $validator->check();
         /**
          * distribute errors
          */
         if ($valid === FALSE) {
             $errors = $validator->errors();
             foreach ($errors as $key => $error) {
                 // key parts
                 $explode = explode('__', $key);
                 if (isset($explode[1]) && $explode[1] == 'element') {
                     // if it is an error on an element only
                     $this->_fields[$explode[0]]['elements'][$explode[2]]->error($error[0]);
                 } else {
                     // set field errors on the first element
                     $this->_fields[$key]['elements'][0]->error($error[0]);
                 }
             }
         }
     }
     return $valid;
 }
示例#4
0
 /**
  * Add validation rules to validation object
  *
  * @param  Validation  Validation object
  * @return array
  */
 protected function _check(Kohana_Validation $data)
 {
     foreach ($this->_fields as $name => $field) {
         if ($field['type'] === 'email') {
             $data->rule($name, 'email');
         }
         if (Arr::element($field, 'required')) {
             $data->rule($name, 'required');
         }
         if (Arr::element($field, 'unique')) {
             $data->rule($name, array($this, '_is_unique'), array(':validation', $name));
         }
         foreach (array('min_value', 'max_value', 'min_length', 'max_length') as $rule) {
             if (Arr::element($field, $rule)) {
                 $data->rule($name, $rule, array(':value', $field[$rule]));
             }
         }
         if (isset($field['rules']) && is_array($field['rules'])) {
             foreach ($field['rules'] as $rule => $val) {
                 $data->rule($name, $rule, array(':value', $val));
             }
         }
     }
     return $data;
 }
示例#5
0
 /**
  * Format error messages from a validation object according to Formo's
  * formatting rules
  *
  * @access public
  * @param Kohana_Validation $validation
  * @return mixed
  */
 public function validation_errors(Kohana_Validation $validation)
 {
     $validation->check();
     $errors = $validation->errors();
     if ($this->driver('is_a_parent')) {
         $array = array();
         foreach ($this->_fields as $field) {
             if ($msg = $field->_error_to_msg($errors)) {
                 $array[$field->alias()] = $field->_error_to_msg($errors);
             }
         }
         return $array;
     } else {
         return $this->_error_to_msg($errors);
     }
 }