本文整理汇总了PHP中app\modules\user\models\User::validate方法的典型用法代码示例。如果您正苦于以下问题:PHP User::validate方法的具体用法?PHP User::validate怎么用?PHP User::validate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类app\modules\user\models\User
的用法示例。
在下文中一共展示了User::validate方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: signup
/**
* Signs user up.
*
* @return User|null the saved model or null if saving fails
*/
public function signup()
{
$auth = Yii::$app->authManager;
$role = $auth->getRole($this->jabatan);
if ($this->validate() && !empty($role)) {
$user = new User();
$user->username = $this->username;
$user->email = $this->email;
$user->setPassword($this->password);
$user->generateAuthKey();
if ($user->validate() && $user->save()) {
$person = new Person();
$person->first_name = $this->first_name;
$person->last_name = $this->last_name;
$person->gender = $this->gender;
$person->birth_date = $this->birth_date;
$person->user_id = $user->id;
if ($person->validate() && $person->save()) {
$auth->assign($role, $user->id);
return true;
}
}
}
return false;
}
示例2: actionCreate
/**
* Creates a new Person model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Person();
$modelUser = new User();
$modelAuthRule = new AuthRule();
$modelAuthItem = new AuthItem();
$authRule = AuthRule::find()->all();
$authItem = AuthItem::find()->all();
if (Yii::$app->request->isPost) {
// do transaction if fails it will not saved
$transaction = Yii::$app->db->beginTransaction();
try {
if ($modelUser->load(Yii::$app->request->post()) && $modelUser->validate()) {
$modelUser->generateAuthKey();
// first attempt save user record
if ($modelUser->save()) {
if ($model->load(Yii::$app->request->post())) {
$model->user_id = $modelUser->id;
// second attemp save person record
if ($model->validate() && $model->save()) {
if ($modelAuthItem->load(Yii::$app->request->post()) && $modelAuthItem->validate()) {
$auth = Yii::$app->authManager;
$role = $auth->getRole($modelAuthItem->name);
if (!empty($role)) {
// thrid attemp assign role to user
$auth->assign($role, $modelUser->id);
$transaction->commit();
Yii::$app->getSession()->setFlash('success', Yii::t('app', 'Data Karyawan Berhasil Disimpan'));
return $this->redirect(['index']);
} else {
throw new \Exception("AuthRole search data checkpoint fail to save");
}
} else {
throw new \Exception("AuthItem (Role) validation checkpoint fail to save");
}
} else {
throw new \Exception("Person save checkpoint fail to save");
}
} else {
throw new \Exception("Person loaded checkpoint fail to save");
}
} else {
throw new \Exception("User save checkpoint fail to save");
}
} else {
throw new \Exception("User validation checkpoint fail to save");
}
} catch (\Exception $e) {
$transaction->rollback();
Yii::$app->getSession()->setFlash('error', Yii::t('app', 'Data Karyawan Gagal Disimpan'));
}
}
return $this->render('create', ['model' => $model, 'modelUser' => $modelUser, 'modelAuthRule' => $modelAuthRule, 'authRule' => $authRule, 'modelAuthItem' => $modelAuthItem, 'authItem' => $authItem]);
}
示例3: actionCreate
public function actionCreate()
{
$user = new User(['scenario' => 'admin-create']);
$profile = new Profile();
$statusArray = User::getStatusArray();
$roleArray = User::getRoleArray();
if ($user->load(Yii::$app->request->post())) {
if ($user->validate()) {
$transaction = Yii::$app->db->beginTransaction();
if ($user->save(false)) {
$profile->user_id = $user->getId();
if ($profile->save(false)) {
$transaction->commit();
return $this->redirect(['index']);
} else {
$transaction->rollback();
print_r($user->getErrors());
return false;
}
} else {
Yii::$app->session->setFlash('danger', 'Ошибка');
return $this->refresh();
}
}
}
// $transaction = Yii::$app->db->beginTransaction();
// if ($user->save())
// {
// $profile->user_id = $user->getId();
// if($profile->save())
// {
// $transaction->commit();
// return $user;
// }
// }
// else
// {
// $transaction->rollback();
// print_r($user->getErrors());
// return false;
// }
return $this->render('create', ['model' => $user, 'statusArray' => $statusArray, 'roleArray' => $roleArray]);
}
示例4: testValidateCorrectData
public function testValidateCorrectData()
{
$model = new User(['username' => 'other_user', 'email' => 'other@example.com', 'status' => 1]);
expect('model is valid', $model->validate())->true();
}