/**
  * 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();
 }
Exemplo n.º 2
0
 /**
  * @param array $formData
  *
  * @throws FormValidationException
  */
 public function validate(array $formData)
 {
     // Instantiate validator instance by factory
     $this->validation = $this->validator->make($formData, $this->rules());
     $this->validation->setAttributeNames($this->setAttributeNames());
     // Validate
     if ($this->validation->fails()) {
         throw new FormValidationException('Validation Failed', $this->getValidationErrors());
     }
     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;
 }