dongleman4760 2012-08-08 09:44
浏览 53
已采纳

使用CakePHP ajax视图验证ajax请求将返回所有无效的字段,而不是正确的字段

I'm not sure why I keep receiving all the errors instead of just the invalid fields even when I fill out some of the required fields properly.

Submissions Controller:

public function submit() {
    $this->set('title_for_layout', 'Submit - ');

    if ($this->request->is('ajax')) {
        if (!empty($this->request->data)) {
            $this->Submission->set($this->request->data);
            if ($this->Submission->invalidFields($this->request->data)) {
                $formErrors = $this->Submission->validationErrors;
            } else {
                $formErrors = null;
            }
        } else {
            $formErrors = null;
        }

        $this->set(compact('formErrors'));

}

/Submissions/json/submit.ctp:

<?php

$toReturn = array(
    'formErrors' => $formErrors
);

echo json_encode($toReturn);

Submission model:

var $validate = array(
    'title' => array(
        'title' => array(
            'rule' => 'notEmpty',
            'required' => true,
            'allowEmpty' => false,
            'message' => 'Please enter a title'
        ),
        'minLength' => array(
            'rule' => array('minLength', 5),
            'message' => 'Please make your title longer (e.g. IJL John F. Kennedy donated his presidential salary to charity)'
        ),
        'maxLength' => array(
            'rule' => array('maxLength', 300),
            'message' => 'Your title needs to be shorter'
        ),
    ),
    'description' => array(
        'shortDescription' => array(
            'rule' => array('shortDescription'),
            'message' => 'Your description needs to be longer'
        ),
        'longDescription' => array(
            'rule' => array('longDescription'),
            'message' => 'Your description needs to be shorter'
        ),
    ),
    'source' => array(
        'source' => array(
            'rule' => 'notEmpty',
            'required' => true,
            'allowEmpty' => false,
            'message' => 'Enter a valid source URL (e.g. http://en.wikipedia.org/wiki/Penguins)'
        ),
        'website' => array(
            'rule' => 'url',
            'message' => 'Enter a valid source URL (e.g. http://en.wikipedia.org/wiki/Penguins)'
        ),
    ),
    'category' => array(
        'category' => array(
            'rule' => 'notEmpty',
            'required' => true,
            'allowEmpty' => false,
            'message' => 'Please choose a category'
        )
    )
);

Form values that are getting serialized and sent:

form values

Errors I'm getting in a json response:

enter image description here

Pulling hair out over here :|

  • 写回答

2条回答 默认 最新

  • duankui6150 2012-08-08 10:31
    关注

    You seem to have got a little muddle up with validates() and invalidFields()

    invalidFields() returns the invalid fields after a validates(), see: http://book.cakephp.org/2.0/en/models/data-validation/validating-data-from-the-controller.html

    So your code should look something like this:

    $this->Submission->set($this->request->data);
    if (!$this->Submission->validates()) {
        $formErrors = $this->Submission->invalidFields();
    } else {
        $formErrors = null;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元