Php YII验证规则,正则表达式不在YII中工作
我在YII中有以下验证规则,不知何故,正则表达式似乎不起作用: Yii-modal中的规则:Php YII验证规则,正则表达式不在YII中工作,php,regex,validation,yii,Php,Regex,Validation,Yii,我在YII中有以下验证规则,不知何故,正则表达式似乎不起作用: Yii-modal中的规则: public function rules() { return array( array('password, email, username, password_confirmation', 'required'), array('password','match', 'pattern'=> '/^[a-zA-Z]\w{5
public function rules()
{
return array(
array('password, email, username, password_confirmation', 'required'),
array('password','match', 'pattern'=> '/^[a-zA-Z]\w{5,20}$/','message'=>"{attribute} should contain only alphanumeric and underscore."),
array('email_conf', 'numerical', 'integerOnly'=>true),
array('username, email, login_source', 'length', 'max'=>150),
array('email','email'),
array('password, password_confirmation', 'length', 'min'=>6, 'max'=>200),
array("password", "compare", "compareAttribute" => "password_confirmation"),
array('fb_id', 'length', 'max'=>100),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('id, username, email, password, login_source, fb_id, email_conf', 'safe', 'on'=>'search'),
array("password_confirmation, email_conf", "safe")
);
}
在上述代码中,与我有关的行如下所示:
array('password','match', 'pattern'=> '/^[a-zA-Z]\w{5,20}$/','message'=>"{attribute} should contain only alphanumeric and underscore."),
正则表达式只接受字母作为第一个字符和特殊字符不接受,空格也不接受 现在,尽管我使用了正确的YII语法() 正则表达式似乎不起作用 我为此模式创建了一个简单的PHP文本案例:
<?php
if (isset($_POST['submit'])) {
$holder = $_POST['regex'];
$regrex = preg_match('/^[a-zA-Z]\w{5,20}$/', $holder);
if ($regrex) {
echo "Match";
}
else {
echo "Not match";
}
}
?>
<form action="" method="post" >
<input type="text" name="regex"><br><br>
<button type="submit" value="submit" name="submit">Submit</button>
</form>
提交
而且正则表达式工作得很好!,那么,即使我正确地遵循了YII语法,YII的问题是什么呢
编辑:
public function actionCreate()
{
$model=new User;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['User']))
{
$username = $_POST['User'];
if(isset($username['email'])) {
$record=User::model()->find(array(
'select'=>'*',
'condition'=>'email=:email',
'params'=>array(':email'=>$username['email']))
);
}
if($record===null){
$model->attributes=$_POST['User'];
$model->password = crypt($model->password,'salt');
$model->password_confirmation = crypt($model->password_confirmation,'salt');
//$model->password = $model->password;
if($model->save()){
// confirm mail send
$confirm_record=User::model()->find(array(
'select'=>'*',
'condition'=>'email=:email',
'params'=>array(':email'=>$username['email']))
);
$to = $username['email'];
$subject = 'Confirm your Account';
$htmlBody = "Welcome and thank you for registering at Aaskar Pet Resort!<br>";
$htmlBody .= "Your account has now been created and you can log in by using your email address and password by visiting our website.<br>";
$htmlBody .= "<p><a style='text-decoration:none;' href ='".Yii::app()->request->hostInfo.''.$this->createUrl('site/confirmed',array('id'=> base64_encode($confirm_record['id'])))."'>Confirm your account</a></p>";
$htmlBody .= "<p>Upon logging in, you will be able to access other services including reservation for your pets, booking services and editing your account information.</p>";
$htmlBody .= "<p>Thanks,<br>Aaskar Pet Resort</p>";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: <donotreply@purplefront.net>' . "\r\n";
$sent = mail($to, $subject, $htmlBody, $headers);
if($sent){
$message = $this->dynamicStatus('create_success');
Yii::app()->user->setFlash('success', $message);
$this->redirect(array('site/index'));
}
//Yii::app()->user->setFlash('success', 'Account has been created');
//$this->redirect(array('site/index'));
}
}else{
$message = $this->dynamicStatus('email_exist');
Yii::app()->user->setFlash('error', $message);
}
}
$this->render('create',array(
'model'=>$model,
));
}
视图文件中相应的标签如下所示:
<div class="">
<?php echo $form->labelEx($model,'password'); ?>
<?php echo $form->passwordField($model,'password',array('size'=>50,'maxlength'=>50)); ?>
<?php echo $form->error($model,'password'); ?>
</div>
泰纳利
编辑:
控制器代码:
public function actionCreate()
{
$model=new User;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['User']))
{
$username = $_POST['User'];
if(isset($username['email'])) {
$record=User::model()->find(array(
'select'=>'*',
'condition'=>'email=:email',
'params'=>array(':email'=>$username['email']))
);
}
if($record===null){
$model->attributes=$_POST['User'];
$model->password = crypt($model->password,'salt');
$model->password_confirmation = crypt($model->password_confirmation,'salt');
//$model->password = $model->password;
if($model->save()){
// confirm mail send
$confirm_record=User::model()->find(array(
'select'=>'*',
'condition'=>'email=:email',
'params'=>array(':email'=>$username['email']))
);
$to = $username['email'];
$subject = 'Confirm your Account';
$htmlBody = "Welcome and thank you for registering at Aaskar Pet Resort!<br>";
$htmlBody .= "Your account has now been created and you can log in by using your email address and password by visiting our website.<br>";
$htmlBody .= "<p><a style='text-decoration:none;' href ='".Yii::app()->request->hostInfo.''.$this->createUrl('site/confirmed',array('id'=> base64_encode($confirm_record['id'])))."'>Confirm your account</a></p>";
$htmlBody .= "<p>Upon logging in, you will be able to access other services including reservation for your pets, booking services and editing your account information.</p>";
$htmlBody .= "<p>Thanks,<br>Aaskar Pet Resort</p>";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: <donotreply@purplefront.net>' . "\r\n";
$sent = mail($to, $subject, $htmlBody, $headers);
if($sent){
$message = $this->dynamicStatus('create_success');
Yii::app()->user->setFlash('success', $message);
$this->redirect(array('site/index'));
}
//Yii::app()->user->setFlash('success', 'Account has been created');
//$this->redirect(array('site/index'));
}
}else{
$message = $this->dynamicStatus('email_exist');
Yii::app()->user->setFlash('error', $message);
}
}
$this->render('create',array(
'model'=>$model,
));
}
公共函数actionCreate()
{
$model=新用户;
//如果需要AJAX验证,请取消注释以下行
//$this->performAjaxValidation($model);
如果(isset($_POST['User']))
{
$username=$_POST['User'];
如果(设置($username['email'])){
$record=User::model()->find(数组(
'选择'=>'*',
'条件'=>'电子邮件=:电子邮件',
'params'=>array(':email'=>$username['email']))
);
}
如果($record==null){
$model->attributes=$\u POST['User'];
$model->password=crypt($model->password,'salt');
$model->password\u confirmation=crypt($model->password\u confirmation,'salt');
//$model->password=$model->password;
如果($model->save()){
//确认邮件发送
$confirm\u record=User::model()->find(数组(
'选择'=>'*',
'条件'=>'电子邮件=:电子邮件',
'params'=>array(':email'=>$username['email']))
);
$to=$username['email'];
$subject='确认您的帐户';
$htmlBody=“欢迎并感谢您在Aaskar宠物度假村注册!
”;
$htmlBody.=“您的帐户现在已创建,您可以通过访问我们的网站使用您的电子邮件地址和密码登录。
”;
$htmlBody.=“”;
$htmlBody.=“登录后,您将能够访问其他服务,包括宠物预订、预订服务和编辑您的帐户信息。”;
$htmlBody.=“谢谢,
Aaskar宠物度假村””;
$headers=“MIME版本:1.0”。\r\n”;
$headers.=“内容类型:text/html;字符集=iso-8859-1”。\r\n”;
$headers.='From:'。“\r\n”;
$sent=mail($to、$subject、$htmlBody、$headers);
如果($已发送){
$message=$this->dynamicStatus('create_success');
Yii::app()->user->setFlash('success',$message);
$this->redirect(数组('site/index'));
}
//Yii::app()->user->setFlash('success','Account have created');
//$this->redirect(数组('site/index'));
}
}否则{
$message=$this->dynamicStatus('email_exist');
Yii::app()->user->setFlash('error',$message);
}
}
$this->render('create',数组(
'model'=>$model,
));
}
下面是上面关于pastebin的代码链接,以防万一:
为什么只为
搜索
场景保留密码
参数安全()
array('id, username, email, password, login_source, fb_id, email_conf', 'safe', 'on'=>'search'),
您可能需要检查场景中的模型。为什么不尝试在所有情况下确保这些安全:
array('id, username, email, password, login_source, fb_id, email_conf', 'safe'),
更新
从您的控制器/操作代码中,我看到,在获取密码字段之后,您立即执行密码加密。但是您错过了对上述模型规则的验证。
因此,如果要进行验证,请在加密之前进行验证:
$model->attributes=$_POST['User'];
$model->validate(); // validation is performed
$model->password = crypt($model->password,'salt');
$model->password_confirmation = crypt($model->password_confirmation,'salt');
...
// comment one or another of the following lines:
$model->save(); // again validation is performed
$model->save(false); // no validation is performed when saving AR
如果要使用已加密的密码在中逃避验证,请执行以下操作:
$model->save(false)代码>u是说,我应该将其设置为数组(“id、用户名、电子邮件、密码、登录源、fb\u id、电子邮件配置”、“安全”、“打开”=>“搜索”),而不是数组(“id、用户名、电子邮件、密码、登录源、fb\u id、电子邮件配置”、“安全”)??这真的会有什么作用?这不会将此规则限制为唯一的“搜索”场景。请参见此处:这些属性将始终安全
。pritty insightful!感谢您的回答和时间,但我的问题是,为什么我的模式regex在YII中不起作用,因为它在普通PHP中工作得很好?@Tenali_raman,可能还有一些其他问题阻碍了regex对模型表单数据的应用。请在问题中添加更多信息(更新/创建控制器操作和视图表单本身)?@lgor Savinkin已编辑。视图中是否有密码标签?因为如果没有,验证程序将被{attribute}
卡住。您可以尝试将消息更改为“密码应仅包含字母数字和下划线。”
。是否尝试修改您的消息?@Dinistro ur谈论返回数组()中的第二行,即数组('Password','match','pattern'=>'/^[a-zA-Z]\w{5,20}$/','message'=>“{attribute}应仅包含字母数字和un