/** * 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"); } }
public function testGetFormBlueprint() { $form = $this->standard_form_handler->getFormBlueprint(); $this->assertEquals($this->standard_form, $form); }
/** * Get a unique form name by combining the class name and the form name * * @param FormHandler $formHandler * @return mixed */ private function getFormName(FormHandler $formHandler) { $formName = $formHandler->getFormBlueprint()->getName(); return str_replace("\\", '', get_class($formHandler->getFormBlueprint()) . '::' . $formName); }