简体   繁体   中英

How can you add Symfony form fields based on failed validation?

Basically, my model requires that an object is tied to a physical location. For this I'm pulling locations from an API via AJAX using keywords entered by a user in a text field, jQuery autocomplete is setup to set a hidden field to a unique ID when the user selects a valid location recommended to them.

This is all fine, assuming the user has JavaScript and clicks a location.

I need to implement a fallback, ie for whatever reason, if the form is submitted without the hidden field having a value, the form should use whatever is in the visible to query the API and present a select box for users to choose a value from.

I know roughly the code I need to add, but I'm lost as to where to bind it in. Any help appreciated.

Essentially you have to create a custome widget and use it in place of the locations form field. You can then add you javascript to the widgets rendering. If you extend sfWidgetChoice or the doctrine equivalent it will default to a select box. Then if js is enabled your widget's js will rewrite the field to an auto complete.

Have a look at the sfFormExtraPlugin for some great examples of how to do this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM