Esempio n. 1
0
 public function validate($value, \ICanBoogie\Errors $errors)
 {
     $from = $this[self::FROM];
     $to = $this[self::TO];
     if ($from < $to ? $value < $from || $value > $to : $value > $from || $value < $to) {
         $errors[$this['name']] = new FormattedString("The value should be between :from and :to, %value given.", array('from' => $from, 'to' => $to, 'value' => $value));
     }
     return parent::validate($value, $errors);
 }
Esempio n. 2
0
 /**
  * Validates the form using the provided values.
  *
  * @inheritdoc
  */
 public function validate($values, Errors $errors)
 {
     #
     # validation without prior save
     #
     if (empty($values[self::STORED_KEY_NAME])) {
         $this->__sleep();
     }
     #
     # we flatten the array so that we can easily get values
     # for keys such as `cars[1][color]`
     #
     $values = array_flatten($values);
     $this->values = $values;
     #
     # process required values
     #
     $validators = $this->validators;
     foreach ($validators as $identifier => $element) {
         $element->form = $this;
         $element->name = $identifier;
         $element->label = self::select_element_label($element);
     }
     #
     # process required elements
     #
     $this->validate_required_elements($this->required, $validators, $values, $errors);
     #
     # process elements validators
     #
     # note: If the value for the element is `null` and the value is not required the element's
     # validator is *not* called.
     #
     foreach ($validators as $name => $element) {
         $value = isset($values[$name]) ? $values[$name] : null;
         if (($value === null || $value === '') && empty($this->required[$name])) {
             continue;
         }
         $element->validate($value, $errors);
     }
     // FIXME-20111013: ICanBoogie won't save the errors in the session, so we have to do it ourselves for now.
     store_form_errors($this->name, $errors);
     if (count($errors)) {
         return false;
     }
     return parent::validate($values, $errors);
 }