Sirius\Validation\Validator::add PHP Method

add() public method

public add ( string $selector, string | callback $name = null, string | array $options = null, string $messageTemplate = null, string $label = null ) : Validator
$selector string
$name string | callback
$options string | array
$messageTemplate string
$label string
return Validator
    public function add($selector, $name = null, $options = null, $messageTemplate = null, $label = null)
    {
        // the $selector is an associative array with $selector => $rules
        if (func_num_args() == 1) {
            if (!is_array($selector)) {
                throw new \InvalidArgumentException('If $selector is the only argument it must be an array');
            }
            return $this->addMultiple($selector);
        }
        // check if the selector is in the form of 'selector:Label'
        if (strpos($selector, ':') !== false) {
            list($selector, $label) = explode(':', $selector, 2);
        }
        $this->ensureSelectorRulesExist($selector, $label);
        call_user_func(array($this->rules[$selector], 'add'), $name, $options, $messageTemplate, $label);
        return $this;
    }

Usage Example

Ejemplo n.º 1
0
 public function validate($rules = null)
 {
     $validator = new Validator();
     foreach ($this->rules as $rule) {
         $fields = explode(',', str_replace(' ', '', $rule[0]));
         foreach ($fields as $field) {
             count($rule) == 4 ? $validator->add($field, $rule[1], $rule[2], \I18n::translate($rule[3]), $this->label($field)) : $validator->add($field, $rule[1], [], \I18n::translate($rule[2]), $this->label($field));
         }
     }
     if (!$validator->validate($this->getAttributes())) {
         $messages = $validator->getMessages();
         foreach ($messages as $message) {
             foreach ($message as $error) {
                 $this->errors[] = (string) $error;
             }
         }
         return false;
     }
     return true;
 }
All Usage Examples Of Sirius\Validation\Validator::add