how to add class inside checkbox and label field - yii

i want to implement this html code into yii format.
<input type="checkbox" name="" class="checkbox1" onclick="" id=""/>
<label class="gender" for="" id="">Female</label>
i tried to implement this code.
$form->checkBox($model,'gender',array('class'=>'gen'),array('template'=>'{label}')
but i don't know how to assign gender css class to label field? could please suggest me how to implement above html code in right way.

Please refer this Different Checkbox Properties. you will get answer from there...
if you want to use Chtml::checkBox...
<?php
echo CHtml::checkBox('gender',
array(''),array('class'=>'checkbox1')); ?>
<label style="width:150px;"class="gender">
<?php echo 'Gender' ;?>
</label>
if you want to use $form->checkBox...
<?php
echo $form->checkBox($model,'gender', array('class'=>'checkbox1'));
?>
<label style="width:150px;"class="gender">
<?php echo 'Gender';?>
</label>
Try it's working i have tried in my form...

CHtml::checkBox() gives you only the input tag
To create a <label> you should use CHtml::label() method. There you can set class property for this tag.

Edited:
I do this:
<?php echo $form->checkBox($model, 'gender', array('class'=>'checkbox1')); ?>
<?php echo $form->labelEx($model,'gender', array('class'=>'genger')); ?>

You just can add labelOptions array value to control label:
echo $form->checkBoxList(
$model,
'attr',
array('ggg','hhh'),
'htmlOptions' => array(
'labelOptions' => array(
//put here what you want
)
)
);

Related

Post inside controller not loading into model in Yii2

When I want to get the variable from the form the post action doesn't load .
This is my view:
<?php
$form = ActiveForm::begin();
?>
<div class="form-group">
<input type="text" name="username" placeholder="FullName">
<?= Html::a(Yii::t('app', 'Start'), ['start', 'link' => $model->link], ['type' => 'button','class' => 'btn btn-primary btn-round']) ?>
</div>
<?php ActiveForm::end(); ?>
This is my controller:
if ($model->load(Yii::$app->request->post())){
exit(var_dump('everything is ok'));
}else {
exit(var_dump('nothing is right'));
}
The result is 'nothing is right'.
Apart from using the anchor link instead of a submit button, you are not using model to create active input hence the field names are without model names or the standard array format that Yii accepts, you should pass empty string to the load method as second parameter which is formName like below
$model->load(Yii::$app->request->post(),'');
So your complete form should look like
<?php
$form = ActiveForm::begin(
[
'action' => 'start',
]
);
?>
<div class="form-group">
<input type="text" name="username" placeholder="FullName">
<?php echo Html::submitButton(Yii::t('app', 'Start'), ['class' => 'btn btn-primary btn-round']) ?>
</div>
<?php ActiveForm::end();?>
EDIT
and your controller code should look like below, mind the first check it needs to be there so that the code is run when you submit only not on page load
if (Yii::$app->request->isPost) { //this should be here before the rest of the code
if ($model->load(Yii::$app->request->post(), '')) {
exit(var_dump('everything is ok'));
} else {
exit(var_dump('nothing is right'));
}
}
This is because load() method looks for post data inside model name property and you are writing the input yourself instead of using the Yii method for forms.
So your post Yii::$app->request->post() returns:
array(
'username' => 'value of username',
)
And your $model->load looks for
array(
'modelName' => array(
'username' => 'value of username',
)
)
To make your post data too look like that you could do it the right way that is, delete your Input and use this method inside the form:
<?= $form->field($model, 'username')->textInput(['maxlength' => true]) ?>
Or the wrong way, modify your input and inside username use:
<input type="text" name="modelName[username]" placeholder="FullName">
Of course where I put username you must put your real model name.
Finally I find the solution
<?php
$form = ActiveForm::begin(
[
'action' => 'start',
]
);
?>
<div class="form-group">
<input type="text" name="username" placeholder="FullName">
<?= Html::a('submit', Url::to(['start', 'link' => $model->link]), ['data-method' => 'POST']) ?>
</div>
<?php ActiveForm::end();?>
thank you for all

Why radio buttons doesn't remain unchangeable and checked?

I'm new here and I'm writing my first php code. I want to prepare a multiple choice quiz in which, after click on a button at the end of the page, the radios button (the choices of the test) will remain checked and unchangeable. I simplify the code in this way:
<?php
$choice1 = "a";
$choice2 = "b";
?>
<form method="post" name="testForm" id="testForm">
<ul>
<li><input type="radio" name="q1" value="a"
<?php if(isset($_POST['test-result']) && $radioVal==$choice1){ echo "disabled='disabled' checked";}?>
<?php if(isset($_POST['test-result']) && $radioVal<>$choice1){ echo "disabled='disabled'";}?>>
<?php echo "<span id='question'>". $choice1 ."</span>";?> </li>
<li><input type="radio" name="q1" value="b"
<?php if(isset($_POST['test-result']) && $radioVal==$choice2){ echo "disabled='disabled' checked";}?>
<?php if(isset($_POST['test-result']) && $radioVal<>$choice2){ echo "disabled='disabled'";}?>>
<?php echo "<span id='question'>". $choice2 ."</span>";?> </li>
</ul><br>
<button class='button' name='test-result'>Finaliza el test</button>
</form>
<?php
if (isset($_POST['test-result']))
{
$radioVal = $_POST["q1"];
}
?>
However, after click on the button, all radios remain uncheged (and this is ok) but the choices made are not checked. Can someone help me? Thanks in advance!
Checkbox is not showing checked because variable $radioVal is set after input box. To resolve your issue could you please set $radioVal before from.

Yii CActiveForm CheckBox not displayed correctly

I'm new using Yii framework. I show a list a checkbox. But it's not displayed correctly.
Here is what I want: http://imageshack.us/photo/my-images/703/checkboxgood.png
But, here is what I get: http://imageshack.us/photo/my-images/715/checkbox.png
Anyway, here is code I used to generate this view:
<div class="row">
<?php echo $form->labelEx($model,'kat3'); ?>
<?php echo $form->checkBox($model,'kat3'); ?>
?php echo $form->error($model,'kat3'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'kat4'); ?>
<?php echo $form->checkBox($model,'kat4'); ?>
<?php echo $form->error($model,'kat4'); ?>
</div>
I am new to web developing, and understand CSS a bit.
Anyone have a suggestion?
thanks! :)
Yii comes with default CSS classes, and that's what you see.
In order to customize these, you must implement your own custom CSS classes for this section. You may wrap your form into a form-class of yours and define the CSS rules as you wish.

