Forms CakePHP-模型验证不起作用

Forms CakePHP-模型验证不起作用,forms,validation,cakephp,Forms,Validation,Cakephp,还有很多类似的问题,但没有一个能真正帮助我。 HTML5表单验证似乎会触发消息“请填写此字段”,而不是模型验证消息的,该消息应为“请输入模型” 我有一个表格可以把计算机添加到数据库中 这是我的表格: echo $this->Form->create('Computer'); echo $this->Form->input('Computer.model', array('label' => 'Model')); echo $this->Form->inp

还有很多类似的问题,但没有一个能真正帮助我。 HTML5表单验证似乎会触发消息“请填写此字段”,而不是模型验证消息的,该消息应为“请输入模型”

我有一个表格可以把计算机添加到数据库中

这是我的表格:

echo $this->Form->create('Computer');
echo $this->Form->input('Computer.model', array('label' => 'Model'));
echo $this->Form->input('Computer.memory', array('label' => 'memory'));
echo $this->Form->input('Computer.hdd', array('label' => 'hdd'));
echo $this->Form->input('Computer.price', array('label' => 'price'));    
echo $this->Form->end('Save Computer');
以下是带有索引和添加操作的完整控制器代码

<?php
class ComputersController extends AppController {

    public $helpers = array('Html', 'Form', 'Session');
    public $components = array('Session');


    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('add');
    }
    public function index() {


        $this->set('computers', $this->Computer->find('all'));

    }

    public function add() {

        if ($this->request->is('post')) {
            if (!empty($this->request->data)) {

                $this->Computer->save($this->request->data);
                $this->Session->setFlash(__('Your Computer has been saved, or so it seems.....'));
                return $this->redirect(array('action' => 'index'));
            }
            $this->Session->setFlash(__('Not sure why we got here 1.'));
        } else {
            $this->Session->setFlash(__('By right, this should be the index page'));


        }

    }

}
?>

这是模型

<?php
class Computer extends AppModel {


public $validate = array(

    'model' => array(
        'Please enter model name'=> array(
            'rule'=>'notEmpty',
            'message'=>'Please enter model'
        )
    )

);
}

?>

我从其他表单中了解到,触发模型保存功能(我这样做)将自动触发模型验证。如何使模型验证工作

谢谢 Kevin

$this->{Model}->save()
如果验证失败,将返回false,但在您的情况下,在save函数之后,您将使用一条flash消息重定向。因此,首先检查表单是否完全保存,如果完全保存,则重定向到列表页面,以其他方式呈现
视图
文件,并显示一条flash消息,您可以在其中查看验证消息

if ($this->Computer->save($this->request->data)) {
    $this->Session->setFlash(__('Your Computer has been saved, or so it seems.....'));
    return $this->redirect(array('action' => 'index'));
} else {
    $this->Session->setFlash(__('Unable to save form'));
}
注意:要禁用html验证,只需执行以下操作

$this->Form->inputDefaults(array(
    'required' => false
)); 
在您的视图文件中


希望这对您有所帮助。

正如您所说,如果您在模型中有
notEmpty
验证,那么CakePHP会在输入属性上添加
required=“required”
。这是由浏览器处理的,因此当您尝试提交空值时,您会看到默认的
请输入此字段
消息。一个优点是,如果您使用不同语言的浏览器,则消息将以该语言显示

如果您想更改该消息,可以尝试类似的解决方案。(这可能不是您想要的)

如果要删除该客户端消息,可以使用
novalidate

echo $this->Form->create('Computer', array('novalidate' => 'novalidate'));
这样,HTML5 required属性将被忽略,您将从模型中获得消息


我不确定是否有方法告诉Cake在客户端上使用服务器端值。

在FormHelper::create()的选项中设置'novalidate'=>true

echo$this->Form->create('Computer',array('novalidate'=>true))


有关更多信息,请转到

注意:模型字段默认设置,因为我发现有一些问题,提示它被设置为“必需”,因为有一个模型验证规则“notEmpty”。这太棒了。但是,它仍然不显示模型验证消息,而是显示HTML5消息。Cake仅添加由浏览器处理的
required=“required”
。如果您“inspect element”并手动删除“required”属性,这将到达服务器,您将从模型中获得消息。Cake不会更改浏览器验证消息。这可以手动更改(),类似于我所做的@AD7six,并添加了一个潜在的解决方案。不确定告诉Cake如何在客户端使用服务器端值。的意思是(通过ajax验证?不,没有)+1.谢谢,这就是我想要的答案。其他答案仍然有用。不明显的是,调用modelsave方法失败,然后重定向到索引页面,会导致显示models错误消息。只是不明显。。。。