Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Validation 如何返回REST调用的验证错误?_Validation_Cakephp 3.0 - Fatal编程技术网

Validation 如何返回REST调用的验证错误?

Validation 如何返回REST调用的验证错误?,validation,cakephp-3.0,Validation,Cakephp 3.0,我正在为我的应用程序制作一个RESTAPI。它几乎可以工作,但我希望它在出现验证错误时返回这些错误 在cakephp2.x中,我看到了一个,但在Cake3中它不再存在了。相反,我看到表单类中有一个,但我不知道如何使用它 这是我的Users/json/add.ctp文件中的内容: <?php $echo = [ 'errors' => $this->Form->errors(), 'user' => $user ]; echo json_encode

我正在为我的应用程序制作一个RESTAPI。它几乎可以工作,但我希望它在出现验证错误时返回这些错误

在cakephp2.x中,我看到了一个,但在Cake3中它不再存在了。相反,我看到表单类中有一个,但我不知道如何使用它

这是我的Users/json/add.ctp文件中的内容:

<?php
$echo = [
    'errors' => $this->Form->errors(),
    'user' => $user
];

echo json_encode($echo);

当出现所有表单错误时,显示它们的正确方法是什么?

这就是我要做的,检查控制器中的验证错误并将它们发送到序列化视图

public function add() {
    $this->request->allowMethod('post');
    $data = $this->Model->newEntity($this->request->data);

    if($data->errors()) {
        $this->set([
            'errors' => $data->errors(), 
            '_serialize' => ['errors']]);
        return;
    }

     //if no validation errors then save here
}

这就是我要做的,检查控制器中的验证错误,并将它们发送到序列化视图

public function add() {
    $this->request->allowMethod('post');
    $data = $this->Model->newEntity($this->request->data);

    if($data->errors()) {
        $this->set([
            'errors' => $data->errors(), 
            '_serialize' => ['errors']]);
        return;
    }

     //if no validation errors then save here
}