Php 验证不适用于更新
更新时未进行模型验证<代码>$model->validate()始终返回true。因此,即使数据错误,也会进行保存 以下是更改密码功能 查看Php 验证不适用于更新,php,yii,Php,Yii,更新时未进行模型验证$model->validate()始终返回true。因此,即使数据错误,也会进行保存 以下是更改密码功能 查看 <?php echo $form->passwordField($model, 'currentpassword', array('class'=>'form-control','required'=>'required', 'value'=>'', 'maxlength'=>'40', 'pattern'=>'[a-zA
<?php echo $form->passwordField($model, 'currentpassword', array('class'=>'form-control','required'=>'required', 'value'=>'', 'maxlength'=>'40', 'pattern'=>'[a-zA-Z0-9-]{6,40}', 'title'=>'Password should be 6-40 characters containing a-z and 0-9')); ?>
<?php echo $form->error($model, 'currentpassword'); ?>
<?php echo $form->passwordField($model, 'password', array('class'=>'form-control','required'=>'required', 'value'=>'', 'maxlength'=>'40', 'pattern'=>'[a-zA-Z0-9-]{6,40}', 'title'=>'Password should be 6-40 characters containing a-z and 0-9')); ?>
<?php echo $form->error($model, 'password'); ?>
<?php echo $form->passwordField($model, 'confirmpassword', array('class'=>'form-control','required'=>'required', 'value'=>'', 'maxlength'=>'40', 'pattern'=>'[a-zA-Z0-9-]{6,40}', 'title'=>'Password should be 6-40 characters containing a-z and 0-9')); ?>
<?php echo $form->error($model, 'confirmpassword'); ?>
型号
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('name, user_type_id', 'required','on'=>'signup'),
array('name, email', 'length', 'max'=>255),
array('email', 'required','on'=>array('recover','signup')),
array('email', 'exists','on'=> 'recover'),
array('email', 'unique'),
array('email', 'email'),
array('user_login_count, user_like_count, user_share_count, user_view_count, user_comment_count, user_rating_count', 'numerical', 'integerOnly'=>true),
array('password', 'length', 'max'=>100),
array('password, confirmpassword', 'required','on'=>array('signup','resetpassword','changepassword')),
array('confirmpassword', 'compare', 'compareAttribute'=>'password','on'=>array('signup','resetpassword','changepassword'),'message'=>'Passwords dont match'),
array('currentpassword', 'compareCurrentPassword','on'=>array('changepassword')),
);
}
查看您的行
$model=User::model()->findByPk(Yii::app()->User->getId())代码>
看起来您需要在那里设置场景,因为您正在创建一个没有“changepassword”场景的新用户
例如:
$model=User::model('changepassword')->findByPk(Yii::app()->User->getId())代码>什么验证失败?模型在不应该保存的情况下保存?没有发生验证。没有抛出错误。它不应该保存。如何在此处传递场景
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('name, user_type_id', 'required','on'=>'signup'),
array('name, email', 'length', 'max'=>255),
array('email', 'required','on'=>array('recover','signup')),
array('email', 'exists','on'=> 'recover'),
array('email', 'unique'),
array('email', 'email'),
array('user_login_count, user_like_count, user_share_count, user_view_count, user_comment_count, user_rating_count', 'numerical', 'integerOnly'=>true),
array('password', 'length', 'max'=>100),
array('password, confirmpassword', 'required','on'=>array('signup','resetpassword','changepassword')),
array('confirmpassword', 'compare', 'compareAttribute'=>'password','on'=>array('signup','resetpassword','changepassword'),'message'=>'Passwords dont match'),
array('currentpassword', 'compareCurrentPassword','on'=>array('changepassword')),
);
}