【发布时间】:2011-05-14 11:50:29
【问题描述】:
使用 CakePHP 验证时如何显示消息?当我使用 input() 手动创建输入字段时,而不是使用简写 form() 助手。
例如表格:
<?php echo $this->Form->create('User', array('id' => 'loginform', 'type' => 'post',
'url' => array('controller' => 'users', 'action' => 'login'))); ?>
<fieldset id="login">
<ul class="clearfix">
<li id="li-username">
<?php echo $this->Form->input('email', array( 'label' => array('class' => 'placeholder', 'text' => 'Email address or username') )); ?>
</li>
<li id="li-password">
<?php echo $this->Form->input('password', array( 'type' => 'password', 'label' => array('class' => 'placeholder', 'text' => 'Password') )); ?>
<span id="iforgot"><?php echo $this->Html->link('?',
array('controller' => 'users', 'action' => 'forgotpassword'), array('title' => 'Forgot your password?')); ?></span>
</li>
<li id="li-submit">
<button type="submit" title="Log in">Log in ►</button>
</li>
</ul>
</fieldset>
<?php echo $this->Form->end(); ?>
这是我在用户模型中的验证:
public $validate = array(
'email' => array(
'valid' => array(
'rule' => 'email',
'message' => 'The email is not valid'
),
'required' => array(
'rule' => 'notEmpty',
'message' => 'Please enter an email'
)
)
);
但是没有显示验证错误消息?
编辑: 我在 /users/add/ 的注册表单上对此进行了测试,它可以正常工作,因此自动验证似乎不适用于登录方法????那么如何为登录表单添加验证:/
【问题讨论】:
-
Cameron 你能发布你的用户/添加操作代码吗?您可以通过转储 invalidfields 变量来查看验证失败的原因。 pr($this->User->invalidFields);
标签: php validation cakephp