【问题标题】:CakePHP - Model validation does not workCakePHP - 模型验证不起作用
【发布时间】:2014-03-06 04:38:03
【问题描述】:

还有很多类似的问题,但没有一个能真正帮助我。 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'
        )
    )

);
}

?>

我从其他触发模型保存功能的表单中读取,我这样做会自动触发模型验证。如何让模型验证工作?

谢谢 凯文

【问题讨论】:

  • 注意:模型字段默认为 I通过一些故障排除发现它是“必需的”,因为有一个模型验证规则“notEmpty”。这很棒。但是,它仍然不显示模型验证消息,而是显示 HTML5 消息。
  • Cake 只添加required="required",由浏览器处理。如果您“检查元素”并手动删除“必需”属性,这将到达服务器,您将从模型中获取消息。 Cake 不会更改浏览器验证消息。这可以手动更改 (stackoverflow.com/questions/5272433/…)
  • @AD7six 我做了,并添加了一个潜在的解决方案。

标签: forms validation cakephp


【解决方案1】:

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

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

如果您想删除该客户端消息,可以使用 novalidate 禁用它

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

这样,HTML5 所需的属性将被忽略,您将从模型中获取消息。

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

【讨论】:

  • 不确定a way to tell Cake to use the server-side value on the client. 是什么意思(通过ajax 验证?不,没有)。 +1。
【解决方案2】:

$this-&gt;{Model}-&gt;save() 如果验证失败,则返回 false,但在您的情况下,您在保存功能后使用 Flash 消息进行重定向。所以首先检查表单是否完美保存,如果完美保存,则重定向到列表页面,否则使用 Flash 消息呈现您的 view 文件,您可以在其中查看验证消息。

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

在您的视图文件中

希望对你有所帮助。

【讨论】:

  • 谢谢,这是我正在寻找的答案。其他答案仍然很有帮助。对模型保存方法的失败调用然后重定向到索引页面会导致模型错误消息显示,这一点并不明显。只是不明显....
【解决方案3】:

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

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

欲了解更多信息,请转至http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多