[英]CakePHP Validation
How do I show the messages when using the CakePHP validation? 使用CakePHP验证时如何显示消息? As I creating the input fields manually using input()
instead using the shorthand form()
helper. 当我使用input()
而不是简写的form()
助手来手动创建输入字段时。
eg 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(); ?>
and this is my validation in the user model: 这是我在用户模型中的验证:
public $validate = array(
'email' => array(
'valid' => array(
'rule' => 'email',
'message' => 'The email is not valid'
),
'required' => array(
'rule' => 'notEmpty',
'message' => 'Please enter an email'
)
)
);
However the validation error messages don't show? 但是验证错误消息不显示吗?
EDIT: I tested this on my register form at /users/add/ and it works so it seems that the auto validation does not work with the login method???? 编辑:我在/ users / add /的注册表单上对此进行了测试,它可以正常工作,因此似乎自动验证不适用于登录方法吗? How do I add validation for the login form then :/ 然后如何为登录表单添加验证:/
The validation is actually stored in the model object. 验证实际上存储在模型对象中。 I'm not entirely sure off-hand how to access the errors, but I think its in $this->User->validationErrors. 我不确定如何立即访问错误,但我认为它在$ this-> User-> validationErrors中。
Have a look at the model api for more information. 请查看模型api以获取更多信息。
For logging in, use the auth component . 要登录,请使用auth组件 。 If you'd rather not, then just get the user from the db and display an error using $this->Session->SetFlash() if the user doesn't authenticate. 如果您不愿意,那么只需从数据库中获取用户,如果用户未通过身份验证,则可以使用$ this-> Session-> SetFlash()显示错误。
You can show your code login function? 您可以显示代码登录功能吗?
I think in your code that have redirect in-case validate false 我认为在您的代码中有重定向情况下验证false
If you use $this->redirect() it'll not show validate messages :) 如果使用$ this-> redirect(),则不会显示验证消息:)
First you check your post data is going to validate function.you can simple check this in your else condition like this : 首先,您检查您的帖子数据将要验证功能。您可以在其他条件下简单检查此内容,如下所示:
$this->{$this->modelClass}->set($this->data);
if($this->{$this->modelClass}->validates(){
//Save data in DB
}else{
pr($this->{$this->modelClass}->validationErrors); // This will show your error message
}
You can also show error in your view.ctp file like this 您也可以像这样在view.ctp文件中显示错误
$errors = ''; foreach ($this->validationErrors[$model] as $key => $validationError) { $errors .= $this->Html->tag('li', $validationError[0]); } echo $this->Html->tag('ul', $errors,array('class' => 'error'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.