/** * Validates the form data using the provided input filter. * * If validation errors are found, the form is populated with the corresponding error messages. * Form values are updated with the clean values provided by the filter. * * @param Form $form * @return boolean */ public function validate(Form $form) { $this->inputFilter->setData($form->values()); if (!($isValid = $this->inputFilter->isValid())) { $form->setErrorMessages($this->inputFilter->getMessages()); return $isValid; } $form->submit($this->inputFilter->getValues()); return $isValid; }
function it_should_add_messages_to_form_when_validation_fails(Form $form, InputFilter $filter) { $submittedValues = ['username' => 'john.doe', 'password' => '']; $errors = ['password' => 'Password cannot be empty']; $this->beConstructedWith($filter); $filter->setData($submittedValues)->shouldBeCalled(); $filter->isValid()->willReturn(false); $filter->getMessages()->willReturn($errors); $form->values()->willReturn($submittedValues); $form->setErrorMessages($errors)->shouldBeCalled(); $this->validate($form); }
function it_should_render_the_opening_tag_of_a_multipart_form(FormTheme $theme, Template $template) { $form = new Form(); $form->add(new File('avatar')); $formView = $form->buildView(); $theme->loadTemplateFor('form_start')->willReturn($template); $theme->blocks()->willReturn([]); $this->renderFormStart($formView); $template->displayBlock('form_start', ['attr' => $formView->attributes + ['enctype' => 'multipart/form-data']], [])->shouldHaveBeenCalled(); }
/** @test */ public function it_should_render_all_the_form_elements_that_were_not_already_rendered_individually() { $form = new Form(); $form->add(new Text('username'))->add(new Hidden('user_id'))->add(new Hidden('user_role_id')); $form->submit(['user_id' => 1, 'user_role_id' => 2]); $view = $form->buildView(); $this->renderer->renderRow($view->username, ['label' => 'Username']); $html = $this->renderer->renderRest($view); $this->assertEquals('<input type="hidden" name="user_id" value="1"><input type="hidden" name="user_role_id" value="2">', $html); }