Zend Framework 2 - Validating a form from multiple sets of input filters?

My ZF2 uses a simple structure that I have seen throughout, with input filters installed in the models and applied to the form during validation. But I really hit the wall here. How do you handle validation for a form that has some fields that need to be checked against one model, and some fields that need to be checked against another?

My scenario: I use one form to insert 2 records in 2 tables of my database (adding a user and adding a donation related to this user). Some fields will appear in one table, others in a second table.

As I said, my input filters are stored at the model level.

If my whole scenario included only one model / table, for example, storing one donation and nothing else, I would go as follows:

$donation = new Donation(); $form->setInputFilter($donation->getInputFilter()); $form->setData($request->getPost()); if ($form->isValid()) { $donation->exchangeArray($form->getData()); $this->getDonationTable()->saveDonation($donation); 

I can do higher.

But I'm trying to analyze and validate form data on two sets of inputFilters, which I collect from two models. One for the data that will go to the donation model, and one for the data that will be transferred to the user. So I get the following:

 $donation = new Donation(); $user = new User(); $form->setInputFilter($donation->getInputFilter()); 

Then I would add something like: (obviously does not exist)

$ form-> addInputFilter ($ user-> getInputFilter ());

So I could continue ...

 $form->setData($request->getPost()); if ($form->isValid()) { // add donation // add user 

I am surprised that I did not find anything about this on the Internet, since I am using models and inputFilters incorrectly? How do you handle validation for a form that has some fields that need to be checked against one model, and some fields that need to be checked against another?

+4
source share
1 answer

Fieldsets (form collections) are what you are looking for. The official documentation is here:

https://zf2.readthedocs.org/en/latest/modules/zend.form.collections.html

+2
source

Source: https://habr.com/ru/post/1484737/


All Articles