Answer the question
In order to leave comments, you need to log in
Yii2, working with forms, how to write a more complex form?
Still digging yii2, now I want to create a contact forum. There are plenty of examples everywhere:
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'name') ?>
<?= $form->field($model, 'email') ?>
<?= $form->field($model, 'subject') ?>
<?= $form->field($model, 'body')->textArea(['rows' => 6]) ?>
<?= $form->field($model, 'verifyCode')->widget(Captcha::className(), [
'captchaAction' => '/site/default/captcha',
'options' => ['class' => 'form-control'],
'template' => '<div class="row"><div class="col-lg-3">{image}</div><div class="col-lg-9">{input}</div></div>',
]) ?>
<?= Html::submitButton(Yii::t('app', 'Отправить'), ['class' => 'btn btn-primary']) ?>
<?php ActiveForm::end(); ?>
<form action="/" method="get" id="contact_form">
<div class="clearfix">
<div class="form-col form-marg small fl-left">
<label>Name<span>*</span></label>
<div class="field"><input class="form-item req" id="contact_name" name="contact_name" type="text"></div>
</div>
<div class="form-col small fl-left">
<label>Email<span>*</span></label>
<div class="field"><input class="form-item req" name="email" id="email" type="email"></div>
</div>
</div>
<div class="form-col">
<label>Message<span>*</span></label>
<textarea id="comment" name="comment" class="form-item req"></textarea>
</div>
<div class="form-btn">
<div class="field"><input class="btn" name="submit" id="submit" value="send message" type="submit"></div>
</div>
</form>
<div class="form-col small fl-left">
<label>Email<span>*</span></label>
<div class="field"><input class="form-item req" name="email" id="email" type="email"></div>
</div>
<?= $form->field($model, 'email') ?>
<div class="form-col form-marg small fl-left">
<div class="form-group field-contactform-email required">
<label class="control-label" for="contactform-email">E-mail адрес</label>
<input type="text" id="contactform-email" class="form-control" name="ContactForm[email]">
<div class="help-block"></div>
</div>
</div>
<?= $form->field($model, 'email') ?>
<div class="form-col small fl-left">
мой label с моими классами
<div class="field">мое текстовое поле с моими классами</div>
<div class="error">Блок ошибок к текстовому полю</div>
</div>
Answer the question
In order to leave comments, you need to log in
Oh.
Well, in general, look towards the fieldConfig parameter (passed when creating the form widget, here <?php $form = ActiveForm::begin(); ?>), well, there are all sorts of options, cssOptions, template, etc.
Example:
$form = ActiveForm::begin([
'id' => 'login-form',
'options' => ['class' => 'form-horizontal'],
'fieldConfig' => [
'template' => '{label}<div class="col-sm-10">{input}</div><div class="col-sm-10">{error}</div>',
'labelOptions' => ['class' => 'col-sm-2 control-label'],
],
]);
I also found this example in the docs:
<?= Html::activeLabel($model, 'password') ?>
<?= Html::activePasswordInput($model, 'password') ?>
<?= Html::error($model, 'password') ?>
or
<?= Html::activeLabel($model, 'username', ['label' => 'name']) ?>
<?= Html::activeTextInput($model, 'username') ?>
<div class="hint-block">Please enter your name</div>
<?= Html::error($model, 'username') ?>
<?php $form = ActiveForm::begin(['id' => 'contact_form']); ?>
<div class="clearfix">
<div class="form-col form-marg small fl-left">
<?= Html::activeLabel($model, 'name') ?>
<div class="field"><?= Html::activeTextInput($model, 'name', ['class'=>'form-item req']) ?></div>
<?= Html::error($model, 'name') ?>
<div class="hint-block"></div>
</div>
<div class="form-col form-marg small fl-left">
<?= Html::activeLabel($model, 'email') ?>
<div class="field"><?= Html::activeTextInput($model, 'email', ['class'=>'form-item req']) ?><div class="hint-block"></div></div>
<?= Html::error($model, 'email') ?>
<div class="hint-block"></div>
</div>
</div>
<div class="form-col">
<?= Html::activeLabel($model, 'body') ?>
<?= Html::activeTextarea($model, 'body', ['class'=>'form-item req']) ?>
<?= Html::error($model, 'body') ?>
<div class="hint-block"></div>
</div>
<div class="clearfix">
<div class="form-col form-marg small fl-left">
<?= $form->field($model, 'verifyCode')->widget(Captcha::className(), [
'captchaAction' => '/site/default/captcha',
'options' => ['class' => 'form-item req'],
'template' => '<div class="row"><div class="col-lg-3">{image}</div><div class="col-lg-9"><div class="field">{input}</div></div></div>',
]) ?>
</div>
</div>
<div class="form-btn">
<div class="field"><?= Html::submitButton(Yii::t('app', 'Отправить'), ['class' => 'btn btn-primary']) ?></div>
</div>
<?php ActiveForm::end(); ?>
By the way, here's another interesting option. I haven't tried it myself yet, but what if?..
https://github.com/yiisoft/yii2/issues/2580
See the last comment from the creator.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question