jjose
(Justinpolackal)
April 12, 2014, 7:53am
1
Hi,
I’m pretty new to Yii let alone 2.0.
Trying to create a simple model based form using ActiveForm. How can I tell ActiveForm class not to print the model variables as labels ? Below is the View I’m trying out:
<?php $form = ActiveForm::begin(['id' => 'samples-form',
'options' => ['class' => 'form-horizontal'],
'action' => "/index.php?r=samples",
'fieldConfig' => [
'template' => "{label}\n<div class=\"col-lg-3\">{input}</div>\n<div class=\"col-lg-8\">{error}</div>",
'labelOptions' => ['class' => 'col-lg-1 control-label'],
],
]
);
?>
<?= $form->field($model, 'myname')->textInput()?>
<?= $form->field($model, 'myemail') ?>
<?= $form->field($model, 'mydate') ?>
<?= $form->field($model, 'mycheck')->checkBox() ?>
<?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>
<?php ActiveForm::end(); ?>
Thanks
samdark
(Alexander Makarov)
April 12, 2014, 8:42am
2
It seems you want hidden inputs, correct?
jjose
(Justinpolackal)
April 12, 2014, 2:45pm
3
No. I want to avoid Yii generating default labels, derived from my variable names. I want to have my own labels.
I could do this for text fields by removing {label} from the ActiveForm’s template option. But for checkbox, it always shows the model variable name as the label.
How can I control this behaviour ?
thanks
jjose:
No. I want to avoid Yii generating default labels, derived from my variable names. I want to have my own labels.
I could do this for text fields by removing {label} from the ActiveForm’s template option. But for checkbox, it always shows the model variable name as the label.
How can I control this behaviour ?
thanks
Easily.
<?= $form->field($model, 'is_cool')->checkbox([
'label' => 'Custom value label'
])->label('Custom outer label'); ?>
PS. there are hell of a lot different options for activeFields, they’re highly customizable.
jjose
(Justinpolackal)
April 12, 2014, 5:05pm
6
Thanks! that works great !
You can also use the model attributeLabels() method. I do like this for multiple languages:
public function attributeLabels()
{
return [
'name' => \Yii::t('contact', 'FORM_NAME'),
'email' => \Yii::t('contact', 'FORM_EMAIL'),
'subject' => \Yii::t('contact', 'FORM_SUBJECT'),
'body' => \Yii::t('contact', 'FORM_BODY'),
'verifyCode' => \Yii::t('contact', 'FORM_VERIFY'),
];
}
I prefer to have all of my language strings in /messages/ using place holders, so I set this in my config:
'sourceLanguage'=>'',
This will force Yii to look for all language strings, including the default (en-US), in /messages/