Example #1
0
 public function validator(\Phalcon\Validation $validation, array $rules, array $urlParams)
 {
     $conut = 0;
     $this->composeCheckData();
     $newRule = array();
     if (isset($rules['url'])) {
         $this->setUrlParToData($rules['url'], $urlParams);
         $newRule[] = $rules['url'];
     }
     if (isset($rules['post']) && $this->method == 'POST') {
         $newRule[] = $rules['post'];
     }
     if (isset($rules['get']) && $this->method == 'GET') {
         $newRule[] = $rules['get'];
     }
     if (count($newRule) < 1) {
         return true;
     }
     foreach ($newRule as $value) {
         foreach ($value as $subKey => $subValue) {
             if (4 > count($subValue)) {
                 throw new \common\form\FormException("the rules config error about key:" . $subKey, 11003);
                 return;
             }
             $must = isset($subValue[4]) ? $subValue[4] : true;
             $isset = isset($this->formData[$subKey]);
             if (false == $must && (false == $isset || true == $isset && empty($this->formData[$subKey]))) {
                 continue;
             }
             $conut++;
             $validation->add($subKey, \core\RuleBase::setRules($subValue[0], $subValue[1], array($subValue[2], $subValue[3])));
             $validation->setFilters($subKey, 'trim');
         }
     }
     if ($conut) {
         return $validation->validate($this->formData);
     }
     return true;
 }
Example #2
0
 public function testValidationFiltering()
 {
     $this->specify("Validation filtering doesn't work as expected", function () {
         $validation = new Validation();
         $validation->setDI(new FactoryDefault());
         $validation->add('name', new Validation\Validator\PresenceOf(array('message' => 'The name is required')))->add('email', new Validation\Validator\PresenceOf(array('message' => 'The email is required')));
         $validation->setFilters('name', 'trim');
         $validation->setFilters('email', 'trim');
         $messages = $validation->validate(['name' => '  ', 'email' => '    ']);
         expect($messages)->count(2);
         $filtered = $messages->filter('email');
         $expectedMessages = array(0 => Message::__set_state(array('_type' => 'PresenceOf', '_message' => 'The email is required', '_field' => 'email', '_code' => '0')));
         expect($filtered)->equals($expectedMessages);
     });
 }
Example #3
0
 /**
  * Validates the form
  *
  * @param array|null $data
  * @param object|null $entity
  * @return boolean
  * @throws Exception
  */
 public function isValid($data = null, $entity = null)
 {
     if (is_array($data) === false && is_null($data) === false) {
         throw new Exception('Invalid parameter type.');
     }
     if (is_object($entity) === false && is_null($entity) === false) {
         throw new Exception('Invalid parameter type.');
     }
     if (is_array($this->_elements) === false) {
         return true;
     }
     //If the user doesn't pass an entity we use the one in this_ptr->_entity
     //@note the text does not match the === true but that's how it is
     if (is_object($entity) === true) {
         $this->bind($data, $entity);
     }
     //If the data is not an array use the one passed previously
     if (is_array($data) === false) {
         $data = $this->_data;
     }
     //Check if there is a method 'beforeValidation'
     if (method_exists($this, 'beforeValidation') === true) {
         if ($this->beforeValidation($data, $entity) === false) {
             return false;
         }
     }
     $notFailed = true;
     $messages = array();
     foreach ($this->_elements as $element) {
         $validators = $element->getValidators();
         if (is_array($validators) === true && empty($validators) === false) {
             $name = $element->getName();
             $preparedValidators = array();
             foreach ($validators as $validator) {
                 $preparedValidators[] = array($name, $validator);
             }
             //Create an implicit validator
             $validation = new Validation($preparedValidators);
             //Get filters in the element
             $filters = $element->getFilters();
             //Assign the filters to the validation
             if (is_array($filters) === true) {
                 $name = $element->getName();
                 $validation->setFilters($name, $filters);
             }
             //Perform the validation
             $elementMessages = $validation->validate($data, $entity);
             if (empty($elementMessages) === false) {
                 $name = $element->getName();
                 $messages[$name] = $elementMessages;
                 $notFailed = false;
             }
         }
     }
     //If the validation fails we update the messages
     if ($notFailed === false) {
         $this->_messages = $messages;
     }
     //Check if there is a method 'afterValidation'
     if (method_exists($this, 'afterValidation') === true) {
         $this->afterValidation($messages);
     }
     return $notFailed;
 }