Yii2 Confirm Password not working - yii2

I am trying to build a signup form, it works fine when I dont use repeat password, it saves to the model, but when I use my repeat password it just does'nt let me save them in the database. My code is below.
SimUser.php file
public function rules()
{
return [
[['user_email', 'user_password_hash','company_id','user_fname', 'user_lname','agree'], 'required'],
['agree','required','requiredValue' => 1, 'message' => ''],
['user_password_hash_repeat', 'compare','compareAttribute' => 'user_password_hash','message' => 'Password don\'t match'],
[['agree'],'safe'],
[['user_password_hash_repeat'],'safe'],
[['user_company', 'user_suspended', 'user_deleted'], 'integer'],
[['user_created'], 'safe'],
[['user_email'], 'string', 'max' => 255],
[['user_password_hash'], 'string', 'max' => 72],
[['user_fname', 'user_lname'], 'string', 'max' => 45],
[['user_auth_key'], 'string', 'max' => 32],
[['user_access_token'], 'string', 'max' => 100],
];
}
My controller action: site/signup
public function actionSignup()
{
$company = new Company();
$company->load(Yii::$app->request->post());
$company->save();
$model = new SimUser();
if ($model->load(Yii::$app->request->post())) {
$model->setPassword($model->user_password_hash);
$model->generateAuthKey();
$model->company_id = $company->company_id;
//var_dump($model); exit();
$model->save();
$model = new LoginForm();
return $this->render('login',[
'model' => $model,
]);
}
return $this->render('signup', [
'model' => $model,
'company' => $company,
]);
}
Here I am saving the comany name in the company model and the others in the user table.
My views file: signup.php
<h1>Sign Up</h1>
<?= $form->field($model, 'user_fname')->textInput(['placeholder'=>'First Name*','class'=>'form-control col-lg-4'])->label(false);?>
<?= $form->field($model, 'user_lname')->textInput(['placeholder'=>'Last Name*','class'=>'form-control col-lg-4'])->label(false); ?>
<?= $form->field($model,'user_email')->textInput(['placeholder'=>'Email*','class'=>'form-control col-lg-4'])->label(false); ?>
<?= $form->field($model, 'user_password_hash')->passwordInput(['placeholder'=>'Password'])->label(false); ?>
<?= $form->field($model, 'user_password_hash_repeat')->passwordInput(['placeholder'=>'Confirm Password*','class'=>'form-control col-lg-4'])->label(false); ?>
<?= $form->field($company, 'company_name')->textInput(['placeholder'=>'Company Name*','class'=>'form-control col-lg-4'])->label(false); ?>
<?php echo $form->field($model, 'agree')->checkbox(); ?>
<div class="form-group">
<?= Html::submitButton('Sign Up', ['class' => 'pull-left padding-0 btn btn-success', 'name' => 'signup-button']) ?>
</div>
The error message for the company doesn't go, it is always there.. attached output as image
Can anyone of you help me solve this issue? Thanks in advance!!
Company.php
*/
public function rules()
{
return [
[['company_name'], 'required'],
[['company_name'], 'string', 'max' => 75],
];
}

