Fuel\Validation\Validator::createRuleInstance PHP Method

createRuleInstance() public method

Creates an instance of the given rule name
Since: 2.0
public createRuleInstance ( string $name, mixed $parameters = [] ) : Fuel\Validation\RuleInterface
$name string
$parameters mixed
return Fuel\Validation\RuleInterface
    public function createRuleInstance($name, $parameters = [])
    {
        $className = $this->getRuleClassName($name);
        if (!class_exists($className)) {
            throw new InvalidRuleException($name);
        }
        /* @var RuleInterface $instance */
        $reflection = new \ReflectionClass($className);
        $instance = $reflection->newInstanceArgs($parameters);
        // Check if there is a custom message
        $message = $this->getGlobalMessage($name);
        if ($message !== null) {
            $instance->setMessage($message);
        }
        return $instance;
    }

Usage Example

Example #1
0
 public function testCreateRuleInstanceWithGlobalMessage()
 {
     $message = 'Test message';
     $ruleName = 'test';
     $class = '\\DummyAbstractRule';
     $this->object->setGlobalMessage($ruleName, $message);
     $this->object->addCustomRule($ruleName, $class);
     $instance = $this->object->createRuleInstance($ruleName);
     $this->assertEquals($message, $instance->getMessage());
 }
All Usage Examples Of Fuel\Validation\Validator::createRuleInstance