Question

CakePHP has a wonderful Error Validation mechanism -> all errors are automatically passed to view and are shown next to each field. Perfect.

The problem comes when you have to use Ajax. Is there a way to automate all of those things?

There is already this answer, that kind of shows how to loope over errors and pass them to JS. However I don't see any further steps. How do you put the errors next to the appropriate fields (if possible) ?

Was it helpful?

Solution

I used this approach: every input field has id set to ModelFieldname like UserUsername. I used something like in the link you provided, to pass error messages with names of fields to js. And in js, I used ids of fields to insert error messages something like this:

$(form+name).before('some styled span with error message');

where form is for example #User and name is the name of field with error eg. username (of course I had it set to upper case first).

There is probably better ways to do it, but that's what I use and it serves me well so far.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top