Form element inside html element not working in cakephp - cakephp

I have read the documentation and tried all the possible solution but no gain.
This is my html code
<?php echo $this->Html->link(
$this->Form->button("Continue", ["type" => "button", "class"=>"btn btn-dark btn-theme-colored btn-flat mr-5"]),
array(
'controller' => 'my_controller',
'action' => 'my_action'
));?>
I want to have a button inside <a> tag but it is outputting the button in '' " tags.
This is what i want
<i>
<button></button>
</i>

All special chars will be converted to html entities, unless you disable this behavior, using escape option. Code below should output what you want:
<?php echo $this->Html->link(
$this->Form->button("Continue", ["type" => "button", "class"=>"btn btn-dark
btn-theme-colored btn-flat mr-5"]),
array(
'controller' => 'my_controller',
'action' => 'my_action'
),
array(
'escape' => false
));?>
More about HTML helper can be found here: https://book.cakephp.org/3.0/en/views/helpers/html.html#creating-links

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

Creating buttons with links using CakePHP's HTML/Form Helpers

I would like to convert a link into a button using CakePHP Helpers.
With $this->Html->link() I am able to use an array() to include 'action' => 'view' . $user['User']['id'], but I am not sure how to include this when using $this->Form->button() instead.
Using $this->Html->link():
$this->Html->link('Click me', array(
'controller' => 'users',
'action' => 'view' . $user['User']['id']));
My Solutions
My solutions do not allow me to add 'action' => 'view' . $user['User']['id']
Using $this->Form->button():
echo $this->Form->button('Click me', array(
'type' => 'button',
'onclick' => 'location.href=\'/rentmyride/users/index/\';',
));
Using <input> tag:
<input type="button" class="btn btn-primary" value="Click me"
onclick="location.href='http://www.domain.com';">
Using <button> tag:
<button class="btn btn-success" onclick="location.href='http://www.domain.com';">
Click me
</button>
HTML5 buttons have a formaction attribute for this use. Of course this works only in modern browsers
$this->Form->button(
'Click me',
array(
'formaction' => Router::url(
array('controller' => 'users','action' => 'view' . $user['User']['id'])
)
)
);
if you are using cakephp 2.X and above please use this line of code for link in button:
<button onclick="window.location.href='<?php echo Router::url(array('controller'=>'Users', 'action'=>'admin_index'))?>'">;Go Back</button>;
I hope this is works for you

link wrap with li tag

Is it possible to create link wrap with li tag?
am using cakephp2
$this->Html->link(
__('title'),array('controller' => 'controller', 'action' => 'index', 'admin' => false)
, array('class' => "", 'id' => "")
);
<li>
<?php
echo $this->Html->link(
__('title'),
array(
'controller' => 'controller',
'action' => 'index',
'admin' => false
),
array('class' => "", 'id' => "")
);
?>
</li>
don't make things more confusing than they need to be.
or if you really must use Cake use HtmlHelper::tag,
$this->Html->tag('li', $this->Html->link(..)); // <li>..</li>
First of all what you posted is not a input box, it is a Link.
I'm assuming you are trying to wrap a Input inside a li tag.
A Quick Search in the CakePHP Book resulted in this approach:
(for cakePHP 2.0 or higher)
echo $this->Form->input('field', array(
'before' => '--before--',
'after' => '--after--',
'between' => '--between---'
));
This Code Results in the following Html
<div class="input">
--before--
<label for="UserField">Field</label>
--between---
<input name="data[User][field]" type="text" value="" id="UserField" />
--after--
</div>
I think this is exactly what you were looking for.
In the future before Posting a Question anywhere, you should first check the CakePHP Book

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)
)
);

how to write this code with cakephp 1.3 html helper?

i try to use gallery prettyphoto but i have problem with this code..how to write this code with cakephp html helper
<li><a href="images/fullscreen/2.jpg" rel="prettyPhoto[gallery1]">
<img src="images/thumbnails/t_2.jpg" width="60" height="60" alt="Nice building" /></a></li>
The key part is to turn off HTML escaping for the link text (as it contains an HTML image tag). Also, images are usually stored in the paths like /img/... but that will depend on your implementation.
<li><?php
$thumb = $this->Html->image('images/thumbnails/t_2.jpg', array(
'width' => 60,
'height' => 60,
'alt' => 'Nice building',
));
echo $this->Html->link($thumb, 'images/fullscreen/2.jpg', array(
'rel' => 'prettyPhoto[gallery1]',
'escape' => false, // important
));
?></li>

Resources