Yii framework, uploading files doesn't work

I have a form and I want to upload a file. here is my code:
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'show-form',
'enableAjaxValidation'=>false,
'htmlOptions' => array('enctype' => 'multipart/form-data'),
)); ?>
<fieldset>
<legend>DATI TECNICI</legend>
<div class="row">
<?php echo $form->labelEx($model,'tec_data_file'); ?>
<?php echo $form->fileField($model,'tec_data_file',array('size'=>45,'maxlength'=>45)); ?>
<?php echo $form->error($model,'tec_data_file'); ?>
</div>
</fieldset>
<?php $this->endWidget(); ?>
</div><!-- form -->
There was nothing added to the database after submitting, I did a bit of debuging with firebug and figured out that filefield generates a code like this:
<input id="ytShow_tec_data_file" type="hidden" name="Show[tec_data_file]" value="">
<input id="Show_tec_data_file" type="file" name="Show[tec_data_file]" maxlength="45" size="45">
and two data are sent by $_POST for tec_data_file (which is my file field in db). The first var is empty (I think it is related to first hidden input). and the second one contains my file. and when I'm assigning the variables to my model for saving:
$modelPhoto->attributes = $_POST['Photo'];
the tec_data_file gets an empty string! So nothing gets uploaded to my db. Anyone have an idea how to solve this? If you need more i
You need something like:
$model = new Photo;
$model->attributes = $_POST['Photo'];
$model->image = CUploadedFile::getInstance($model,'file');
where file is the name of the field.
I haven't tested this, it's from the documentation.

Yii , hidden dropdown

i am trying to make a dropdown , but i don't want i to be visible for now , here is the code.
<div class="row">
<?php echo $form->labelEx($model,'lang_id'); ?>
<?php echo $form->dropdownlist($model,'lang_id',CHtml::listData(Lang::model()->findAll(), 'id', 'name')); ?>
<?php echo $form->error($model,'lang_id'); ?>
</div>
How do i make this type = 'hidden' or something like that ?
In other words , i want to keep the field but i don't want it to be shown.
try this..
<div class='row' style='display:none'>
Also, you can define style attribute of your dropDownList.
public static string dropDownList(string $name, string $select, array $data, array $htmlOptions=array ( ))
You can try with:
<div class="row">
<?php echo $form->labelEx($model,'lang_id'); ?>
<?php echo $form->dropDownList($model,'lang_id',CHtml::listData(Lang::model()->findAll(), 'id', 'name'), array('style' => 'display: none'); ?>
<?php echo $form->error($model,'lang_id'); ?>
</div>