Skip to content
Advertisement

How to display validation errors in JSON of associated fields in Cakephp4?

I’m looking for a way to display validation errors in jQuery after sending a form in ajax that returns validation errors in JSON.

My code works for simple form, but the difficulties appear for more complex forms, especially for forms containing inputs for associated data.

For example :

Here is a form containing input files and associated data for writing captions :

JavaScript

I post the form in ajax to my ArticlesController‘s edit method that looks like that :

JavaScript

Here is the return of validation errors in JSON :

JavaScript

Here is how I’m tring to display validation errors :

JavaScript

The problem is that validation errors for associated data fields are nested (e.g photos), so how to get the input’s id involved ?

Could anyone help me to find a correct implementation for my jQuery function displayFieldErrors() ?

Advertisement

Answer

I’ve solved the problem by returning formated validation errors from a JSON View (server side) :

JavaScript

JSON is now looking like that :

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement