问题
I'm working with Zend Form and I need to dynamically (when filling out the form by user) add groups of elements. For example I have form with few fields describing offer and one subform to set offer price. But offer can have more than one price and price is not only 1 element, its composed of
- offer regular price
- offer discount price
- offer items count
So there are 3 different elements in one gruop.
I can create elements with javascript but when should I add them to Zend Form Object?
回答1:
you could make each of the groupings a subform that way when you add via javascript, you could ajax that portion of the form in by just rending the subform with a 'Belongsto'. Then, you could read in your post when it comes back to you and do a foreach on it and add the elements back that way.
check out http://www.stephenrhoades.com/?p=364
Otherwise, in your ajax to get the form markup, you could be building a form object that you could save to the session, that way it will already be built at posting time.
来源:https://stackoverflow.com/questions/6831145/zend-form-dynamic-adding-subforms