再次有很多类似的问题,但没有一个真正帮助我。 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'
)
)
);
}
?>
我与其他形式的阅读触发模型保存功能,我会自动触发模型验证。我怎样才能让模型验证工作?
感谢 凯文
注:模型字段默认为<输入名称= “数据[计算机] [模型]” 最大长度= “10” 类型= “文本” ID =“ComputerModel “required =”required“> 我发现一些疑难解答,它是”需要“,因为有一个模型验证规则'notEmpty'。这很棒。但是,它仍然不显示模型验证消息,而是显示HTML5消息。 – aDvo
Cake只添加'required =“required”',这是由浏览器处理的。如果您'检查元素'并手动删除'必需'属性,这将到达服务器,您将从该模型中获得消息。 Cake不会更改浏览器验证消息。这可以手动更改(http://stackoverflow.com/questions/5272433/html5-form-required-attribute-set-custom-validation-message) – cornelb
类似于这个http://stackoverflow.com/a/21094082/ 2776508 – arilia