yii2 checkbox is not showing but label is visible - checkbox

<?= $form->field($model, 'term_condition')->checkbox(); ?>
It is not showing the checkbox but it shows the label.
and I looked into the generated html and that is also fine.here is the generated html.
<div class="form-group field-dynamicmodel-term_condition">
<input type="hidden" name="DynamicModel[term_condition]" value="0"><label> .
<input type="checkbox" id="dynamicmodel-term_condition"
name="DynamicModel[term_condition]" value="1"> Term Condition</label>
<div class="help-block"></div>

I had the same problem, in my case I could solve it with css:
.form-group input[type="checkbox"]{ display: block;}
For a reason when the input is rendered, the display property is none

There are options missing.
As stated in the docs the method is defined as
public $this checkbox ( $options = [], $enclosedByLabel = true )
With the default - an empty $options array - there is nothing to display.
So just specify some options and the checkbox will be displayed:
<?= $form->field($model, 'term_condition')->checkbox([
"0" => "Option 1",
"1" => "Option 2"
]); ?>

Related

CakePHP 2 output checkbox array inside loop

I've read CakePHP multiple checkbox array HTML the right way but getting strange results. I have a list of tags (from a Model called Tag). I want to loop through these and output checkboxes for them in a View.
So I have obtained the Tag data in my Controller with:
$tags = $this->Tag->find('list', ['order' => ['name' => 'ASC']]);
$this->set('tags',$tags);
When I loop through it in my View I am trying to output the checkboxes in between Bootstrap markup:
<?php echo $this->Form->create('GroupTag'); ?>
<?php foreach ($tags as $tag_id => $tag): ?>
<div class="checkbox">
<label>
<?php echo $this->Form->checkbox('tag_id[]', array( 'value'=> $tag_id)); ?>
<?php echo $tag; ?>
</label>
</div>
<?php endforeach; ?>
I copied the syntax for tag_id[] from the post I linked to.
But when I inspect the markup it's producing the following as the name attribute for each <input type="checkbox">:
data[GroupTag][tag_id[]]
Should this not be
data[GroupTag][tag_id][]
?
The idea is that I have multiple checkboxes with a name attribute tag_id[] and then in the Controller I can loop through what has been checked.
Please can someone advise on this as I can't get it working and have looked into examples provided on here/docs.
Try this
$this->Form->checkbox('tag_id.', array( 'value'=> $tag_id))
You can also do this:
$this->Form->input('GroupTag.tag_id', [
'type' => 'select',
'multiple' => 'checkbox',
'options' => $tag_id
]);
FormHelper::select(string $fieldName, array $options, array $attributes)
Example:
$options = array(
'Value 1' => 'Label 1',
'Value 2' => 'Label 2'
);
echo $this->Form->select('Model.field', $options, array(
'multiple' => 'checkbox'
));
Output:
<div class="input select">
<label for="ModelField">Field</label>
<input name="data[Model][field]" value="" id="ModelField"
type="hidden">
<div class="checkbox">
<input name="data[Model][field][]" value="Value 1"
id="ModelField1" type="checkbox">
<label for="ModelField1">Label 1</label>
</div>
<div class="checkbox">
<input name="data[Model][field][]" value="Value 2"
id="ModelField2" type="checkbox">
<label for="ModelField2">Label 2</label>
</div>
</div>

How to convert below code in CakePHP 3?

<div class="form-group">
<label for="inputEmail" class="col-lg-2 control-label">Title</label>
<div class="col-lg-10">
<input type="text" style="width: 45%;" class="form-control" id="titleId" name="title" placeholder="Title">
</div>
</div>
I have this type of code in my add.ctp file and I don't know how to convert it into like
<? echo $this->Html->input('title',['class'=>'']);
You have to use Form helper instead of CakePHP Html helper. Form helper helps you to create form field as well as help to validate the form. But Html helper helps you to create Html like Html for image displaying. Here we go
$this->Form->input('title', [
'label' => [
'text' => 'Title',
'class' => 'col-lg-2'
],
'style' => [
'width: 45%;'
]
]);
I would like to advise you to go to this link, have a deeper look on the docs :)
Here is the Form helper of CakePHP 3
Here is the Html helper of CakePHP 3

Yii dynamic checkbox

