D
D
Dmitry Krivoshein2016-10-16 12:58:49
Yii
Dmitry Krivoshein, 2016-10-16 12:58:49

Yii2 how to load and volition into a form, form fields of another model?

Hello colleagues!
Help solve the problem,
there is a form that implements the AgentUser model:

<?php $form = ActiveForm::begin(); ?>
        <div class="row">
            <div class="col-md-6">
                <?= $form->field($model, 'user_id')->textInput()->hint('Прекрепить пользователя из системы') ?>
            </div>
            <div class="col-md-6">
                <?= $form->field($model, 'relations')->dropDownList($model->getRelations()); ?>
            </div>
        </div>
        <!--   Доп. форма-->
        <div id="is-dogovar" style="display: none"></div>
        <!--   Доп. форма end-->
.....

the form has a "relations" field
private $_relations = [
        0 => 'Без договора',
        1 => 'Договор',
        2 => 'Соглашение',
        3 => 'Друзья',
    ];

if we select "Agreement", then additional form fields from another model should be loaded. i did it the traditional way
<?php
$script = <<< JS

 renderContractForm($('#agentlegal-relations option:selected').val());

$('#agentlegal-relations').on('change', function() {
    var relations = $('#agentlegal-relations option:selected').val();
     renderContractForm(relations);
 });
 
 function renderContractForm(relations) {
     if(relations == 1)
    {   
    	$.post('/accounting/agent-legal/render-contract-form', {is_contract:relations})
    	    .then(function(response) {
                $('#is-dogovar').html(response);
                $('#is-dogovar').fadeIn();  
    	    }, function(error) {
    	    	console.log('error', error);
    	    })
    } else{
    	$('#is-dogovar').empty();
        $('#is-dogovar').fadeOut();
    }
}
JS;
//маркер конца строки, обязательно сразу, без пробелов и табуляции
$this->registerJs($script, yii\web\View::POS_READY);

?>

we send an ajax request to the controller which will render us additional. view
public function actionRenderContractForm()
    {
        if (\Yii::$app->request->isAjax) {

            $is_contract = Yii::$app->request->post('is_contract');
            if ($is_contract) {

                $model_contr = new AgentContract();
                echo $this->renderAjax('_contract_form', ['model_contr' => $model_contr]);
            }
        }
    }

So the problem is that the ActiveForm additional form will generate a nested form tag, Can someone tell me what solutions they can,

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrew, 2016-10-16
@kdes70

Use activeTextInput without a form. Simply display fields in a nested view.
Click
You can display all fields at once, which are unnecessary to hide in display:none; then, if necessary, show it if the fields are not dynamic.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question