1
votes

I just started my first web app with Yii2. Had some problem with active form validation.

In my Model class, i have a set of rule.

ProfileForm.php

class ProfileForm extends Model
{
    public $email;
    public $lastName;
    public $firstName;
    public $password;
    public $phone;
    public $address;

    public function rules()
        {
            return [
                [['email', 'firstName', 'lastName', 'address'], 'required'],
            ];
        }

UserController

public function actionUpdateProfile()
{
    $model = new ProfileForm();

    if ($model->load(Yii::$app->request->post()) && $model->validate()) {
        $user = User::findOne(Yii::$app->user->id);
        if ($user != NULL) {
            $user->email = $model->email;
            $user->firstName = $model->firstName;
            $user->lastName = $model->lastName;
            $user->address = $model->address;
            $user->phone = $model->phone;
            $user->save();
        }
    }   
}

public function actionProfile()
{
        $model = User::findOne(Yii::$app->user->id);
        return $this->render('profile', [
            'model' => $model,
        ]); 
}

view profile.php

<?php
    $form = ActiveForm::begin([
        'id' => 'profile-form',
        'action' => ['user/update-profile'],
        'options' => [],
        'fieldConfig' => [],
    ]);
?>
<div class="form-group">
    <label class="col-sm-3 control-label">First Name</label>
    <div class="col-sm-9">
        <?= $form->field($model, 'firstName')->textInput([
            'placeholder' => $model->attributeLabels()['firstName']
            ])->label(false) ?>
    </div>
</div>
..
..
..
..
<div class="form-group">
    <div class="col-sm-offset-3 col-sm-9">
        <?= Html::submitButton(Yii::t('app', 'Updated'), ['class' => 'btn btn-default', 'name' => 'profile-button']) ?>
    </div>
</div>
<?php ActiveForm::end(); ?>

I want ProfileForm model class to validate the value before updating the actual model class. But the form validation does not trigger when i inserted empty value to fields in the view class.

2
Do you mean client side validation or server side is not triggered? - Yupik
client side validation - yhtan
For start remove options' => [], 'fieldConfig' => [] - Yupik
you can also change the manual assignment of all the fields from Profile to User in a single line i.e $user->attributes=$model->attributes - Muhammad Omer Aslam
@Yupik removing that does not work. - yhtan

2 Answers

2
votes

You're calling wrong model to actionProfile():

public function actionProfile()
{
        $model = User::findOne(Yii::$app->user->id);  //here 
        return $this->render('profile', [
            'model' => $model,
        ]); 
}

You're calling User::findOne(), so you can't expect that rules from ProfileForm will work. If you wan't to use ProfileForm, you can extend your User model in ProfileForm class, like:

class ProfileForm extends User   // here your `User` model instead of `Model`

^remember about namespaces^

then change actionProfile() to:

public function actionProfile()
{
        $model = ProfileForm::findOne(Yii::$app->user->id);  //here ProfileForm now 
        return $this->render('profile', [
            'model' => $model,
        ]); 
}

==========================

Remember, if you're going to extend User by ProfileForm you have to remove your attributes definitions:

public $email;
public $lastName;
public $firstName;
public $password;
public $phone;
public $address;
0
votes
 public function actionUpdateProfile() {
        $model = ProfileForm::find()->where(['id' =>Yii::$app->user->id])->one();
        if (Yii::$app->request->post()) {
            $model->load(Yii::$app->request->post());
            if ($model->validate()) {
                $updateId = $model->update(Yii::$app->user->id)
            } 
        }
        return $this->render('profile', [
                    'model' => $model
        ]);
    }