CakePHP - pass an empty value into a radio button - cakephp

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

Related

Yes No check box

I'm new at php the form I'm creating has a Yes No checkbox my form works fine except if you check the Yes box and then check the No box they both stay highlighted. How can I make it so if the Yes box is checked and they check the No box the Yes box is unchecked.
If you need my code let me know.
I've tried some of the code for checkbox but being new to php some of it doesn't make any sense.
You probably want to use radio button instead of checkbox.
Radio button will automatically deselect when you choose the other option.
Example :
Gender:
<input type="radio" name="gender"
<?php if (isset($gender) && $gender=="female") echo "checked";?>
value="female">Female
<input type="radio" name="gender"
<?php if (isset($gender) && $gender=="male") echo "checked";?>
value="male">Male
<input type="radio" name="gender"
<?php if (isset($gender) && $gender=="other") echo "checked";?>
value="other">Other
This will render like :
RadioButtonPng

checkbox remain checked after submit laravel

I have this simple checkbox.
My problem is, if I select any of the checkbox, it should remain checked after form submission.
<label>
<input type="checkbox" name="services_offered[]" value="water jet cutting"> Water Jet Cutting</label>
<label>
<input type="checkbox" name="services_offered[]" value="plasma cutting"> Plasma Cutting</label>
<label>
<input type="checkbox" name="services_offered[]" value="CNC router cutting"> CNC Router Cutting</label>
I tried to use my code before but no luck,
<?php
function isChecked($value)
{
return (!empty($_REQUEST['services_offered']) && in_array($value,$_REQUEST['services_offered']));
}
<label><input type="checkbox" name="services_offered[]" value="water jet cutting" <?php if(isChecked('water jet cutting')) echo ' checked' ?>> Water Jet Cutting</label>
<label><input type="checkbox" name="services_offered[]" value="plasma cutting" <?php if(isChecked('plasma cutting')) echo ' checked' ?> > Plasma Cutting</label>
<label><input type="checkbox" name="services_offered[]" value="CNC router cutting" <?php if(isChecked('CNC router cutting')) echo ' checked' ?>> CNC Router Cutting</label>
I want it to convert in "laravel way" but I dont know how.
Hope you understand me.
Thanks.
I don't know if I understand you correct but:
The form does not post values of the checkboxes.
So I made hidden inputs associated with each checkbox and maintain there values with javascript something like:
$("#services_offered").change(function() {
if(this.checked) {
$('#services_offeredHidden').attr('value','1');
}
else {
$('#services_offeredHidden').attr('value','0');
}
});
In laravel controller I play only with hidden inputs which I get the from Request $request.
When you return the view from a controller you do like this
return view('nameOfView')->with('checkbox',$checkboxValue);
and the in the blade you can access the value using:
{{$checkbox}}
Hope it helps!
If you want the "Laravel way" of doing things then I suggest you check out the HTML package by Laravel Collective for marking up the HTML forms in your view. Also make sure the methods on your controller are receiving a Request as it's argument and maybe even checkout Laravel's built-in Form Request Validation as well.
Note, your current code is very old fashioned so it might take a while to learn how to do things the modern way but stick with it and you will be rewarded.
In your view you will open your form like this:
{!! Form::open(['url' => 'foo/bar']) !!}
//
{!! Form::close() !!}
Your method on the controller that receives the form will look something like this:
public function store(Request $request)
{
$name = $request->input('name');
//
}
But in general, because your question is too broad to answer clearly without basically re-writing your code for you, you need to start doing some tutorials to learn Laravel properly. I recommend Jeff Way's video tutorial courses on Laracasts.
I had a similar problem - here is how I solved it:
My blade template rendered a list of checkboxes as html:
<label for="sId1"><input type='checkbox' name='sIds[]' id="sId1" value="1" checked >Draft</label>
<label for="sId2"><input type='checkbox' name='sIds[]' id="sId2" value="2" checked >Submitted</label>
I wanted to a) default them all to checked; and b) preserve their checked/unchecked status between validations.
The key is to use the laravel helper function old() - and to realise the old() function returns an array - so you need to do an in_array(value,old()). You also need to cover when the old() is empty on first load by checking is old()==null:
Here is the blade template snippet I used to do it:
#foreach($wfStatusIdsArray as $wfsId=>$statusName)
<label for="sId{{$wfsId}}"><input type='checkbox' #if(old('sIds')==NULL || in_array($wfsId,old('sIds'))) checked #endif name='sIds[]' id="sId{{$wfsId}}" value="{{$wfsId}}">{{$statusName}}</label>
#endforeach
There is a bug in this - if ALL checkboxes are cleared, on validation, the checkboxes are reloaded as all ticked. This is fine for my situation so I have not fixed it. Let me know if you fix that bug!

How to format the name of the checkbox while send by a GET cakephp

