문제

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) ?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top