如何在CakePHP中将验证错误字段名称转换为输入名称? [英] How can I convert validation error field names to input names in CakePHP?

查看:87
本文介绍了如何在CakePHP中将验证错误字段名称转换为输入名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CakePHP(最新版本)网络应用程序与表单和验证都使用传统的回发正常工作,但现在我正在切换一些表单提交通过ajax。当有验证错误,我想让他们回来在客户端的JSON格式如下:

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"]
}}

errors数组的键需要与表单字段上的name属性相对应。我们有一些预构建的客户端脚本,希望以这种方式格式化JSON。好消息是,这与CakePHP中的validationErrors对象非常接近。所以我目前在我的控制器中这样做:

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);
    }
}

但是,这是JSON响应:

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"]
}}

请注意,错误键只有其中的基本数据库表字段名称。它们不会转换为数据[User] [username]格式,FormHelper通常会处理。

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.

在返回之前,有没有一些简单的方法来调整数组?我不想简单地循环和前置data [User],因为它不够健壮。我想要一些代码,我可以放在一个地方,并从各种控制器调用各种型号。 FormHelper使用什么来得到输入名称属性?我可以搞定吗?我应该以某种方式使用JSON视图?

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?

推荐答案

这是因为这是 $ validationErrors 数组已格式化。要获得你想要的输出,你必须循环,没有办法绕过它。

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]);
}



我建议将所有错误传递给 json_encode ) $ this-> validationErrors 是视图上可用的请求的所有模型验证错误的组合列表(在渲染后编译)。

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.

在视图中显示逻辑b
$ b

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

这将输出如下:

{
  "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"
  ]
}

这篇关于如何在CakePHP中将验证错误字段名称转换为输入名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