Answer the question
In order to leave comments, you need to log in
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-->
.....
private $_relations = [
0 => 'Без договора',
1 => 'Договор',
2 => 'Соглашение',
3 => 'Друзья',
];
<?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);
?>
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]);
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question