Help Needed On Activeform And Labels

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

It seems you want hidden inputs, correct?

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.

also see this commit: https://github.com/yiisoft/yii2/commit/8ac79a35aea98b84fcf7563296d1ff43591da639

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/