簡體   English   中英

Yii2表格驗證

[英]Yii2 Form validation

我剛剛使用Yii2啟動了我的第一個Web應用程序。 活動表單驗證存在一些問題。

在我的Model課上,我有一組規則。

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,
        ]); 
}

查看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(); ?>

我希望ProfileForm模型類在更新實際模型類之前先驗證值。 但是,當我向視圖類中的字段插入空值時,表單驗證不會觸發。

您正在將錯誤的模型actionProfile()

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

您正在調用User::findOne() ,所以您不能指望ProfileForm 規則會起作用。 如果您不想使用ProfileForm ,則可以在ProfileForm類中擴展User模型,例如:

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

^記住名稱空間^

然后將actionProfile()更改為:

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

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

請記住,如果要通過ProfileForm擴展User則必須刪除屬性定義

public $email;
public $lastName;
public $firstName;
public $password;
public $phone;
public $address;
 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
        ]);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM