How can I line up radio buttons horizontally rather than vertically? - cakephp

I have been looking at the Cookbook - 7.3.3 Automagic Form Elements trying to find a way for radio buttons to line up horizontally rather than vertically. I have been looking at the 'div' => 'class_name' $options also the $options[‘between’], $options[‘separator’] and $options[‘after’] but have been unsuccessful. Is there a "cake" way of doing this?
<?=$form->input('Product Type', array(
'type' => 'radio',
'id' => $tire['ProductType']['id'],
'name' => $tire['ProductType']['id'],
'options' => array(2=>'Tire Manufacturer', 7=>'Rim Manufacturer '),
));?>
The of equivalent this
<label>Product Type</label>
<div style="padding-top:5px;">
<input type="radio" name="data[Manufacturer][product_type_id]" value="<?=$tire['ProductType']['id']?>"> Tire Manufacturer
<input type="radio" name="data[Manufacturer][product_type_id]" value="<?=$wheel['ProductType']['id']?>"> Rim Manufacturer
</div>

This works for me:
Inside your view file (viewfile.ctp):
<div>
<?php
echo $this->Form->create('changeRegion');
$options=array('US'=>'U.S.','CA'=>'Canada', 'FN'=>'International');
$attributes=array('legend'=>false, 'label'=>false, 'value'=>'US', 'class'=>'locRad');
echo $form->radio('subLocation',$options,$attributes);
echo $this->Form->end();
?>
<div class="clear"></div>
</div>
Make sure 'label'=>false and 'legend'=>false are set in your attributes.
In your stylesheet:
input[type=radio] {
float:left;
margin:0px;
width:20px;
}
form#changeRegionStdForm input[type=radio].locRad {
margin:3px 0px 0px 3px;
float:none;
}
I fought this forever because of the default CakePHP style rule for input[type=radio] (inside the app/webroot/css/cake.generic.css file) was always taking precedence. I generated another rule to specifically pick out the radio group in question by referencing the form along with the radio input rule appended with the class name (form#changeRegionStdForm input[type=radio].locRad). I initially did this with the form# rule listed before the input[type=. Only when I moved it after the input[type= did the radio buttons line up horizontally.
I hope this makes sense and actually helps someone else.

The easiest way to do this is to put each radio button inside an 'LI' tag, and then apply CSS styles to the 'UL' that tells it to show 'LI' tags horizontally instead of vertically. You can find a lot of horizontal list designs here.

I found this post very late. But i thought others might stumble upon this like i did now.
Try something like this:
<ul id="hMenu"><li>
<?php
$attributes=array('legend'=>false, 'separator'=>'</li><li>', 'label'=>false);
$form->input('Product Type',$options, $attributes);
?>
</li></ul>
Now style the id hMenu. That should solve this problem.
This without any styling gives verical radio buttons. But if you use css styling on ul and li you could literally make it stand on its head! :)

use this:
$form->input('Product Type', array(
'type' => 'radio',
'id' => $tire['ProductType']['id'],
'name' => $tire['ProductType']['id'],
'options' => array(2=>'Tire Manufacturer', 7=>'Rim Manufacturer '),
***********************
'seperator' => '<br />'
***********************
));
which result look like this :
<label>Product Type</label>
<div style="padding-top:5px;">
<input type="radio" name="data[Manufacturer][product_type_id]" value="<?=$tire['ProductType']['id']?>"> Tire Manufacturer
<br />
<input type="radio" name="data[Manufacturer][product_type_id]" value="<?=$wheel['ProductType']['id']?>"> Rim Manufacturer
</div>

Try this, I got my code working through this..
<ul id = "navlist">
<fieldset>
<legend><?php echo __('Insert User'); ?></legend>
<li><?php //echo $this->Form->input('Site', array('options' => $arraycharges));?></li>
<li><?php echo $this->Form->input('MobileApp', array('options' => array('admin' => 'Admin', 'user' => 'User')));?></li>
<li><?php echo $this->Form->input('Location', array('options' => array('admin' => 'Admin', 'user' => 'User')));?></li>
<li><?php echo $this->Form->end(__('GO')); ?></li>
</fieldset>
</ul>
in the style sheet add the following..
#navlist li {
display: inline-block;
list-style-type: none;
padding-right: 20px;
}

