Allow a User to Add Fields to a Form Created With Zend Framework

耗尽温柔 提交于 2020-02-24 09:05:07

问题


I'm using Zend Framework and I currently have an existing form using zend-form which functions as required.

I want to add another optional text field to it, but allow the user to choose to display the field and also display it multiple times. e.g. A user registration form with an 'alternative emails' area, allowing the user to add further text fields for each of their email addresses.

Unfortunately, I'm not sure how to go about this. I think sub-forms might be the way forward, but not too sure.

Ideally, once the form is submitted I'd want the data in an array so that I can process it for storing in a MySQL table afterwards.


回答1:


See this excellent post by Jeremy Kendall on dynamically adding fields to a form:

Dynamically Adding Elements to Zend_Form

Upshot is to use jQuery on the client-side to add fields and maintain a registry of the new fields. Then on the server-side, call a new preValidate() method on the form object which checks the posted registry and adds the required fields into the $form object before standard processing - like isValid() and getValues() - is invoked.




回答2:


Why don't you make an AJAX call to an addfieldAction() method in which you generate the HTML of another textfield and return that? You can still make use of Zend Form and the whole form can be processed the normal way.



来源:https://stackoverflow.com/questions/6691378/allow-a-user-to-add-fields-to-a-form-created-with-zend-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!