Beispiel #1
0
 public function validate(CM_Form_Abstract $form)
 {
     if ($this->_extra_validation) {
         $values = array();
         foreach ($form->get_values() as $name => $value) {
             $values[$name] = $value->get_raw();
             $this->_extra_validation->label($name, $form->get_field($name)->get_label());
         }
         // Validation только read-only, поэтому создаем новый объект
         $this->_extra_validation = $this->_extra_validation->copy($values);
     }
     try {
         $this->get_model()->check($this->_extra_validation);
     } catch (ORM_Validation_Exception $e) {
         $errors = $e->errors('validation');
         if ($external = arr::get($errors, '_external')) {
             $errors = arr::merge($errors, $external);
             unset($errors['_external']);
         }
         foreach ($errors as $name => $error) {
             $form->get_field($name)->set_error($error);
         }
         return FALSE;
     }
     return TRUE;
 }
Beispiel #2
0
 public function validate(CM_Form_Abstract $form)
 {
     $values = array();
     foreach ($form->get_values() as $name => $value) {
         $values[$name] = $value->get_raw();
         $this->_validation->label($name, $form->get_field($name)->get_label());
     }
     // Validation только read-only, поэтому создаем новый объект
     $this->_validation = $this->_validation->copy($values);
     if ($this->_validation->check()) {
         return TRUE;
     }
     foreach ($this->_validation->errors('validation') as $name => $error) {
         $form->set_error($name, $error);
     }
     return FALSE;
 }
Beispiel #3
0
 /**
  * Initializes validation rules, and labels
  *
  * @return void
  */
 protected function _validation()
 {
     // Build the validation object with its rules
     $this->_validation = Validation::factory($this->_object)->bind(':model', $this)->bind(':original_values', $this->_original_values)->bind(':changed', $this->_changed);
     foreach ($this->rules() as $field => $rules) {
         $this->_validation->rules($field, $rules);
     }
     // Use column names by default for labels
     $columns = array_keys($this->_table_columns);
     // Merge user-defined labels
     $labels = array_merge(array_combine($columns, $columns), $this->labels());
     foreach ($labels as $field => $label) {
         $this->_validation->label($field, $label);
     }
 }
Beispiel #4
0
 /**
  * Add rules to the validation object
  *
  * @access protected
  * @param mixed Formo_Container $field. (default: NULL)
  * @return void
  */
 protected function _add_rules(Validation $validation)
 {
     if (!($rules = $this->get('rules'))) {
         // Only do anything if the field has rules
         return;
     }
     $validation->label($this->alias(), $this->view()->label());
     $validation->rules($this->alias(), $rules);
 }
 /**
  * Tests Validation::check()
  *
  * @test
  * @covers Validation::check
  * @covers Validation::rule
  * @covers Validation::rules
  * @covers Validation::errors
  * @covers Validation::error
  * @dataProvider provider_check
  * @param array   $array            The array of data
  * @param array   $rules            The array of rules
  * @param array   $labels           The array of labels
  * @param boolean $expected         Is it valid?
  * @param boolean $expected_errors  Array of expected errors
  */
 public function test_check($array, $rules, $labels, $expected, $expected_errors)
 {
     $validation = new Validation($array);
     foreach ($labels as $field => $label) {
         $validation->label($field, $label);
     }
     foreach ($rules as $field => $field_rules) {
         foreach ($field_rules as $rule) {
             $validation->rule($field, $rule[0], $rule[1]);
         }
     }
     $status = $validation->check();
     $errors = $validation->errors(TRUE);
     $this->assertSame($expected, $status);
     $this->assertSame($expected_errors, $errors);
     $validation = new validation($array);
     foreach ($rules as $field => $rules) {
         $validation->rules($field, $rules);
     }
     $validation->labels($labels);
     $this->assertSame($expected, $validation->check());
 }