I would like to know if it's possible to format the name of the checkbox when I try to send it to my other page via a GET method.
Actually I've got a multiple checkboxes that generate something like:
<div class="checkbox"><input type="checkbox" name="test[]" value="1" id="ResearchTest1" /><label for="ResearchTest1">First Test</label></div>
<div class="checkbox"><input type="checkbox" name="test[]" value="2" id="ResearchTest2" /><label for="ResearchTest2">Second Test</label></div>
<div class="checkbox"><input type="checkbox" name="test[]" value="3" id="ResearchTest3" /><label for="ResearchTest3">Third Test</label></div>
And when I send my form, my URL looks like:
research%3D%26test%3D%26test%5B%5D%3D1%26test%5B%5D%3D2%26test%5B%5D%3D3
Which is :
research=&test=&test[]=1&test[]=2&test[]=3
And what I would like, will be:
research=&test1=1&test2=2&test3=3
Or
research=&test=1&test=2&test=3
Or, much better:
research=&test=1,2,3
Any ideas ?
That is how checkboxes work, if you want test1=1&test2=2&test3=3 then you should name each checkbox individually.
echo $this->Form->checkbox('foo', array('name' => 'test1'));
echo $this->Form->checkbox('foo', array('name' => 'test2'));
echo $this->Form->checkbox('foo', array('name' => 'test3'));
This will make your processing much harder.
You can do research=&test=1,2,3 with JS join(). Why you want to make this difficult for yourself I dont know. You can easily get that same format in the controller doint the GET with implode(',', $theData)
I would recommend using standards for submitting your form and process the data later.
You can define the name of the input using the FormHelper:
echo $this->Form->checkbox('yourInputId', array('name' => 'yourInputName'));
The problem is that I use a multiple checkboxes:
$this->Form->input('tests', array('type' => 'select', 'multiple' => 'checkbox', 'options' => $options));
So I can't rename each checkbox individually.
Maybe I should not use cakephp for this, but try to write my own checkboxes directly in html?

How do I remove the wrapping div from a Form input of type select-multiple

I have two tables in my database "cars" and "car_types". "cars" table refers to "car_types" by "car_type_id". For example "car_types" has 2 fields "id" and "car_type". It also has 3 entries "new", "used dealer", "used private". How can I show these 3 entries as checkbox in my view.
I'm trying to adjust the output from:
foreach ($car_types as $car_type)
{
$car_type_new[$car_type['CarType']['id']]=$car_type['CarType']['car_type'];
}
echo $this->Form->input('Car.car_type_id',array('div'=>false,'multiple'=>'checkbox','options'=>$car_type_new,'style'=>"margin-left:20px; padding:0;"));
I also want to remove the wrapper div around each checkbox.
Each checkbox is output by the Form helper like so, even if the div => false option is set:
<input type="hidden" id="CarCarTypeId" value="" name="data[Car][car_type_id]">
<div class="checkbox"><input type="checkbox" id="CarCarTypeId1" value="1" name="data[Car][car_type_id][]"><label for="CarCarTypeId1">New</label></div>
<div class="checkbox"><input type="checkbox" id="CarCarTypeId2" value="2" name="data[Car][car_type_id][]"><label for="CarCarTypeId2">Used Dealer</label></div>
<div class="checkbox"><input type="checkbox" id="CarCarTypeId3" value="3" name="data[Car][car_type_id][]"><label for="CarCarTypeId3">Used Private
</label></div>
the div => false option only removes the div wrapped around the entire collection of checkboxes, not each checkbox.
Any Ideas on how I could remove the div that wraps around each checkbox? And please do tell me if I am doing it wrong.
I know the question is about CakePHP 1.3, but I found this while searching on the Internet, so I'll share the solution that worked for me anyway.
You can pass a corresponding key to the $options for the input() helper function:
'div'=>false
More info: http://api.cakephp.org/2.5/class-FormHelper.html#_input
Taking a look at the API, you can use the after and before array options to control what goes before and after the <input> and <label> pair. Here is the documentation on the input function or the API
Alternatively, you you could use the checkbox function (API) in the Form helper to get around it

CakePHP Form Helper just generated too much code for me

I am using cakePHP.
I used the cakePHP built-in Form Helper to generate an input text box:
echo $form->input('quote', array('label'=>'Post Number', 'class'=>''));
But when I looked at the HTML source code, I found out these:
<div class="input text">
<label for="ReplyQuote">Post Number</label>
<input name="data[Reply][quote]" type="text" class="" maxlength="12" value="1" id="ReplyQuote" />
</div>
It's really more than enough. I mean the code generated by the Cake built-in Form Helper.
Those DIV tags with class named in a strange naming convention way are not helpful,
because there is space in between the Class name like:
<div class="input text">
Does CakePHP have any options for users to omit those DIV Tags?
Yes.
Check out the options array that you can pass to the FormHelper::input() method. Book reference is at http://book.cakephp.org/view/189/Automagic-Form-Elements
In short, the form helper is adding two distinct classes to the div -- input, and text. If you don't want a div, just do:
echo $form->input( 'quote', array( 'label' => 'Post Number', 'div' => false ) );
Setting the options['class'] value only affects the class selector assigned to the actual input itself (see in your HTML code how the input tag has class=""?)
you can also use $form->text() for input box.

Resources