1

我不确定为什么即使我正确填写了一些必填字段,我也会继续收到所有错误,而不仅仅是无效字段。

提交控制器:

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

}

/提交/json/submit.ctp:

<?php

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

echo json_encode($toReturn);

提交模型:

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

正在序列化和发送的表单值:

表单值

我在 json 响应中遇到的错误:

在此处输入图像描述

在这里拉头发:|

4

2 回答 2

1

您似乎对 validates() 和 invalidFields() 有点困惑

invalidFields() 在 validates() 之后返回无效字段,请参阅:http ://book.cakephp.org/2.0/en/models/data-validation/validating-data-from-the-controller.html

所以你的代码应该是这样的:

$this->Submission->set($this->request->data);
if (!$this->Submission->validates()) {
    $formErrors = $this->Submission->invalidFields();
} else {
    $formErrors = null;
}
于 2012-08-08T10:31:20.770 回答
0

首先,给模型设置数据:

$this->ModelName->set($this->request->data);

然后,要检查数据是否验证,请使用模型的 validates 方法,如果验证则返回 true,否则返回 false:

if ($this->ModelName->validates()) {
    // it validated logic
} else {
    // didn't validate logic
    $errors = $this->ModelName->validationErrors;
}

验证来自控制器的数据

于 2013-10-26T12:20:12.503 回答