Beispiel #6
0
 /**
  * Validate the data, returning array of errors (if any).
  *
  * @return array
  */
 public function validate()
 {
     $validation = new Validation($this->_data);
     foreach ($this->_fields as $fieldName => $fieldOptions) {
         foreach (Arr::get($fieldOptions, 'rules', array()) as $fieldRule) {
             if (is_array($fieldRule)) {
                 // Take the 1st param as the name, the rest as params.
                 $ruleName = array_shift($fieldRule);
                 $ruleParams = $fieldRule;
             } else {
                 // Just use the rule as passed with no params.
                 $ruleName = $fieldRule;
                 $ruleParams = NULL;
             }
             // Check if the named function is actually a method on this object.
             if (!function_exists($ruleName) && method_exists($this, $ruleName)) {
                 $ruleName = array($this, $ruleName);
             }
             $validation->rule($fieldName, $ruleName, $ruleParams);
             $validation->label($fieldName, Arr::get($fieldOptions, 'label', $fieldName));
         }
     }
     if ($validation->check()) {
         $this->_errors = array();
         return TRUE;
     } else {
         if (!(bool) ($errorMessageFile = $this->_errorMessageFile)) {
             // Konform_Contact => 'konform/contact'
             $errorMessageFile = strtolower(str_replace('_', '/', get_class($this)));
         }
         $this->_errors = $validation->errors($errorMessageFile);
         return FALSE;
     }
 }
Beispiel #7
0
 /**
  * Add validation rules to validation object
  *
  * @param  Validation  Validation object
  * @return array
  */
 protected function _check(Validation $data)
 {
     foreach ($this->_fields as $name => $field) {
         if ($field['type'] === 'email') {
             $data->rule($name, 'email');
         }
         if (Arr::get($field, 'required')) {
             $data->rule($name, 'required');
         }
         if (Arr::get($field, 'unique')) {
             $data->rule($name, array(':model', '_is_unique'), array(':validation', $name));
         }
         foreach (array('min_value', 'max_value', 'min_length', 'max_length') as $rule) {
             if (Arr::get($field, $rule) !== NULL) {
                 $data->rule($name, $rule, array(':value', $field[$rule]));
             }
         }
         if (isset($field['rules'])) {
             $data->rules($name, $field['rules']);
         }
         if (isset($field['label'])) {
             $data->label($name, $field['label']);
         }
     }
     return $data;
 }
Beispiel #8
0
 /**
  * Правила валидации значения поля документа.
  * При сохранении документа происходит валидация значений его полей. 
  * Каждое поле прогоняется в цикле и происходит вызов этого метода.
  * 
  * @see DataSource_Hybrid_Document::validate()
  * 
  * @param Validation $validation
  * @param DataSource_Hybrid_Document
  * @return \Validation
  */
 public function onValidateDocument(Validation $validation, DataSource_Hybrid_Document $doc)
 {
     if ($this->isreq === TRUE and $this->is_required()) {
         $validation->rule($this->name, 'not_empty');
     }
     if ($this->unique === TRUE) {
         $validation->rule($this->name, array($this, 'check_unique'), array(':value', $doc));
     }
     return $validation->label($this->name, $this->header);
 }
Beispiel #9
0
 /**
  * 
  * @param Validation $validation
  * @param DataSource_Hybrid_Document $doc
  * @return Validation
  */
 public function onValidateDocument(Validation $validation, DataSource_Hybrid_Document $doc)
 {
     $image = $doc->get($this->name);
     $url = $doc->get($this->name . '_url');
     $from_url = FALSE;
     if (Valid::url($url)) {
         $from_url = TRUE;
     }
     if ($this->isreq === TRUE and $from_url === FALSE) {
         if (is_array($image)) {
             $validation->rules($this->name, array(array('Upload::not_empty')));
         } else {
             $validation->rules($this->name, array(array('not_empty')));
         }
     } elseif ($this->isreq === TRUE and $from_url === TRUE) {
         $validation->rules($this->name . '_url', array(array('not_empty')));
     }
     if ($from_url === FALSE and is_array($image)) {
         $validation->rules($this->name, array(array('Upload::valid'), array('Upload::type', array(':value', $this->types)), array('Upload::size', array(':value', $this->max_size))));
     } else {
         if ($from_url === TRUE) {
             $ext = strtolower(pathinfo($url, PATHINFO_EXTENSION));
             $validation->rules($this->name . '_url', array(array('url'), array('in_array', array($ext, $this->types))));
         }
     }
     return $validation->label($this->name . '_url', $this->header)->label($this->name, $this->header);
 }