Php 使用ajax unique Yi2验证默认用户名和电子邮件时,请避开它
在更新用户信息时,如何避开用户当前的用户名和密码?例如,当我尝试只更新名称时,它会显示电子邮件已被接收,反之亦然。这样我就不能只更新邮件,也不能只更新电子邮件,只能同时更新它们。 我的行动更新:Php 使用ajax unique Yi2验证默认用户名和电子邮件时,请避开它,php,yii2,Php,Yii2,在更新用户信息时,如何避开用户当前的用户名和密码?例如,当我尝试只更新名称时,它会显示电子邮件已被接收,反之亦然。这样我就不能只更新邮件,也不能只更新电子邮件,只能同时更新它们。 我的行动更新: public function actionUpdate() { $model = new UpdateForm(); $id = \Yii::$app->user->identity->id; $user = $this->
public function actionUpdate()
{
$model = new UpdateForm();
$id = \Yii::$app->user->identity->id;
$user = $this->findModel($id);
//default values
$model->username = $user->username;
$model->email = $user->email;
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ActiveForm::validate($model);
}
if (isset($_POST['UpdateForm']))
{
$model->attributes = $_POST['UpdateForm'];
if($model->validate())
{
$user->username = $model->username;
$user->email = $model->email;
$user->password = md5($model->password);
$user->update();
$this->redirect(['view', 'id' => $user->id]);
}
}
else
{
return $this->render('update', [
'model' => $model,
]);
}
}
我的UpdateForm规则:
public function rules()
{
return [
[['email', 'password', 'username'], 'required'],
[['email', 'password', 'username'], 'string', 'max' => 50],
[['image'], 'string', 'max' => 255],
[['username', 'password', 'email'], 'trim'],
['email', 'email'],
[[ 'email', 'username'], 'unique'],
];
}
提前谢谢你 两种方法都应用于唯一 例如: 就你而言
[[ 'email', 'username'], 'unique', 'targetAttribute' => ['email', 'username']],
如果要一次更新一个字段。您需要删除必需的字段。然后,他检查3必须存在。删除此行:
[['email', 'password', 'username'], 'required'],
当我第一次读它的时候,我很难理解这个规则,也许是因为语言障碍,但现在我明白了!非常感谢你!好啊这是有帮助的,但现在似乎我不能只更新密码,而不改变其中一个用户名或电子邮件。这是我的坏,因为我没有注意到我有3个字段用户名,电子邮件,密码。应该怎么做呢?再次感谢你,我很抱歉,但我仍在学习,有时会有点困惑:)你说“我有三个领域”是什么意思??你是说3个独一无二的。。解释一个新的描述良好的具体问题。。(不要改变这一点,否则会让人无法理解……)好的!将提出新问题。非常感谢。
[['email', 'password', 'username'], 'required'],