You an follow like this :
<ul>
<li style="width: 200px;float: left;list-style: none">
<?php
$attributes=array(
'legend'=>false,
'after'=>'</li>',
'separator'=>'</li><li style="width: 200px;float: left;list-style: none">',
);
echo $this->Form->radio('xyz_name',$myOptions,$attributes);
?>
</li>
</ul>

Related

Cakephp 3. Image Radio Buttons

Regarding to Cakephp 3 Form Helper, for Radio Buttons with Images, i got a question need to ask for help.
From the forumn there's a question, but it was 10 years version earlier, which is not suitable for my codes.
originally the Add.ctp for this item is meant for Type=>Select
<?php echo $this->Form->control('product_image_id', array('label'
=> false, 'div' => false,
'class' => 'form-control', 'type' => 'select', 'empty' =>
'Choose Image')); ?>
in the Type select, the name of the images only shows out. not the images.
When i want to put the Form Control into the code for the add.ctp
<?php foreach ($product['product_images'] as $productImg) : ?>
<div class="container parent">
<div class="row">
<div class='col text-center'>
<?php echo $this->Form->control('product_image_id', ['type' =>
'radio', 'escape' =>
false, 'class' => 'form-control', 'id' =>$productImg->id,
'name'=>'imgbackground' ]); ?
>
<label for="<?= $productImg->id ?>">
<?php
echo $this->Html->image($imgPath, ['width' => '100']);
?>
<div class="tick_container">
<div class="tick"><i class="fa fa-check"></i></div>
</div>
</label>
</div>
</div>
</div>
<?php endforeach; ?>
Information regarding settings for Form Control for radio buttons is limited....
can anyone help to advise where i went disaligned?
thanks
You basically have two options, that is either manipulate the radio options for FormHelper::control() to include your custom label contents, or to manually create each label and radio button.
For the former you build an options array that holds nested arrays with text and value keys, where text is the label contents, and value the radio button value, eg
[
['text' => '<img src="..."> ...', 'value' => '1'],
['text' => '<img src="..."> ...', 'value' => '2'],
// etc ...
]
A quick and dirty example could look like this (I'm just guessing here that there's something like a path property on your product images, you'll most likely have to adjust this a bit according to your needs to build the proper web path to the image):
echo $this->Form->control('product_image_id', [
'type' => 'radio',
'templates' => [
'radioWrapper' => '{{input}}{{label}}',
'nestingLabel' => '{{hidden}}<label{{attrs}}>{{text}}</label>',
],
'labelOptions' => [
'escape' => false,
],
'options' => collection($product['product_images'])
->map(function ($productImage) {
return [
'text' =>
$this->Html->image($productImage->path, ['width' => '100']) .
'<div class="tick_container">
<div class="tick"><i class="fa fa-check"></i></div>
</div>',
'value' => $productImage->id,
];
}),
]);
Note that you need to define custom/modified templates for the label and the wrapper if you actually need the input to be rendered outside of the label. If you don't actually need it to be rendered that way, then you can drop this example's templates option, by default the form helper would render the input inside of the label, eg:
<label><input/>text</label>
See also
Cookbook > Views > Helper > Form > Creating Radio Buttons
Cookbook > Views > Helper > Form > Using Collections to build options

In my <a> tag, there are lots of code HTML. How to add Html link (CakePHP)

My code:
<a href="#">
<div class="list_content">
<p class="title"><?php echo $note['Note']['title']; ?></p>
<p class="create_at"><?php echo $note['Note']['create_at'] ?></p>
<p> <?php echo substr($note['Note']['content'], 0,100) . '...' ?></p>
</div>
</a>
How to add <?php echo $this->Html->link('...') ?> in CAKEPHP 2.x
If you want to insert HTML element in any HTML helper, you have to add 'escape' => false. Check the document https://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::link
Simple Example:
$this->Html->link('<b>My Content</b>','#',[
'escape' => false
]);
For you case:
$this->Html->link(
$this->Html->div('list_content',
$this->Html->para('title',$note['Note']['title']).
$this->Html->para('create_at',$note['Note']['create_at']).
$this->Html->para(null,substr($note['Note']['content'], 0,100) . '...')
),
'#',
['escape' => false]
);
If you are going to use Aman's answer, remember that by setting 'escape' => false you are disabling a default security feature. So you probably want to make sure you then escape any user input using the h() method:-
$this->Html->link(
$this->Html->div('list_content',
$this->Html->para('title', h($note['Note']['title'])).
$this->Html->para('create_at', h($note['Note']['create_at'])).
$this->Html->para(null, substr(h($note['Note']['content']), 0,100) . '...')
),
'#',
['escape' => false]
);
If you've got a lot of markup you want inside your <a> tags it is sometimes simpler to use $this->Html->url() instead (and can lead to more readable code):-
<a href="<?= $this->Html->url('#') ?>">
<div class="list_content">
<p class="title"><?php echo $note['Note']['title']; ?></p>
<p class="create_at"><?php echo $note['Note']['create_at'] ?></p>
<p> <?php echo substr($note['Note']['content'], 0,100) . '...' ?></p>
</div>
</a>
The only real disadvantage I am aware of doing this second example is that you lose any functionality that you may add to $this->Html->link(), but I suspect this isn't a concern for the majority of users.

Cakephp Multiple Checkboxes in a row not showing values

$this->Form->checkbox('username', array('value' => 'basssr', 'style' => 'float: left; display: inline'));
this code only show check box not values
Try this:
<div style="float:left;">
<?php
echo $this->Form->input('username', array('type' => 'checkbox','value' => 'basssr', 'style' => 'float: left; display: inline'));
?>
</div>
this code put it 2 times This will shows checkboxes in horizontal.

Outputting multiple elements within <a> using CakePHP HTML Helper

I'd like to know if this code...
echo $this->Html->link(
"<h3>test</h3>".$this->Html->image("image.jpg")."<p>Some text</p>",
"/link",
array('escape' => false)
);
...is the best way to generate this HTML in CakePHP...
<a href="/path/to/link">
<h3>test</h3>
<img alt="" src="/path/to/image.jpg">
<p>Some text</p>
</a>
Or, is there a more "correct" way of doing this? I want the and all to be within the tag so that I can set the to display: block; in CSS and have the whole area clickable with a hover effect.
Something tells me that having HTML echoed like this isn't the right way to go about it, but I can't see an alternative if I'm going to use the HTML Helper. Is there one?
Just use the URL method of the HTML helper instead of the link one,do the rest as static HTML, may as well keep the amount of PHP down to a minimum as far as I see it.
<a href="<?php echo $this->Html->url($params); ?>">
<h3>test</h3>
<?php echo $this->Html->image($params); ?>
<p>Some text</p>
</a>
This would be the most efficient way of doing this task. But maybe you should think about your markup structure and what you want to achieve...
I use
$this->Html->tag('li',
$this->Html->link(
'<i class="entypo-book"></i>'.
$this->Html->tag('span', $nom
.$this->Html->tag('span', '32', array('class' => 'badge'))
),
array('controller' => 'Pages', 'action' => 'index'),
array('class' => 'active', 'title' => 'Pages', 'escape' => false)
)
);

Custom Submit button

How can I create submit button, and define custom title on it, together with custom class style?
You could use either submit() or button() methods of the Form helper instead of the end() method. For example:
echo $this->Form->submit(
'Send',
array('class' => 'custom-class', 'title' => 'Custom Title')
);
Don't forget to close the form. You can do it by calling the end() method without any arguments.
echo $this->Form->end();
Also remember, you can always do it old school
I prefer to use $this->Form->end( ); without arguments and build my own submit buttons and markup. It's easy
<div class="buttons clearfix">
<button type="submit" class="positive">
<span class="icon-wrapper"><img src="path/to/tickmark.png" alt="" title="" /></span>
Save Item
</button>
</div>
I would also tell you to experiment with the $this->Form->input('Model.field', 'options' => array( array('type' => 'button'))); - particularly the before, between, after, and class options. You can use the helper to create <input type="button" /> elements with a good amount of flexibility.
you can create costum submit by this code
echo $this->Form->submit(
'Submit',
array('div' => false,'class' => 'urclass', 'title' => 'Title')
);
This is enough:
echo $this->Form->submit("Custom message");
Also as #Mike suggest close the form with
echo $this->Form->end();
Or you can combine both with:
echo $this->Form->end("Custom Message");
I created a custom button using an image in my under app/webroot/img that uses inline style for specifying size and changing the position to center
$options=array('type'=>'Make secure payment', 'type'=>'image', 'style'=>'width:200px; height:80px; display:block; margin-left:auto; margin-right:auto;');
echo $this->Form->submit('/img/axiaepaysecurebuttongray_med.png', $options);
echo $this->Form->end();
For CakePHP 2.x, you can use
$options = array(
'label' => 'Update',
'div' => array(
'class' => 'glass-pill',
)
);
echo $this->Form->end($options);

Resources