/**
  * 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();
 }
 protected function validate($scope = Validator::SCOPE_ALL, $options = null)
 {
     if (!$this->validationRun) {
         $form = $this->formHandler->getFormBlueprint();
         if (method_exists($form, 'getValidationRules')) {
             $form->getValidationRules($this->validator);
         }
         $this->doFieldValidationRules($form->getAll());
         $entities = $this->formHandler->saveToAndGetClonedEntities();
         if (!empty($entities)) {
             foreach ($entities as $entity) {
                 $this->validator->addSubValidation($entity['entity'], $entity['fields']);
             }
         }
         $this->validator->validate($this->formHandler->getData(), 'standard', $scope);
         $this->validationRun = true;
     } else {
         throw new \Exception("Can't validate twice, use existing validation result");
     }
 }
Beispiel #3
0
 public function testMergeData()
 {
     $this->basic_form_handler->setData('one', 1);
     $this->basic_form_handler->mergeData(['two' => 2]);
     $this->assertEquals(['one' => 1, 'two' => 2], $this->basic_form_handler->getData());
 }