Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Appropriate approach to failed server side validation return to ajax?


Go to End


1068 Views

Avatar
flipsidenz

Community Member, 49 Posts

26 January 2014 at 1:05am

I have a form on a site I am building which is posted via ajax. It first runs through jQuery validate plugin validation, and on success POSTS through ajax.

If the form has been filled out correctly, all works well. But if for whatever reason client-side validation succeeds when it shouldn't have, when the ajax POST is made, the form detects a validation error (e.g. blank required name) & returns the full template HTML for the rendered page.

Because Ajax is POST'ing the form data, is there a way to manipulate the response? Rather than sending through the template HTML, return an json object of validation errors?

Looking into framework's form.php, I see the method getValidationErrorResponse. I guess I could subclass form and write my own getValidationErrorResponse to manage how the response is handled? However, is there something already pre-built into SS to manage this type of situation?