使用 Yii2 活动表单为枚举字段的每个项目插入值



我有一个user模型和一个attachment模型。

用户

id    name     email         password_hash
--------------------------------------
1     Reza     reza@web.af   xxxxxxx

附件

user_id    type(enum)   name
--------------------------------------
1          resume       fk4k34kdfmkg3.pdf
1          photo        59rg3kerfn3ju.jpg
1          nid          34kf2wkefclh0.jpg

我需要为它创建活动表单,我想知道为它们创建模型字段的合适(最佳)方法是什么。 每次注册新用户时,三行附件也会插入到附件表中。当然,下面的活动形式并没有做我想做的事情。请帮忙。

<?php $form = ActiveForm::begin(); ?>
<?=$form->field($user, 'name')->textInput(['maxlength' => true]) ?>
<?=$form->field($user, 'email')->textInput(['maxlength' => true]) ?>
<?=$form->field($user, 'password_hash')->passwordInput(['maxlength' => true]) ?>
<?= $form->field($attachment, 'type')->fileInput()->label('photo') ?>
<?= $form->field($attachment, 'type')->fileInput()->label('resume') ?>
<?= $form->field($attachment, 'type')->fileInput()->label('nid') ?>
..
..

在你的模型中定义三个公共变量,如

public $typePhoto;
public $typeResume;
public $typeNid;

然后定义规则,例如

public function rules()
{
    return [
        [['typePhoto', 'typeResume', 'typeNid'], 'required'],
    ];
}

然后像这样创建活动表单

<?php $form = ActiveForm::begin(); ?>
<?=$form->field($user, 'name')->textInput(['maxlength' => true]) ?>
<?=$form->field($user, 'email')->textInput(['maxlength' => true]) ?>
<?=$form->field($user, 'password_hash')->passwordInput(['maxlength' => true]) ?>
<?= $form->field($attachment, 'typePhoto')->fileInput()->label('photo') ?>
<?= $form->field($attachment, 'typeResume')->fileInput()->label('resume') ?>
<?= $form->field($attachment, 'typeNid')->fileInput()->label('nid') ?>
..
..

请参阅创建表单

我认为为此目的,您应该使用从yii\base\Model扩展的yii模型形式。它可以提供与数据库不同的列。

以下是您需要的表单模型的简化示例:

class LoginForm extends yiibaseModel
{
    public $name;
    public $email;
    public $password_hash;
    public $type_resume;
    public $type_photo;
    public $type_nid;
    public function rules()
    {
        return [
        // define validation rules here
        ];
    }
    public function saveData() 
    {
         $userModel = new User();
         $userModel->name = $this->name;
         //and so on, save all user properties
         $userModel->save();
         $attachModel = new Attachment();
         $attachModel->type = 'resume'; //it better to use constant
         $attachModel->user_id = $userModel->id;
         $attachModel->name = ' '; //set the necessary value
         $attachModel->save();
         //and so on, save your attachment models for all types
    } 
}

所以你的ActiveForm应该是这样的:

<?php $form = ActiveForm::begin(); ?>
<?=$form->field($loginForm, 'name')->textInput(['maxlength' => true]) ?>
<?=$form->field($loginForm, 'email')->textInput(['maxlength' => true]) ?>
<?=$form->field($loginForm, 'password_hash')->passwordInput(['maxlength' => true]) ?>
<?= $form->field($loginForm, 'type_photo')->fileInput()->label('photo') ?>
<?= $form->field($loginForm, 'type_resume')->fileInput()->label('resume') ?>
<?= $form->field($loginForm, 'type_nid')->fileInput()->label('nid') ?>
..

在上面的代码中,$loginForm是 LoginForm 类的对象。 使用 LoginForm 的 saveData 方法正确保存数据。

有关 yii 表单模型的更多信息,请访问 http://www.yiiframework.com/doc-2.0/guide-input-forms.html

最新更新