/** * Create an instance of given validator. * * @param string $validatorName * @param array $arguments * * @return ValidatorInterface * * @throws ClassNotFoundException * @throws InvalidValidatorException */ public function createInstance($validatorName, $arguments) { $validatorClassName = $this->getFullClassName($validatorName); if (!$this->assertIfClassExists($validatorClassName)) { throw new ClassNotFoundException(sprintf($this->translator->get('class_not_found'), $validatorName)); } $instance = $this->createValidatorInstance($validatorClassName, $arguments); if (!$instance instanceof ValidatorInterface) { throw new InvalidValidatorException(sprintf($this->translator->get('invalid_validator'), $validatorName)); } $instance->setMessages($this->translator->get($validatorName)); return $instance; }
/** * Run validators list for a given attribute. * * @param string $attribute * @param array $rules * * @throws \Sparta\Exceptions\InvalidValidatorException * @throws \Sparta\Exceptions\MethodNotFoundException */ public function runAttributeValidators($attribute, $rules) { // Process list of validators for the given attribute foreach ($rules as $validator) { // Does the validator have arguments? if yes, get them $arguments = $validator->hasArguments() ? $validator->getArguments() : []; try { $validatorInstance = $this->validatorFactory->createInstance($validator->getName(), $arguments); } catch (ClassNotFoundException $e) { throw new InvalidValidatorException(sprintf($this->translator->get('invalid_validator'), $validator->getName())); } catch (InvalidValidatorException $e) { throw new InvalidValidatorException(sprintf($this->translator->get('invalid_validator'), $validator->getName())); } if (!call_user_func([$validatorInstance, 'isValid'], $this->field($attribute))) { $this->collectAttributeErrors($attribute, $validatorInstance->errors()); $this->failedValidation = true; } } }
/** * Ensure that we can set a default message value to be returned when nothing * found in the loaded list * * @return void */ public function testGetDefaultMessageIfNoMatchingValueFoundInMessagesList() { $this->translator->load(); $this->assertEquals('default_value', $this->translator->get('invalid_message_key', 'default_value')); }