Fuel\Validation\Validator::addRule PHP Method

addRule() public method

Adds a rule that can be used to validate a field
Since: 2.0
public addRule ( string | Fuel\Validation\FieldInterface $field, Fuel\Validation\RuleInterface $rule )
$field string | Fuel\Validation\FieldInterface
$rule Fuel\Validation\RuleInterface
    public function addRule($field, RuleInterface $rule)
    {
        if (is_string($field)) {
            try {
                $field = $this->getField($field);
            } catch (InvalidFieldException $ife) {
                // The field does not exist so create it
                $this->addField($field);
                $field = $this->getField($field);
            }
        }
        // We have a valid field now so add the rule
        $field->addRule($rule);
        $this->lastAddedRule = $rule;
        return $this;
    }

Usage Example

Example #1
0
 /**
  * Adds an individual rule for the given field to the given validator.
  * If the $ruleName is numeric the function will assume that $params is the rule name and that there are no
  * parameters.
  *
  * @param string     $fieldName
  * @param string|int $ruleName
  * @param mixed      $params
  * @param Validator  $validator
  *
  * @since 2.0
  */
 protected function addFieldRule($fieldName, $ruleName, $params, Validator $validator)
 {
     // If $ruleName is numeric assume that $params is the name and there are no actual parameters
     if (is_numeric($ruleName)) {
         $ruleName = $params;
         $params = [];
     }
     // Create and add the rule
     $ruleInstance = $validator->createRuleInstance($ruleName, $params);
     $validator->addRule($fieldName, $ruleInstance);
 }
All Usage Examples Of Fuel\Validation\Validator::addRule