1

In a project I have a huge form -- with many fields, nested fieldsets, collections etc. The form validation is failing and I don't know, why.

How should such a problem be handled? What is a good, efficient approach to find out, why a form validation is failing?

automatix
  • 14,018
  • 26
  • 105
  • 230
  • 1
    after `isValid` you can fetch the errors using `$form->getMessages()`. – AlexP Apr 12 '16 at 14:44
  • you can also use a debugger like xdebug – Unex Apr 12 '16 at 15:02
  • @AlexP I'm not sure, that it always helps. E.g. in my case: I've debugged the [`Zend\Form#isValid()`](https://github.com/zendframework/zend-form/blob/master/src/Form.php#L523) class at the place, when the validation is completed and was expecting the messages. But `Order\Form\OrderForm#messages` was empty. The `$result` is `false` and the `$messages` is empty. – automatix Apr 12 '16 at 15:38
  • @Unex Yes, of course, I'm debugging with Xdebug. The question was more about, what to looking for, in order to make the debugging fast. – automatix Apr 12 '16 at 15:41
  • @automatix Provided you have called it after `isValid()`, you should always get an error. If you're debugging, I would start with the form's input filter. The input filter (and the validator chain for each element) is what determines the error messages. – AlexP Apr 12 '16 at 21:47
  • Dumb question but have you prepared your form ? – Greco Jonathan Apr 13 '16 at 10:49
  • @Hooli You mean [`$form->prepare()`](https://github.com/zendframework/zend-form/blob/master/src/Form.php#L197)? Yes, I have. – automatix May 26 '16 at 15:57

1 Answers1

0

I finally found a way to detect the validation error(-s) without to debug every (sub-sub-sub-)fieldset and every (sub-sub-sub-)element. It's simple $form->getMessages(). Or alternatively $this->FormElementErrors($form) in the view script.

Did I know about this methods before? Sure, I did. But I was unconcerned about the manner, how it works, and the property messages led me astray:

When I got validation issues, I set a breakpoint somewhere after the Zend\Form#isValid() call and saw then in Xdebug, that Zend\Form#messages property was empty. And I was surprised about it, because a property "messages" suggests, that it will contain (validation error) messages. This logic applies to in Elements -- and a Form actually is an Element (since Form extends Fieldset extends Element).

automatix
  • 14,018
  • 26
  • 105
  • 230