Sirius\Validation\RuleFactory::createRule PHP Method

createRule() public method

Factory method to construct a validator based on options that are used most of the times
public createRule ( string | callable $name, string | array $options = null, string $messageTemplate = null, string $label = null ) : Sirius\Validation\Rule\AbstractValidator
$name string | callable name of a validator class or a callable object/function
$options string | array validator options (an array, JSON string or QUERY string)
$messageTemplate string error message template
$label string label of the form input field or model attribute
return Sirius\Validation\Rule\AbstractValidator
    public function createRule($name, $options = null, $messageTemplate = null, $label = null)
    {
        $validator = $this->construcRuleByNameAndOptions($name, $options);
        // no message template, try to get it from the registry
        if (!$messageTemplate) {
            $messageTemplate = $this->getSuggestedMessageTemplate($name, !!$label);
        }
        if (is_string($messageTemplate) && $messageTemplate !== '') {
            $validator->setMessageTemplate($messageTemplate);
        }
        if (is_string($label) && $label !== '') {
            $validator->setOption('label', $label);
        }
        return $validator;
    }

Usage Example

Example #1
0
 /**
  * Remove validation rule
  *
  * @param mixed $name
  *            rule name or true if all rules should be deleted for that selector
  * @param mixed $options
  *            rule options, necessary for rules that depend on params for their ID
  *
  * @throws \InvalidArgumentException
  * @internal param string $selector data selector
  * @return self
  */
 public function remove($name = true, $options = null)
 {
     if ($name === true) {
         $this->rules = new RuleCollection();
         return $this;
     }
     $validator = $this->ruleFactory->createRule($name, $options);
     $this->rules->detach($validator);
     return $this;
 }