Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8526851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:23:26+00:00 2026-06-11T08:23:26+00:00

I have a CakePHP (latest version) web app with forms and validation all working

  • 0

I have a CakePHP (latest version) web app with forms and validation all working properly using traditional postback, but now I’m switching some of the forms to submit via ajax. When there are validation errors, I would like to get them back on the client as JSON formatted like so:

{
"success":false,
"errors":{
    "data[User][username]":["This is not a valid e-mail address"],
    "data[User][password]":["You must choose a password"]
}}

The keys for the errors array need to correspond to the name attribute on the form fields. We have some prebuilt client script that is expecting JSON formatted in this way. The good news is that this is very close to what the validationErrors object looks like in CakePHP. So I’m currently doing this in my controller:

if ($this->User->save($this->request->data)) {

} else {
    if ($this->request->is('ajax')) {
        $this->autoRender = $this->layout = false;
        $response['success'] = false;
        $response['errors'] = $this->User->validationErrors;
        echo json_encode($response);
        exit(0);
    }
}

However, this is what the JSON response looks like:

{
"success":false,
"errors":{
    "username":["This is not a valid e-mail address"],
    "password":["You must choose a password"]
}}

Note that the errors keys have just the basic database table field names in them. They are not converted into data[User][username] format, which the FormHelper usually takes care of.

Is there some easy way to adjust the array before I return it? I don’t want to simply loop through and prepend “data[User]” because that is not robust enough. I’d like some code I can put in one place and call from various controllers for various models. What does FormHelper use to come up with the input name attributes? Can I tap into that? Should I somehow use a JSON view?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T08:23:27+00:00Added an answer on June 11, 2026 at 8:23 am

    That’s because that’s the way the $validationErrors array is formatted. To obtain the output you want you will have to loop through, there’s no way around it.

    foreach ($this->User->validationErrors as $field => $error) {
      $this->User->validationErrors["data[User][$field]"] = $error;
      unset($this->User->validationErrors[$field]);
    }
    

    I would suggest instead passing all errors to json_encode(). $this->validationErrors is a combined list of all model validation errors for that request available on the view (compiled after render). You should move your display logic (echoing json) into your view, and loop through it there.

    in the view

    $errors = array();
    foreach ($this->validationErrors as $model => $modelErrors) {
      foreach ($modelErrors as $field => $error) {
        $errors["data[$model][$field]"] = $error;
      }
    }
    $response['errors'] = $errors;
    echo json_encode($response);
    

    This would output something like this:

    {
      "success":false,
      "errors": [
          "data[User][username]": "This is not a valid e-mail address",
          "data[User][password]": "This is not a valid password",
          "data[Profile][name]": "Please fill in the field"
      ]
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a simple web application using CakePHP version 1.2 (the latest) and am
I have a CakePHP ticketing app where I am using Revision Behavior to keep
I am currently using Ubuntu 12 and trying to run cakephp's latest stable version,
I'm coding a tag search in CakePHP (latest release) but the solution I have
I have a CakePHP site that is working fine (in terms of encoding)...but a
Ok, this is definitely weird. I have a cakephp app I'm using. I've removed
I have a cakePHP app working at http://domain1.com/domain2 , and want to point http://domain2.com/
I have configured CakePHP 2.1 to use Memcache . But debugkit insists on using
I have CakePHP app in which I'd like to attach gallery to multiple resources.
I have used cakePHP 1.2 and know how to use behaviors in it. using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.