【发布时间】: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