How to add class to ActiveForm in Yii 2.0 framework?
In Yii2 I don't think 'htmlOptions' works. Just 'options' is correct, e.g.
<?php
$form = ActiveForm::begin(
[
'action' => '/login',
'options' => [
'class' => 'userform'
]
]
);
// ... add all your inputs here for example:
echo $form->field($model, 'login');
ActiveForm::end();
?>
Yii2 - How To Add a CSS class to an ActiveForm Field
You can add it with
<?= $form->field($model, 'url')->textInput(['maxlength' => 255, 'class' => 'your class'])->label(false); ?>
As a rule you can pass html elements when telling activeField what type it should be. The default is textInput so that is why your code works, but if you want to change the input then you have to explicitly tell it the type of input.
http://www.yiiframework.com/doc-2.0/guide-input-forms.html
How to add class to Form in Yii framework?
You need to pass an htmlOptions property, like this:
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'mail-list-addmail-form',
'enableAjaxValidation'=>false,
'htmlOptions'=>array(
'class'=>'subscribe_form',
)
)); ?>
from http://www.yiiframework.com/doc/api/1.1/CActiveForm#htmlOptions-detail
How to add css class to form-group div of ActiveField in Yii2?
if you are looking to add the class to all the group divs mean all the div that have the class form-group
then you can use the fieldConfig
option of the ActiveForm
or if you want it for one specific field then you can use the options
option of the $form->field()
as the 3rd parameter
For the whole Form
$form = yii\widgets\ActiveForm::begin([
'fieldConfig' => [
'options' => [
'class' => 'my-group'
]
]
]);
For Single Field
echo $form->field($model, 'name', ['options' => ['class' => 'my-class']])->textInput();
Conversion
About converting your above HTML using ActiveForm the following should work you can use template
option of the $form->field()
3rd parameter to add your custom icon after the input, along with other, see below will create your desired HTML
echo $form->field($model, 'date', [
'options' => [
'class' => 'form-group has-icon has-label'
],
'inputOptions' => [
'class' => 'form-control'
],
'template' => '{label}{input}<span class="form-control-icon"><i class="fa fa-map-marker"></i></span>{error}'
])->widget(yii\jui\DatePicker::class, [
'id' => 'created_at',
'options' => [
'placeholder' => 'Airport or Anywhere'
]
]);
You will have something like below
How to add a css class to a ActiveForm field in Yii2
You should simply add the needed class :
<?= $form->field($model, 'name_pu')->textInput(['maxlength' => true, 'class' => 'form-control punjabi']) ?>
How to add a class to a Yii form label?
As the doc says, $htmlOptions is an array of additional HTML attributes. The keys are the attribute, while the values are the attribute values, so to add a class attribute to the label:
<?php echo $form->label( $model,'username', array('class'=>'className') ); ?>
Yii. How to add css error class to input on form submit?
To have AJAX validation also, you should add something more or less like this:
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'contact-form',
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
'afterValidate' => 'js:function(form, data, hasError) {
if(hasError) {
for(var i in data) $("#"+i).addClass("error_input");
return false;
}
else {
form.children().removeClass("error_input");
return true;
}
}',
'afterValidateAttribute' => 'js:function(form, attribute, data, hasError) {
if(hasError) $("#"+attribute.id).addClass("error_input");
else $("#"+attribute.id).removeClass("error_input");
}'
),
)); ?>
How to add contextual classes in Yii 2 ActiveField / ActiveForm?
Yii automatically adds has-error
class in case of validation errors.
If you want to add any CSS-class to ActiveField container then you can use options property. For example:
<?= $form->field($model, 'field', [
'options' => [
'class' => 'form-group has-error',
],
])
->textInput(['maxlength' => true, 'placeholder' => 'Erroneous input'])
->label('Erroneous label')
->hint('Dummy hint');
?>
Related Topics
Why Does Svg Get Blurred When Scaled Under Webkit Browsers
React Testing Library (Rtl): Test a Responsive Design
Loading Global SASS Files in Multiple Compass Projects
Ie7 Cause of "Text - Empty Text Node"
How to Prevent a Flex Item Height to Overflow Due to It's Content
How to Select Parent Pseudo-Class from Within Child Using SCSS
Make Position: Fixed Behavior Like Sticky (For Vue2)
Flexslider Fades But Will Not Slide
Is It Somehow Possible to Style an Iframes Before/After Pseudo-Element
How to Turn Off View Encapsulation for One Property in Angular 2
How to Make Space Between Middle Line and Text
Fill Element with Slanted Background on Hover
Bootstrap Modal Restores Button Focus on Close
Scss Function for Animation Keyframes
How to Align One Element to The Right and One Element to The Left Inside a Containing Element