【发布时间】:2012-05-02 19:11:34
【问题描述】:
我有问题..我尝试填写表单然后单击提交按钮然后它显示表单错误但所有文本框字段都变为空白并显示表单错误。我希望它与验证之前的值相同。就像我不想在验证后清除/清空值。
我做了这样的事情:(在视图中)
<div class="regform">
<div id="formleft">First Name<asterix>*</asterix></div>
<div id="formright"><?php echo $this->Form->input('First Name', array(
'name' => 'firstname',
'label'=> false
)); ?></div>
<div id="formerror"><?php echo $this->Form->error('Customer.firstname'); ?></div>
</div>
(在模型中):
'firstname' => array(
'firstname_cant_be_empty' => array(
'rule' => 'notEmpty',
'message' => 'First Name must be filled.'
),
'firstname_must_be_alphabet' => array(
'rule' => '/^[a-z]{3,}$/i',
'message' => 'First Name is only letters. No numbers, Dotted, etc.'
)
)
(在控制器中):
if ($this->request->is('Post')) {
if ($this->Customer->save($this->request->data) && $this->Customer->validates()) {
//debug($this->request->data);
$this->Session->setFlash('good!');
//$this->redirect('/');
} else {
$this->Session->setFlash('Fill up form now!');
$this->data = $this->request->data;
}
}
【问题讨论】:
-
我认为你不需要 $this->Customer->validates() 因为它已经在 save 方法中做到了。
标签: forms validation cakephp post