Search code examples
yii2active-form

Yii2 Loading and saving ActiveForm


Model:

class Team extends ActiveRecord {
    public function rules() {
        return [
            [['code', 'name'], 'required'],
        ];
    }
}

Controller:

class TeamController extends ActiveController {
    public $modelClass = 'frontend\models\Team';
    protected function verbs(){
        return [
            'create' => ['POST'],
        ];
    }
}

View:

<?php Pjax::begin(['id' => 'new_team']) ?>
<?php $form = ActiveForm::begin(['action' => 'team/create'], 'options' => ['data-pjax' => true ]]); ?>
    <?= $form->field($model, 'code')->textInput() ?>
    <?= $form->field($model, 'name')->textInput() ?>
    <div class="form-group">
        <?= Html::submitButton(Yii::t('app', 'Create'), ['class' => 'btn btn-success']) ?>
    </div>
<?php ActiveForm::end(); ?>
<?php Pjax::end(); ?>

Form's fields will have Team[code] and Team[name] as name attribute values. I would like them to be name="code" and name="name" instead. At the moment when I submit the form I get:

[{"field":"code",
  "message":"Code cannot be blank."},
 {"field":"name",
  "message":"Name cannot be blank."}]

How can I change name attribute's value?


Solution

  • The current format of the name attribute Team[code] is because YiiActiveForm uses the Model Information to generate the input names in the format ModelName[FieldName] this helps to reduce the effort to manually assigning the post fields to the model fields before saving and if you do not want the form field names to follow ActiveForm format you need to use yii\helpers\Html helper to create the form and fields but be careful as you will need to map the field to hte model manually now so what you have to do is change your form declaration from

    <?php $form = ActiveForm::begin(['action' => 'team/create'], 'options' => ['data-pjax' => true ]]); ?>
        <?= $form->field($model, 'code')->textInput() ?>
        <?= $form->field($model, 'name')->textInput() ?>
        <div class="form-group">
            <?= Html::submitButton(Yii::t('app', 'Create'), ['class' => 'btn btn-success']) ?>
        </div>
    <?php ActiveForm::end(); ?>
    

    To

    <?= yii\helpers\Html::beginForm(['team/create', 'id' => 'team-form'], 'post', ['data-pjax' => true]) ?>
         <?= yii\helpers\Html::activeInput('text',$model, 'code')?>
         <?= yii\helpers\Html::activeInput('text',$model, 'name')?>
        <div class="form-group">
            <?= Html::submitButton(Yii::t('app', 'Create'), ['class' => 'btn btn-success']) ?>
        </div>
    <?= Html::endForm() ?>
    

    EDIT

    As per discussion the OP had to revert back to ActiveForm and was trying to save record but having a problem so I thought I might add this info in the answer maybe it would help out someone.

    in order to use ActiveForm when you submit your form to any controller/action you need to look there that if you are properly loading the model with the post array using

    $model->load(Yii::$app->request->post()) 
    

    and then call $model->save() or $model->validate(), apart from this try print_r($model->attributes) right after you call $model->load() to check if the values are properly loaded into the model.