Component with form file

Hello!

I’m new here (sort of), and I haven’t posted yet, though, I have this thing I’ve been developing myself locally and I wonder if this is of any interest of you guys.

I’m working on something I like to call componentization of forms.

I’ve been through the documentation when it connects to how you could implement two models forms coming from one view and I didn’t like it very much, so I came up with this idea.

I moved the fields to their own file, so I created a file _fields within the scope of the views for a certain model, and now the _form render the _fields.

In the fields file I have this code:


<?php

/* @var $this yii\web\View */

/* @var $model app\models\Picture */

/* @var $form yii\widgets\ActiveForm */

?>


<div class="picture-form-fields">


    <?= $form->field($model, 'image')->fileInput() ?>


    <?= $form->field($model, 'picture_type_id')->textInput() ?>


    <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>


    <?= $form->field($model, 'alt')->textInput(['maxlength' => true]) ?>


    <?= $form->field($model, 'active')->checkbox() ?>


</div>



In the form file I have this code:


<?php


use yii\helpers\Html;

use yii\widgets\ActiveForm;


/* @var $this yii\web\View */

/* @var $model app\models\Picture */

/* @var $form yii\widgets\ActiveForm */

?>


<div class="picture-form">


    <?php $form = ActiveForm::begin(); ?>


    <?= $this->render('_fields', [

        'form' => $form,

        'model' => $model,

    ]) ?>


    <div class="form-group">

        <?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>

    </div>


    <?php ActiveForm::end(); ?>


</div>



So I came here to ask you guys what do you think about it? Do you like it? Is there a problem using this I didn’t account for?

Any feedback would be nice. Thank you!

Oh, create/update files were not changed. My idea with this changes were, really, to make it easier to include fields from one model into another without much more trouble, so in this way we don’t need to include manually, field by field, so we just include a series of fields that are working somewhere else, allowing the validation to work in both places.