esp
(Adrian Drozdz)
November 1, 2014, 11:44pm
1
Hi I have another issue and I hope you can help me understand why it happens.
Anyway.
I genereate typicall CRUD via gii and when _form.php is created and displayed like this
<div class="pupil-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'name')->textInput(['maxlength' => 45]) ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
the validation script
<script src="/assets/51b9ca2f/yii.validation.js">
is included in the cenerated html
hower to have more control on form layout I use the following code
<div class="teacher-form">
<?php $form = ActiveForm::begin([
'id' => 'login-form',
'options' => ['class' => 'form-horizontal'],
'enableAjaxValidation' => true,
]); ?>
<div class="form-group">
<?= Html::activeLabel($model, 'name', ['class'=>'col-xs-1 control-label']); ?>
<div class="col-xs-5">
<?= Html::activeInput('text',$model,'name',['class'=>'form-control','maxlength' => 45]); ?>
<?= Html::error($model,'name',['class'=>'help-block']); ?>
</div>
</div>
<div class="form-group">
<div class="col-xs-12 text-right">
<?= Html::submitButton($model->isNewRecord ? 'Dodaj' : 'Zapisz', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
</div>
<?php ActiveForm::end(); ?>
</div>
the validation script is not included. Do you have maybe some knowledge on this?
KASTINpl
(Kastin)
November 2, 2014, 9:39am
2
"yii.validation.js" is added by yii\validators\ValidationAsset registered by validators when using ActiveField with $model.
The simplest way is to use ActiveForm::field:
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'name')->textInput(['maxlength' => 45]) ?>
to asset "yii.validation.js" correctly.
My solution to customise form html is to extend ActiveForm or ActiveField and overwrite $template
class MyActiveField extends \yii\bootstrap\ActiveField {
/**
* @var string
*/
public $template = "<fieldset><legend>{label}</legend>\n{input}\n{hint}\n{error}</fieldset>";
public $myTemplate = '<div class="form-group">{label}<div class="col-xs-5">{input}{error}</div></div>';
public function myInput($options = []) {
$this->template = $this->myTemplate;
return parent::textInput($options);
}
}
then code:
<?= $form->field($model, 'name')->myInput(['maxlength' => 45]) ?>
should do the work.
esp
(Adrian Drozdz)
November 2, 2014, 11:12am
3
Thank you so much for the answer. I understand that I have to create a template for a field. But I really don’t imagine using it instead of form-group with f.e. two form controls. I attach a specific form.
esp
(Adrian Drozdz)
November 2, 2014, 10:48pm
4
<div class="form-group">
<?= $form->field($model,'name')->begin(); ?>
<?= Html::activeLabel($model,'name',['class'=>'col-xs-1 control-label']); ?>
<div class="col-xs-5">
<?= Html::activeInput('text',$model,'name',['class'=>'form-control','maxlength' => 45]); ?>
<?= Html::error($model,'name',['class'=>'help-block']); ?>
</div>
<?= $form->field($model,'name')->end(); ?>
<?= $form->field($model,'surname')->begin(); ?>
<?= Html::activeLabel($model,'surname',['class'=>'col-xs-1 control-label']); ?>
<div class="col-xs-5">
<?= Html::activeInput('text',$model,'surname',['class'=>'form-control','maxlength' => 45]); ?>
<?= Html::error($model,'name',['class'=>'help-block']); ?>
</div>
<?= $form->field($model,'surname')->end(); ?>
</div>
Does the trick I needed with validation but the begin() method adds div wrapper with class form-group for each field and makes it 100% width and overrides my col settings. If I just could remove that class from the field wrapper …
GOT IT !!!
<?= $form->field($model,‘name’,[‘options’=>[‘class’ => ‘’]])->begin(); ?>
jus empty the default class then oepn tag