/** * @param Form $form * @return bool */ public function validateForm(Form $form) { if ($form != $this->form) { throw new ValidationException("Cannot validate different form with the same validator."); } return $this->validator->passes(); }
/** * Validate the form * * @param null $scope * @param null $options * @return mixed * @throws \Exception */ private function validate() { if (isset($this->validator)) { throw new \Exception('Cannot validate the same form twice, use the existing result'); } $fieldRules = $this->getFieldValidationRules($this->formHandler->getFormBlueprint()->getAll()); $validationRules = $fieldRules['rules']; $validationAttributeNames = $fieldRules['names']; // Get validation rules from any assigned entities (models) foreach ($this->formHandler->getEntities() as $entity) { if (is_callable([$entity['entity'], 'getValidationRules'])) { try { $entityRules = $entity['entity']->getValidationRules(); } catch (\BadMethodCallException $e) { $entityRules = []; } foreach ($entityRules as $field => $entityRule) { // If we already have rules for that parameter, concatenate them if (isset($validationRules[$field])) { $validationRules[$field] .= '|' . $entityRule; } else { $validationRules[$field] = $entityRule; } } } } $this->validator = \Validator::make($this->formHandler->getData(), $validationRules); $this->validator->setAttributeNames($validationAttributeNames); $this->isValid = $this->validator->passes(); }
/** * Test Valid Data * @return bool */ public function isValid() { $vRules = $this->getValidationRules(); $rules = $vRules->getRules(); $messages = $vRules->getMessages(); $currentData = $this->export(); $this->validator = Validator::make($currentData, $rules, $messages); return $this->validator->passes(); }
/** * Returns whether the input data is valid. * * @throws NoValidationRulesFoundException * @return bool */ public function isValid() { $this->beforeValidation(); if (!isset($this->rules)) { throw new NoValidationRulesFoundException('no validation rules found in class ' . get_called_class()); } $this->validator = Validator::make($this->getInputData(), $this->getPreparedRules(), $this->getMessages()); $this->afterValidation(); return $this->validator->passes(); }
public function validate() { $this->initValidator(); if (!$this->validator->passes()) { throw new ProcessorException(ProcessorException::INVALID_PARAM, $this->error()); } $this->validateAmount(); $this->validateDestination(); $this->validateTime(); return true; }
/** * Validates the model. * * @param array $customRules * @param array $customMessages * @param array $attributeNames * * @return bool */ protected function runValidation(array $customRules = [], array $customMessages = [], array $attributeNames = []) { $rules = empty($customRules) ? $this->getRules() : $customRules; $messages = empty($customMessages) ? $this->getCustomMessages() : $customMessages; $attributeNames = empty($attributeNames) ? $this->getAttributeNames() : $attributeNames; $attributes = $this->prepareAttributes(); $this->validator = $this->makeValidator($attributes, $rules, $messages); $this->validator->setAttributeNames($attributeNames); $success = $this->validator->passes(); if (!$success) { $this->setErrors($this->validator->errors()); } return $success; }
/** * Determine if the data passes the validation rules. * * @return bool */ public function passes() { if ($this->isRemoteValidationRequest()) { return $this->validateJsRemoteRequest($this->data['_jsvalidation'], [$this, 'parent::passes']); } return parent::passes(); }
/** * Validate input data. * * @param $input * * @return bool */ protected function validate($input) { $data = $rules = []; foreach ($this->builder->fields() as $field) { if (!method_exists($field, 'rules') || !($rule = $field->rules())) { continue; } $columns = $field->column(); if (is_string($columns)) { if (!array_key_exists($columns, $input)) { continue; } $data[$field->label()] = array_get($input, $columns); $rules[$field->label()] = $rule; } if (is_array($columns)) { foreach ($columns as $key => $column) { if (!array_key_exists($column, $input)) { continue; } $data[$field->label() . $key] = array_get($input, $column); $rules[$field->label() . $key] = $rule; } } } $this->validator = Validator::make($data, $rules); return $this->validator->passes(); }
function it_returns_messages_on_failed_validation(Validator $validator, MessageBag $bag) { $validator->setData($data = [])->shouldBeCalled(); $validator->passes()->willReturn(false); $validator->messages()->willReturn($bag); $bag->all()->willReturn($messages = ['foo' => 'failed']); $this->validate($data)->shouldReturn(false); $this->getMessages()->shouldReturn($messages); }
/** * Returns whether the current state of the validator is valid. * * @param bool $partial Whether a partial validation is okay. * @return bool */ public function valid($partial = false) { if ($partial) { $this->useFields(array_keys($this->values)); } // Generate our implementation from the app. $this->validator = self::$factory->make($this->values, $this->rules); if ($this->validator->passes()) { return true; } $this->errors = $this->validator->messages(); return false; }
/** * Validate the values * * @param array $values * * @return bool */ public function validate(array $values) { $values = $this->snakeArrayKeys($values); $this->validator = $this->make($values, $this->getRules()); // Make sure any forbidden keys are reported foreach ($this->forbiddenKeys as $key) { if (array_key_exists($key, $values)) { $this->addErrorMessage($key, sprintf('%s should not be set', $key)); return false; } } $passes = $this->validator->passes(); // Validate the sub rules foreach ($this->getSubValidators() as $key => $validator) { // Skip missing data, if it were required the require rule would // kick in if (!isset($values[$key])) { continue; } $subValues = $values[$key]; if (count($subValues)) { // If the 0 index is set it means we have multiple sub // objects to validate if (isset($subValues[0])) { foreach ($subValues as $subValueSet) { /** @var Validator $validator */ $validator = new $validator(); $passes = $passes && $this->subValidate($validator, $subValueSet, $key); } } else { /** @var Validator $validator */ $validator = new $validator(); $passes = $passes && $this->subValidate($validator, $subValues, $key); } } } return $passes; }
/** * Validate the model instance * * @param array $rules Validation rules * @param array $customMessages Custom error messages * @param array $customAttributes Custom attributes * @return bool * @throws InvalidModelException */ public function validate(array $rules = array(), array $customMessages = array(), array $customAttributes = array()) { if ($this->fireModelEvent('validating') === false) { if ($this->throwOnValidation) { throw new InvalidModelException($this); } else { return false; } } // check for overrides, then remove any empty rules $rules = empty($rules) ? static::$rules : $rules; foreach ($rules as $field => $rls) { if ($rls == '') { unset($rules[$field]); } } if (empty($rules)) { $success = true; } else { $customMessages = empty($customMessages) ? static::$customMessages : $customMessages; $customAttributes = empty($customAttributes) ? static::$customAttributes : $customAttributes; if ($this->forceEntityHydrationFromInput || empty($this->attributes) && $this->autoHydrateEntityFromInput) { $this->fill(Input::all()); } $data = $this->getAttributes(); // the data under validation // perform validation $this->validator = static::makeValidator($data, $rules, $customMessages, $customAttributes); $success = $this->validator->passes(); if ($success) { // if the model is valid, unset old errors if ($this->validationErrors === null || $this->validationErrors->count() > 0) { $this->validationErrors = new MessageBag(); } } else { // otherwise set the new ones $this->validationErrors = $this->validator->messages(); // stash the input to the current session if (!self::$externalValidator && Input::hasSession()) { Input::flash(); } } } $this->fireModelEvent('validated', false); if (!$success && $this->throwOnValidation) { throw new InvalidModelException($this); } return $success; }
/** * Set the response based on validation. * * @param Validator $validator * @param FormBuilder $builder */ protected function setResponse(Validator $validator, FormBuilder $builder) { if (!$validator->passes()) { $builder->setSave(false)->setFormErrors($validator->getMessageBag()); $this->dispatch(new SetErrorMessages($builder)); } $this->dispatch(new RepopulateFields($builder)); }
/** * Determine if the data passes the validation rules. * * @return bool */ public function passes() { return $this->validator->passes(); }
public function testCustomValidators() { $trans = $this->getRealTranslator(); $trans->addResource('array', array('validation.foo' => 'foo!'), 'en', 'messages'); $v = new Validator($trans, array('name' => 'taylor'), array('name' => 'Foo')); $v->addExtension('Foo', function () { return false; }); $this->assertFalse($v->passes()); $v->getMessages()->setFormat(':message'); $this->assertEquals('foo!', $v->getMessages()->first('name')); }
/** * Returns whether the input data is valid. * * @return bool */ public function isValid() { $this->validator = Validator::make($this->getInputData(), $this->getPreparedRules(), $this->getMessages()); return $this->validator->passes(); }
/** * Valida los datos antes de grabar la categoria * * @param $input * @return bool */ private function validate($input) { $this->validation = Validator::make($input, $this->rules); return $this->validation->passes(); }
/** * @param array $testData * @param array $rules * * @return bool */ public function passes(array $testData, array $rules) : bool { $this->validation = $this->factory->make($testData, $rules); return $this->validation->passes(); }
public function isValid() { return $this->validator ? $this->validator->passes() : true; }
/** * Set the response based on validation. * * @param Validator $validator * @param FormBuilder $builder */ protected function setResponse(Validator $validator, FormBuilder $builder) { if (!$validator->passes()) { $builder->setSave(false); $bag = $validator->getMessageBag(); foreach ($bag->getMessages() as $field => $messages) { foreach ($messages as $message) { $builder->addFormError($field, $message); } } $this->dispatch(new SetErrorMessages($builder)); } $this->dispatch(new RepopulateFields($builder)); }