A
A
Andrey Degtyaruk2014-01-30 13:09:22
Zend Framework
Andrey Degtyaruk, 2014-01-30 13:09:22

How to display label and description for each element of a form, form, fieldset, having previously taken them from the database?

Task: display label and description for each element of a form, form, fieldset, having previously taken them from the database. That is, not as usual

$element->setOptions(array(
'label' => 'Какой хороший лейбл',
));

Namely, having previously pulled them out of the base, while not violating the general principles of MVC and ZF2. Moreover, the keys of the elements in the database must be = the full name of the element on the form (as they leave the POST request). At first the solution seems obvious - to override the prepare() and prepareElement methods, however, I immediately run into a problem in collections, since elements are created differently there and there is no access to the full name. Maybe someone has already done this? I ask for the advice of the wise.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Degtyaruk, 2014-02-03
@hlogeon

I looked through the entire code of the Zend\Form\Element, Fieldset, Form classes,
Taki found a solution. In the prepare() and prepareElement() methods , which, by the way, are called recursively for all descendants (that is, we always know the name of the "parent", which in this case plays a decisive role). I added a method there, which by the element id (which consists of the full name on the form) gets the desired description and label. This is what happened before. The problem itself lay in the fact that in the Collection, the members of this very collection are created using a factory. Zend\Form\Factory. That is, my mistake and the problem turned out to be very simple, it was necessary, in addition to the basic forms and fieldsets themselves, to redefine the factory that creates them. I puzzled for more than one hour what the trouble was, but everything turned out to be very simple)
Special thanks to @cawakharkov for help =)
I hope it will come in handy for someone else someday.

C
cawakharkov, 2014-02-01
@cawakharkov

I think the easiest way would be to write your own viewhelper

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question