Yii2表格验证

时间:2018-02-13 07:54:46

标签: php yii yii2

我刚用Yii2开始了我的第一个网络应用程序。活动表单验证存在一些问题。

在我的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模型类在更新实际模型类之前验证该值。但是当我将空值插入视图类中的字段时,表单验证不会触发。

2 个答案:

答案 0 :(得分:2)

您将错误的型号称为$('.btn-counter').on('click', function(event, count) { event.preventDefault(); var $this = $(this), count = $this.attr('data-count'), active = $this.hasClass('active'), multiple = $this.hasClass('multiple-count'); $.fn.noop = $.noop; $this.attr('data-count', ! active || multiple ? ++count : --count ) [multiple ? 'noop' : 'toggleClass']('active'); });

actionProfile()

您正在拨打public function actionProfile() { $model = User::findOne(Yii::$app->user->id); //here return $this->render('profile', [ 'model' => $model, ]); } ,因此您无法预期来自User::findOne()规则会有效。如果您不想使用ProfileForm,可以在ProfileForm课程中扩展User模型,例如:

ProfileForm

^记住命名空间^

然后将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, ]); } 延长User,则必须删除属性定义

ProfileForm

答案 1 :(得分:0)

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