In the controller have saved Company in the beginnig itself. So everytime, I load the page It is saving before I could submit it. Hence the error remains as it is.
Code to be changed:
$company = new Company();
$model = new SimUser(['scenario' => SimUser::SCENARIO_REGISTER]);
if ($model->load(Yii::$app->request->post()) && $company->load(Yii::$app->request->post())) {
$company->save();
$model->setPassword($model->user_password_hash);
$model->generateAuthKey();
$model->company_id = $company->company_id;
$model->save();
So 1 issue has been solved.. Unable to solve the confirm password issue! Would be helpful if someone can post what the mistake is?

Related

Yii2 - Two models in one form - Values are not saved in the second model

I have a model called Patientinformation.php and a model called AlternativeInformation.php. I would like to use the two models in the patientinformation_form.php
Patientinformation model:
- id (PK)
- patient_id
- patient_initials
- collection_site
- cancer
- study (FK to study model)
AlternativeInformation model:
- alternative_id (PK)
- patients_patient_id (FK to id of the Patientinformation model)
- alternative_study (FK to study model)
- alternative_patient_id
I have updated the Patientinformation controller as well as the create.php file and the _form.php file.
If I enter values in the Patientinformation form.php, I am redirected to the Patientinformation view site. The values of the Patientinformation model (id, patient_id, patient_initials, collection_site, cancer and study) are saved in the database. However, the values of the AlternativeInformation model (alternative_study and alternative_patient_id) are not saved in the database.
This is the PatientinformationController.php file:
<?php
namespace app\controllers;
use Yii;
use app\models\Patientinformation;
use app\models\AlternativeInformation;
use app\models\PatientinformationSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
/**
* Creates a new Patientinformation model.
* If creation is successful, the browser will be redirected to the 'view' page.
* #return mixed
*/
public function actionCreate()
{
$model = new Patientinformation();
$modelAlternative = new AlternativeInformation();
if ($model->load(Yii::$app->request->post()) && $modelAlternative->load(Yii::$app->request->post())){
$model->save();
$modelAlternative->patients_patient_id = $model->id;
$modelAlternative->save();
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('create', [
'model' => $model,
'modelAlternative' => $modelAlternative,
]);
}
This is the create.php file:
<?php
use yii\helpers\Html;
/* #var $this yii\web\View */
/* #var $model app\models\Patientinformation */
/* #var $modelAlternative app\models\AlternativeInformation */
$this->title = 'Add patient';
$this->params['breadcrumbs'][] = ['label' => 'Patients', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="patientinformation-create">
<h1><?= Html::encode($this->title) ?></h1>
<?= $this->render('_form', [
'model' => $model,
'modelAlternative' => $modelAlternative,
]) ?>
</div>
And this is the _form.php file:
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use app\models\CollectionSiteInformation;
use app\models\AlternativeInformation;
use app\models\StudyInformation;
use app\models\CancerInformation;
use unclead\multipleinput\MultipleInput;
use kartik\select2\Select2;
/* #var $this yii\web\View */
/* #var $model app\models\Patientinformation */
/* #var $modelAlternative app\models\AlternativeInformation */
/* #var $form yii\widgets\ActiveForm */
?>
<div class="patientinformation-form">
<?php $form = ActiveForm::begin(); ?>
<p>
Please note that all fields marked with an asterisk (<font color="red">*</font>) are required.
</p>
<?= $form->field($model, 'study')->widget(Select2::classname(), [
'data' => ArrayHelper::map(StudyInformation::find()->all(),'id','study'),
'language' => 'en',
'options' => ['placeholder' => 'Please select a study'],
]);
?>
<?= $form->field($model, 'patient_id')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'patient_initials')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'collection_site')->widget(Select2::classname(), [
'data' => ArrayHelper::map(CollectionSiteInformation::find()->all(),'id','collection_site'),
'language' => 'en',
'options' => ['placeholder' => 'Please select a collection site'],
]);
?>
<?= $form->field($model, 'cancer')->widget(Select2::classname(), [
'data' => ArrayHelper::map(CancerInformation::find()->all(),'id','cancer'),
'language' => 'en',
'options' => ['placeholder' => 'Please select a cancer type'],
]);
?>
<?= $form->field($modelAlternative,'alternative_study')->widget(MultipleInput::class,[
'max' => 6,
'allowEmptyList' => false,
'enableGuessTitle' => true,
'columns' => [
[
'name' => 'alternative_study',
'type' => 'dropDownList',
'title' => 'Alternative Study',
'items' => ArrayHelper::map(studyInformation::find()->asArray()->all (),'id','study'),
'options' => ['prompt' => 'Please select an alternative study if applicable'],
],
]
])
->label(false);
?>
<?= $form->field($modelAlternative,'alternative_patient_id')->widget(MultipleInput::class,[
'max' => 6,
'allowEmptyList' => false,
'enableGuessTitle' => true,
'columns' => [
[
'name' => 'alternative_patient_id',
'title' => 'Alternative patient ID',
'options' => ['prompt' => 'Please select an alternative study if applicable'],
],
]
])
->label(false);
?>
<div class="form-group">
<?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
If I don`t use unclead\multipleinput\MultipleInput in my _form.php, all the values of AlternativeInformation model (alternative_study and alternative_patient_id) are saved into the model/database.
So I assume that I don`t pass any items to the MultipleInput widget.
This is the AlternativeInformation model file:
/**
* {#inheritdoc}
*/
public function rules()
{
return [
[['patients_patient_id', 'alternative_study'], 'required'],
[['patients_patient_id', 'alternative_study'], 'integer'],
[['alternative_patient_id'], 'string', 'max' => 265],
[['patients_patient_id'], 'exist', 'skipOnError' => true, 'targetClass' => PatientInformation::className(), 'targetAttribute' => ['patients_patient_id' => 'id']],
[['alternative_study'], 'exist', 'skipOnError' => true, 'targetClass' => StudyInformation::className(), 'targetAttribute' => ['alternative_study' => 'id']],
];
}
/**
* {#inheritdoc}
*/
public function attributeLabels()
{
return [
'alternative_id' => 'Alternative ID',
'patients_patient_id' => 'Patients Patient ID',
'alternative_study' => 'Alternative Study',
'alternative_patient_id' => 'Alternative Patient ID',
];
}
/**
* #return \yii\db\ActiveQuery
*/
public function getPatientsPatient()
{
return $this->hasOne(PatientInformation::className(), ['id' => 'patients_patient_id']);
}
/**
* #return \yii\db\ActiveQuery
*/
public function getAlternativeStudy()
{
return $this->hasOne(StudyInformation::className(), ['id' => 'alternative_study']);
}
}
Does anyone have any suggestions how this issue can be solved? How will be the values of "alternative_study" and "alternative_patient_id" saved into the AlternativeInformation model if using the MultipleInput widget?
I really need the option in the form to enter no, one or several alternative studies as well as alternative patient ids.
I really appreciate any help how I can solve this issue.
Thanks :)
You are probably getting some validation errors that you can't see because you are redirecting to the view action even if the AlternativeInformation model has validation errors.
The first step towards finding a solution would be making sure that, if AlternativeInformation doesn't save you show the form again, that way you will see the validation errors.
Update your controller code to this:
public function actionCreate()
{
$model = new Patientinformation();
$modelAlternative = new AlternativeInformation();
if ($model->load(Yii::$app->request->post())
&& $modelAlternative->load(Yii::$app->request->post())
&& $model->save()) {
$modelAlternative->patients_patient_id = $model->id;
if ($modelAlternative->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
}
return $this->render('create', [
'model' => $model,
'modelAlternative' => $modelAlternative,
]);
}
That should help you find the problem, it will also give your users information about any future errors that may happen.
The validation problem may be on alternative_patient_id, it seems that you are not passing any items to the MultipleInput widget.
Update your controller with the following code.
public function actionCreate()
{
$model = new Patientinformation();
$modelAlternative = new AlternativeInformation();
if ($model->load(Yii::$app->request->post()) && $modelAlternative->load(Yii::$app->request->post())){
if($model->save()){
$modelAlternative->patients_patient_id = $model->id;
$modelAlternative->save();
return $this->redirect(['view', 'id' => $model->id]);
}
}
return $this->render('create', [
'model' => $model,
'modelAlternative' => $modelAlternative,
]);
}

Yii2 create or update user profile

In my basic app, I have a User model and a UserForm model that stores and update the users in the database. Now, I got problem when updating the user profile. It feedbacks 'Successful update!' even though the new data won't save.
My table:
Table name: User
Attributes: id, fname, mname, lname, address, username, auth_key, password_hash, password_reset_token, email, status, created_at, updated_at
My Model:
class UserForm extends Model
{
public $fname;
public $mname;
public $lname;
public $address;
public $username;
public $email;
public $password_hash;
/**
* #inheritdoc
*/
public function rules()
{
return [
['fname', 'required'],
['fname', 'trim'],
['mname', 'trim'],
['lname', 'required'],
['lname', 'trim'],
['address', 'required'],
['address', 'trim'],
['address', 'string', 'max' => 255],
['username', 'trim'],
['username', 'required'],
['username', 'unique', 'targetClass' => '\app\models\User', 'message' => 'This username has already been taken.'],
['username', 'string', 'min' => 4, 'max' => 255],
['email', 'trim'],
['email', 'required'],
['email', 'email'],
['email', 'string', 'max' => 255],
['email', 'unique', 'targetClass' => '\app\models\User', 'message' => 'This email address has already been taken.'],
['password_hash', 'required', 'on' => 'insert'],
['password_hash', 'string', 'min' => 8],
];
}
public function beforeSave($insert) {
if ($insert) {
$this->setPassword($this->password_hash);
} else {
if (!empty($this->password_hash)) {
$this->setPassword($this->password_hash);
} else {
$this->password_hash = (string) $this->getOldAttribute('password_hash');
}
}
return parent::beforeSave($insert);
}
}
My controller:
public function actionUpdate($id)
{
$model = $this->findModel($id);
$model->setAttribute('password_hash', null);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
Yii::$app->session->setFlash('success', Yii::t('app', 'Successful update!'));
return $this->redirect(['overview']);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
protected function findModel($id) {
if (($model = User::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
My view.php
<?= $form->field($model, 'fname')->textInput(['autofocus' => true])->label('First Name') ?>
<?= $form->field($model, 'mname')->textInput()->label('Middle Name') ?>
<?= $form->field($model, 'lname')->textInput()->label('Last Name') ?>
<?= $form->field($model, 'address')->textInput() ?>
<?= $form->field($model, 'username')->textInput() ?>
<?= $form->field($model, 'email') ?>
<?= $form->field($model, 'password_hash')->passwordInput(['placeholder' => Yii::t('app', 'Password'), 'value' => ''])->label(false) ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
Some of the steps are from here: yii2 user CRUD create or update with password
Your UserForm is never used. Your findModel() returns User, so all validation and saving is handled by User model - I guess it does not have any validation rules, so $model->load() does not load anything.
You may try to extend your form model from active record model:
class UserForm extends User
And use it in findModel() or create separate method for returning form model:
protected function findFormModel($id) {
if (($model = UserForm::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
Then use this method in your update action. Don't forget to remove old properties from UserForm - they're unnecessary and may break attributes behavior.

Yii2 client side validation refreshes the page on form submit

I am trying to perform client side check but the page is being refreshed. My rules looks like:
public function rules()
{
return [
[['email'], 'required'],
[['email','cron'], 'string'],
[['email'], 'string', 'max' => 100],
[['cron', 'group', 'type'], 'integer'],
[['email'], 'unique'],
['group', 'required', 'when' => function($model){
return $model->type == 1;
}, 'whenClient' => 'function(attribute, value){
return 1==2;
}']
];
}
The form enableClientValidation is switched to true like this :
<?php $form = ActiveForm::begin([
'enableClientValidation' => true
]) ?>
Where is my mistake ? Thank you in advance!
You need to set enableAjaxValidation property of AcitveForm:
$form = ActiveForm::begin([
'id' => 'form-id',
'enableAjaxValidation' => true,
]);
Controller
if ($model->load(Yii::$app->request->post())) {
if(Yii::$app->request->isAjax) {
return $this->asJson(ActiveForm::validate($model));
}
}
Yii2 Ajax Validation
asJson()
<?php $form = ActiveForm::begin([
'id' => 'form',
'enableAjaxValidation' => true
]) ?>

Yii2 Automatically Login after Registration

I'm creating a website using yii2 framework. I have a problem in registration. I have a modal in home and it contains the sign up form. Now when i trying to register, Yes it saved successful but it only stay in the modal. Now i want is after registering it will automatically login.
This is my sign up form_:
<div class="row">
<div class="col-lg-12">
<?php yii\widgets\Pjax::begin(['id' => 'sign-up']) ?>
<?php $form = ActiveForm::begin(['id' => 'form-signup', 'options' => ['data-pjax' => true]]); ?>
<div class="row">
<div class="col-sm-6">
<?= $form->field($model, 'role')->dropDownList(['2' => 'User', '1' => 'Encoder', '3' => 'Admin']) ?>
<?= $form->field($model, 'username')->textInput(['placeholder' => 'Username....']) ?>
<?= $form->field($model, 'email')->textInput(['placeholder' => 'Email....']) ?>
<?= $form->field($model, 'password')->passwordInput(['placeholder' => 'Password.....']) ?>
</div>
<div class="col-sm-6">
<?= $form->field($model, 'confirmPassword')->passwordInput(['placeholder' => 'Confirm Password.....']) ?>
<?= $form->field($model, 'first_name')->textInput(['placeholder' => 'First Name....']) ?>
<?= $form->field($model, 'middle_name')->textInput(['placeholder' => 'Middle Name....']) ?>
<?= $form->field($model, 'last_name')->textInput(['placeholder' => 'Last Name....']) ?>
</div>
</div>
<center>
<?= $form->field($model, 'verifyCode')->widget(Captcha::className()) ?>
</center>
<div class="form-group">
<?= Html::submitButton('Signup', ['class' => 'btn btn-primary', 'name' => 'signup-button', 'style' => 'width: 100%; padding: 10px;']) ?>
</div>
<?php ActiveForm::end(); ?>
<?php yii\widgets\Pjax::end() ?>
</div>
</div>
This is my model:
class SignupForm extends Model
{
public $role;
public $username;
public $email;
public $password;
public $first_name;
public $middle_name;
public $last_name;
public $confirmPassword;
public $verifyCode;
public function rules()
{
return [
['role', 'required'],
['username', 'trim'],
['username', 'required'],
['username', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This username has already been taken.'],
['username', 'string', 'min' => 2, 'max' => 20],
['email', 'trim'],
['email', 'required'],
['email', 'email'],
['email', 'string', 'max' => 30],
['email', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This email address has already been taken.'],
['password', 'required'],
['password', 'string', 'min' => 6],
['first_name', 'trim'],
['first_name', 'required'],
['middle_name', 'trim'],
['middle_name', 'required'],
['last_name', 'trim'],
['last_name', 'required'],
['verifyCode', 'captcha'],
['verifyCode', 'required'],
[['confirmPassword'], 'compare', 'compareAttribute' => 'password', 'message' => 'Passwords do not match.'],
];
}
/**
* Signs user up.
*
* #return User|null the saved model or null if saving fails
*/
public function signup()
{
if (!$this->validate()) {
return null;
}
$user = new User();
$user->username = $this->username;
$user->email = $this->email;
$user->setPassword($this->password);
$user->generateAuthKey();
$user->role = $this->role;
$user->first_name = $this->first_name;
$user->middle_name = $this->middle_name;
$user->last_name = $this->last_name;
return $user->save() ? $user : null;
}
}
This is my controller:
public function actionSignup()
{
$model = new SignupForm();
if ($model->load(Yii::$app->request->post())) {
if ($user = $model->signup()) {
if (Yii::$app->getUser()->login($user)) {
return $this->goHome();
}
}
}
return $this->renderAjax('signup', [
'model' => $model,
]);
}
I don't have any ideas, I think my codes is correct but i don't know why is not working.
UPDATED
When i clicked the button signup it stay only in the modal and when i clicked it again the button the validations is working. It means it saves to database but not automatically login.
you should get the user identity for login
public function actionSignup()
{
$model = new SignupForm();
if ($model->load(Yii::$app->request->post())) {
if ($user = $model->signup()) {
$identity = User::findOne(['username' => $model->$username]);
if (Yii::$app->user->login($identity)) {
return $this->goHome();
}
}
}
return $this->renderAjax('signup', [
'model' => $model,
]);
}
see this for more http://www.yiiframework.com/doc-2.0/guide-security-authentication.html
http://www.yiiframework.com/doc-2.0/yii-web-user.html
I have used below code and its working for me . Its also a standard code for YII2 .Controller Action Code :
public function actionSignup()
{
$model = new SignupForm();
if ($model->load(Yii::$app->request->post())) {
if ($user = $model->signup()) {
$model= \common\models\User::findOne([$user->id]); //you need to get complete model again and pass it to login function
if (Yii::$app->user->login($model) {
return $this->goHome();
}
}
}
return $this->renderAjax('signup', [
'model' => $model,
]);
}
After couple minutes of step by step debug - it works well
//Signup
public function actionSignup()
{
$model = new SignupForm();
if ($model->load(Yii::$app->request->post()) && $model->signup()) { /*Save
to DB first*/
$genmail = $model->email; //get model email value
$identity = User::findOne(['email' => $genmail]); //find user by email
if (Yii::$app->user->login($identity)) { // login user
return $this->redirect('account'); // show accaount page
}
}
return $this->render('signup', [
'model' => $model,
]);
}

Required either one filed in Yii2 ActiveForm

I have a form Message
In which there are 2 fields message and file
I want the validator to validate either one of them field as required
Here is what i have done until now but its not working
public function rules()
{
return [
[['sender_id', 'receiver_id'], 'integer'],
[['message'], 'string'],
[['message','file'],'my_required'],
[['is_delivered', 'is_notified', 'is_deleted', 'is_group'], 'boolean'],
[['created_date', 'updated_date'], 'safe'],
[['image'], 'string', 'max' => 500],
[['file'], 'file', 'extensions'=>'jpg, gif, png, pdf'],
];
}
public function my_required($attribute_name, $params)
{
if (empty($this->file)
&& empty($this->message)
) {
$this->addError($attribute_name, Yii::t('user', 'At least 1 of the field must be filled up properly'));
return false;
}
return true;
}
Here is the code of my form
<div class="row">
<p>
<?php $form = ActiveForm::begin(['options'=>['enctype'=>'multipart/form-data','id' => 'message-form']]); ?>
<div class="form-group col-xs-3 col-lg-3">
<?= $form->field($model, 'message')->textarea(['rows' => 6]) ?>
</div>
<div class="form-group col-xs-3 col-lg-3">
<?= $form->field($model, 'file')->fileInput() ?>
<?= $form->field($model, 'keys')->hiddenInput()->label(false); ?>
<div class="form-group">
<?= Html::submitButton('Send',['class' => 'btn btn-danger','data-placement'=>'right','id'=>'sendMessage']) ?>
</div>
</div>
<?php ActiveForm::end(); ?>
</p>
</div>
I also tried to use this but it didnt work as well
public function rules()
{
return [
[['sender_id', 'receiver_id'], 'integer'],
[['message'], 'string'],
[['is_delivered', 'is_notified', 'is_deleted', 'is_group'], 'boolean'],
[['created_date', 'updated_date'], 'safe'],
[['image'], 'string', 'max' => 500],
[['file'], 'file', 'extensions'=>'jpg, gif, png, pdf'],
['file', 'required', 'when' => function($model) {
return empty($model->message);
}],
];
}
I have also tried using inline validation like this
['file', function ($attribute,$model) {
if (empty($model->message)) {
$this->addError($attribute, 'Form must contain a file or message.');
}
}],
But that did not work out either...
Below works perfectly for me:
['clientGroupId', 'required', 'when' => function($model) {
return empty($model->clientId);
}, 'message' => 'Client group or client selection is required'],