I have a column in my table named 'availability_option' with type enum('0', '1', '2'). Zero means 'Fixed Price', One means 'Auction', Two means 'Both'.
I want to generate 2 checkboxes dynamically One for Fixed Price and another 'Auction'.
How is it possible?
I didi it static.
But it should not be the right syntax of yii.
<input value="0" id="fixedprice" type="checkbox" name="ProductShop[availability_option][]">
<label for="fixedprice">Fixed Price</label>
<input value="1" id="auctionprice" type="checkbox" name="ProductShop[availability_option][]">
<label for="auctionprice">Auction</label>
I want it dynamically, So how is it possible?Any idea?
Use checkboxList():
<?= $form->field($model, 'attribute_name')->inline(true)->checkboxList([0 => 'Fixed Price', 1 => 'Auction']) ?>
http://www.yiiframework.com/doc-2.0/yii-bootstrap-activefield.html#inline%28%29-detail
I was facing The same problem and I solved It by doing like this :
<?php echo $form->field($model, 'name[]')->checkboxList(['a' => 'Item A', 'b' => 'Item B', 'c' => 'Item C']); ?>
Note that the checkboxList expects the first argument of the passed array to be of string type as well so you may need to make changes to your array.
Generated HTML will be as:
<label><input type="checkbox" name="fomrmName[name][]" value="'1'"> a</label>
another way by which I solved My problem was to wrap the checkbox in foreach block like following:
<?php foreach($vendorsData as $value){?>
<li><?= $form->field($ucVendors, 'vendor_id['.$value.']')->checkbox(array('label'=>$value)); ?>
and the Generated html is as:
<label><input type="checkbox" id="usedcarvendors-vendor_id-value" name="UsedCarVendors[vendor_id][value]" value="1"> value</label>
Reference Link here

CakePHP - pass an empty value into a radio button

How can I pass an empty value to a radio button in Cake?
I've looked into Cake's 2.0 helpers form documentation but I didn't seem to find much about this.
Maybe some of you can enlighten me?
Cheers
FormHelper::radio(string $fieldName, array $options, array $attributes)
Creates a set of radio button inputs.
$options = array('M' => 'Male');
$attributes = array('legend' => false);
echo $this->Form->radio('gender', $options, $attributes);
Will output:
<input name="data[User][gender]" id="UserGender_" value="" type="hidden" />
<input name="data[User][gender]" id="UserGenderM" value="M" type="radio" />
If for some reason you don’t want the hidden input, setting
$attributes['value'] to a selected value or boolean false will do just
that.
As you can see on the documentation of FormHelper::radio

How can I display CakePHP input validation errors in a different spot the default?

Let's say I have field that looks like this in the view:
<li class="bigfield">
<?php echo $form->input('phone', array(
'placeholder' => 'Phone',
'label' => false,
'between' => '<br />'
)); ?>
</li>
If I have a validation rule on this field and validation fails, I see the following HTML:
<li class="bigfield">
<div class="input text required error">
<br>
<input name="data[Appointment][email]" type="text" placeholder="Email"
maxlength="45" value="" id="AppointmentEmail" class="form-error">
<div class="error-message">Please enter a valid email address</div>
</div>
</li>
I'm like to do something like move the error message div to an entire different part of the page rather then have it inside with the same <li> as the field itself. What would be the most straight forward way of doing this?
Just updating an old post.
The validations errors are automatically passed on to view (as pointed out by #Angel S. Moreno)
$this->validationErrors
In you controller:
$this->set('validationErrorsArray', $this->ModelName->invalidFields());
You will have $validationErrorsArray in your views.
UPDATE (Sept. 2014):
From the view
From CakePHP 2.3 you can access validation errors array from the view:
$this->validationErrors;
From the controller
If you tried to save data in the controller you can access validation errors this way:
$this->ModelName->validationErrors;
If you want to validate data before saving do it this way:
$this->ModelName->set($this->request->data);
if ($this->ModelName->validates()) {
$this->ModelName->save();
} else {
$errors = $this->ModelName->validationErrors;
// handle errors
}
Validating Data from the Controller
From controller you can use:
$this->Modelname->validationErrors['TheFieldYouWantToDisplay'] = 'This is not correct'
In your case it would be like this in your controller:
$this->Appointment->validationErrors['email'] = 'Error message'
This code is just to make a custom error message on the fly. But you can also define $validate in the model and do it like how brancer has described it.

Resources