我正在将CakePhp 1.1升级到1.2 (甚至更高).终于来了。
我在表单验证方面有问题。我从文档中了解到,我发现$html->tagErrorMsg是不推荐的,需要更改为$form->error。
我在所有位置都这样做,但是错误不会显示。在我的1.1版本中,它们工作得很好。
这是来自.ctp的代码
.ctp曾经是:
<div class="column span-5">
<?php echo $html->input('Account/firstname', array('size' => 20, 'class'=>'span-4 first last txt')); ?>
</div>
<div class="column span-3 last"><span class="my_error"><?php echo $html->tagErrorMsg('Account/firstname', 'Please enter a first name.');?></span></div>
</div>.ctp现在是:
<div class="column span-5">
<?php echo $form->input('Account/firstname', array('size' => 20, 'class'=>'span-4 first last txt')); ?>
</div>
<div class="column span-3 last"><span class="my_error"><?php echo $form->error('Account/firstname', 'Please enter a first name.');?></span></div>
</div>在模型(account.php)中:我将其更改为:
var $validate = array(
'firstname' => VALID_NOT_EMPTY,
);至:
var $validate = array(
'firstname' => 'notEmpty',
);我做错了什么?请在1.2及以后提供一个正确的表单验证示例好吗?
发布于 2013-08-14 15:27:54
我想通了。在1.1到1.2之间还有一些更多的约定更改
在控制器中,我必须添加:
$this->Account->set($this->data);
if ($this->Account->validates()) {
// validated logic
} else {
// didn't validate logic
$errors = $this->Account->validationErrors;
}然而,这也没有完全做到这一点。我还必须删除ctp文件中的"Account/“引用。
以下是正确的ctp:
<div class="column span-5">
<?php echo $form->input('firstname', array('size' => 20, 'class'=>'span-4 first last txt')); ?>
</div>
<div class="column span-3 last"><span class="my_error"><?php echo $form->error('firstname', 'Please enter a first name.');?></span></div>
</div>事实证明,我的控制器也不是使用复数约定命名的。因此,为了使我的$form->create()正确工作,我还必须添加它。
<?php echo $form->create('Account', array('action' => 'register')); ?>它位于ctp中表单的开头,而不是
<form action="<?php echo $html->url('/account/register/'); ?>" method="post">https://stackoverflow.com/questions/18212001
复制相似问题