/** * 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(); }
public function testMultipleEntityAssignment() { $entity_one = new StandardFormEntity(); $entity_one->setName('Name One'); $entity_one->setDescription('Description One'); $entity_one->setCategory('5'); $entity_one->setPassword('Secure One'); $entity_two = new StandardFormEntity(); $entity_two->setName('Name Two'); $entity_two->setDescription('Description Two'); $entity_two->setCategory('1'); $entity_two->setPassword('Secure Two'); $this->standard_form_handler->bindEntity($entity_one, array('name', 'description')); $this->standard_form_handler->bindEntity($entity_two, array('name', 'password', 'category')); $this->assertEquals(2, count($this->standard_form_handler->getEntities())); $name_field = $this->standard_form_handler->getField('name'); $this->assertEquals('Name Two', $name_field['value']); $description_field = $this->standard_form_handler->getField('description'); $this->assertEquals('Description One', $description_field['value']); $_POST = $this->standard_form_request; $this->standard_form_handler->handleRequest(); $this->standard_form_handler->saveToEntities(); $this->assertEquals('Example Name', $entity_one->getName()); $this->assertEquals('Example Description', $entity_one->getDescription()); $this->assertEquals('5', $entity_one->getCategory()); $this->assertEquals('Secure One', $entity_one->getPassword()); $this->assertEquals('Example Name', $entity_two->getName()); $this->assertEquals('Description Two', $entity_two->getDescription()); $this->assertEquals('3', $entity_two->getCategory()); $this->assertEquals('', $entity_two->getPassword()); }