2014-03-06 92 views
0

再次有很多类似的问题,但没有一个真正帮助我。 HTML5表单验证似乎与消息来触发它应该是“请输入模式”CakePHP - 模型验证不起作用

我有一个表格将计算机添加到数据库中的模型验证消息的代替“请填写此字段”。

这里是我的形式:

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

); 
} 

?> 

我与其他形式的阅读触发模型保存功能,我会自动触发模型验证。我怎样才能让模型验证工作?

感谢 凯文

+0

注:模型字段默认为<输入名称= “数据[计算机] [模型]” 最大长度= “10” 类型= “文本” ID =“ComputerModel “required =”required“> 我发现一些疑难解答,它是”需要“,因为有一个模型验证规则'notEmpty'。这很棒。但是,它仍然不显示模型验证消息,而是显示HTML5消息。 – aDvo

+2

Cake只添加'required =“required”',这是由浏览器处理的。如果您'检查元素'并手动删除'必需'属性,这将到达服务器,您将从该模型中获得消息。 Cake不会更改浏览器验证消息。这可以手动更改(http://stackoverflow.com/questions/5272433/html5-form-required-attribute-set-custom-validation-message) – cornelb

+0

类似于这个http://stackoverflow.com/a/21094082/ 2776508 – arilia

回答

0

$this->{Model}->save()返回false如果验证失败,但在你的情况你用闪光灯消息重定向保存功能后。所以首先检查表格是否完全保存,如果完美保存,然后重定向到列表页面,则可以使用闪存消息显示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 
)); 

在你的视图文件

希望这有助于你。

+0

谢谢,这是我正在寻找的答案。其他答案仍然有帮助。调用模型保存方法失败并重定向到索引页会导致显示模型错误消息并不明显。它只是不明显.... – aDvo

1

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

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

如果你想删除客户端的消息,您可以使用novalidate

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

这种方式禁用它,在HTML5所需的属性将被忽略,而你将从模型中获取消息。

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

+0

不知道有什么方法可以告诉Cake在客户端使用服务器端的值。意思是(通过ajax验证吗?不,没有)。 +1。 – AD7six