/** * 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(); }
/** * @param $formHandler * @param $request null Unused, global vars used to get data * @return array * @throws \Exception */ public function handleRequest(FormHandler $formHandler, $request) { if ($formHandler->getMethod() == "GET") { return $_GET; } else { if ($formHandler->hasFieldOfType('file')) { return array_merge_recursive($_POST, $_FILES); } else { return $_POST; } } }
/** * @param $formHandler * @param $request \Symfony\Component\HttpFoundation\Request * @return array * @throws \Exception */ public function handleRequest(FormHandler $formHandler, $request) { if (!is_a($request, 'Symfony\\Component\\HttpFoundation\\Request')) { throw new \Exception("The form is set to use Symfony request objects but was not given one in the handleRequest method"); } if ($formHandler->getMethod() == 'POST') { $paramBag = 'request'; } else { $paramBag = 'query'; } $params = $request->{$paramBag}->all(); if ($formHandler->hasFieldOfType('file')) { $files = $request->files->all(); $params = array_merge_recursive($params, $files); } return $params; }
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 buildForm(FormBlueprintInterface $form, FormViewInterface $formView = null, ValidatorExtensionInterface $validator = null) { if ($this->eventDispatcher !== null) { $event = new FilterNewFormEvent($form, $formView, $validator); $this->eventDispatcher->dispatch('form_factory.create', $event); $form = $event->getFormBlueprint(); $formView = $event->getFormView(); $validator = $event->getValidator(); } $formHandler = new FormHandler($form, $this->requestHandler, $this->entityProcessor, $this->typeHandler, $this->eventDispatcher, $this->restoreDataHandler); if ($validator) { $formHandler->setValidator($validator); } if (!$formView) { $formView = new FormView(); } $formHandler->setFormView($formView); $formHandler->setTransformerManager($this->transformerManager); return $formHandler; }
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()); }
/** * 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); }