0

Which is considered best practice? What are the benefits to each? To create two separate Zend\Form\Form objects - one for inserting and one for updating? Or to have a single Zend\Form\Form object and using different validation when Inserting to allow for an empty identification field.

I'm new to ZF2 and ran into this situation and was wondering which method to use. The forms are identical except when updating I have a field which identifies the PK in my table to determine which record to update. When I used the same form for insertion the validation would fail because that field was empty.

I've saw this question which suggests a single validation rule can be created which would return true on empty/null or if a value is provided would return true if it met the rest of the conditions. But this was really all I could find on SO or Google on the specific subject.

Empty values passed to Zend framework 2 validators

Community
  • 1
  • 1
d.lanza38
  • 2,525
  • 7
  • 30
  • 52
  • 1
    You can have one fieldset/form that can be reused by having have two factories. In the 'insert' factory you could use the [`setValidationGroup()` option](http://framework.zend.com/manual/current/en/modules/zend.input-filter.intro.html). – AlexP Apr 14 '16 at 15:33
  • Interesting, it basically turns off the validation for the field. I started implementing a validation chain but was going to have to write my own validation classes. This seems like a better approach. – d.lanza38 Apr 14 '16 at 18:23

0 Answers0