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

报告相同问题?

悬赏问题

  • ¥15 根据以下文字信息,做EA模型图
  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥60 关机时蓝屏并显示KMODE_EXCEPTION_NOT_HANDLED,怎么修?
  • ¥66 如何制作支付宝扫码跳转到发红包界面