/** * 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); }