I want to add glyphicons instead of text in my index, add, edit views.
This works in the index.ctp
<?= $this->Html->link(__('<i class="glyphicon glyphicon-pencil"></i>'), ['action' => 'edit', $user->user_id], array('escape' => false)) ?>
But when I do it for the delete action it shows me the glyphicon but it doesn't give me the 'Are you sure you want to delete the user?' anymore
<?= $this->Form->postLink(__('<i class="glyphicon glyphicon-minus"></i>'), ['action' => 'delete', $user->user_id], array('escape' => false), ['confirm' => __('Are you sure you want to delete {0}?', $user->username)]) ?>
In the view.ctp it breaks the code that comes after so the content that comes after isn't shown. (in this example that's the content after the glyphicon-pencil. The glyphicon-pencil itself isn't shown as well.
<?= $this->Html->link(__('<i class="glyphicon glyphicon-pencil'), ['action' => 'edit', $user->user_id], ['escape' => false]) ?>
Take a closer look at the arguments that you are passing, you are passing 4, where the method only accepts 3, ie the confirm option is not passed in the actual options argument.
Proper formatting helps a lot to spot such mistakes.
<?=
$this->Form->postLink(
__('<i class="glyphicon glyphicon-minus"></i>'),
[
'action' => 'delete',
$user->user_id
],
[
'escapeTitle' => false,
'confirm' => __('Are you sure you want to delete {0}?', $user->username)
]
)
?>
And your FormHelper::link() example is missing a closing double quote for the <i> elements class attribute, as well as a closing tag for the element itself:
'<i class="glyphicon glyphicon-pencil"></i>'
Also you may want to use escapeTitle instead of escape, in order to avoid disabling escaping for attributes too.
Related
I want to create a Cakephp delete post link within form like the following. But the very first delete post button doesn't include delete Form when I inspect in browser and can't delete but the others include as I want and can delete.
Is it cakephp bug or something I need to change my source code?
<?php
echo $this->Form->create('Attendance', array('required' => false, 'novalidate' => true));
foreach($i = 0; $i < 10; i++):
echo $this->Form->input('someinput1', value => 'fromdb');
echo $this->Form->input('someinput2', value => 'fromdb');
echo $this->Form->postLink('Delete',array('action'=>'delete',$attendanceid),array('class' => 'btn btn-dark btn-sm col-md-4','confirm' => __('Are you sure you want to delete')));
endforeach;
echo $this->Form->button('Submit', array('class' => 'btn btn-success pull-right'));
echo $this->Form->end();
?>
Forms cannot be nested, the HTML standard forbids that by definition. If you try to, most browsers will drop the nested form and render its contents outside of the parent form.
If you need post links inside of existing forms, then you must use the inline or block options (available as of CakePHP 2.5, inline has been removed in CakePHP 3.x), so that the new form is being set to a view block that can be rendered outside of the main form.
CakePHP 2.x
echo $this->Form->postLink(
'Delete',
array(
'action' => 'delete',
$attendanceid
),
array(
'inline' => false, // there you go, disable inline rendering
'class' => 'btn btn-dark btn-sm col-md-4',
'confirm' => __('Are you sure you want to delete')
)
);
CakePHP 3.x
echo $this->Form->postLink(
'Delete',
[
'action' => 'delete',
$attendanceid
],
[
'block' => true, // disable inline form creation
'class' => 'btn btn-dark btn-sm col-md-4',
'confirm' => __('Are you sure you want to delete')
]
);
Close the main form and output post link forms
// ...
echo $this->Form->end();
// ...
echo $this->fetch('postLink'); // output the post link form(s) outside of the main form
See also
CakePHP 2.x
API > FormHelper::postLink()
Cookbook > Views > Using view blocks
CakePHP 3.x
API > \Cake\View\Helper\FormHelper::postLink()
Cookbook > Views > Helpers > Form > Creating Standalone Buttons and POST links
Cookbook > Views > Using View Blocks
The code:
<?php echo $this->Form->submit('<i class="glyphicon glyphicon-arrow-right"></i>', array('class' => array('btn btn-danger')), array('escape' => false)); ?>
Instead of glyphicon it shows just text:
<i class="glyphicon glyphicon-arrow-right"></i>
How to solve this?
As per the docs, you can't use escape with Submit- you have to use Button instead and specify that it's a submit button:
echo $this->Form->button('<i class="glyphicon glyphicon-arrow-right"></i>', array(
'type' => 'submit',
'class' => 'btn btn-danger',
'escape' => false
));
Form->submit() should take two options, a caption and an array of options. You are passing it the caption plus two arrays. Also I don't think in this case you need to wrap these options within sub-arrays.
Try this:
echo $this->Form->submit('<i class="glyphicon glyphicon-arrow-right"></i>',
array('class' => 'btn btn-danger', 'escape' => false)
);
I have this HTML link tag that I need to generate using yii\helpers\Html
<i class="fa fa-fw fa-user"></i> Sign Up
I am able to do it using method a() but I do not know how to include the font awesome class. Here is the code that I already have using a() method
<?= Html::a('Sign Up',['site/signup'], ['class' => 'btn btn-black', 'title' => 'Sign Up']) ?>
I am using bootstrap for my CSS. Any help would be appreciated.
It's simple
<?= Html::a('<i class="fa fa-fw fa-user"></i> Sign Up',['site/signup'], ['class' => 'btn btn-black', 'title' => 'Sign Up']) ?>
Following code generate your desired HTML.
<?= Html::a(Html::tag('i', '', ['class' => 'fa fa-fw fa-user']) . ' Sign Up ', ['site/signup'], ['class' => 'btn btn-black', 'title' => 'Sign Up']) ?>
You can also use yii2-icons, see: https://github.com/kartik-v/yii2-icons
At first you have to install the extension:
composer require kartik-v/yii2-icons "#dev"
Then you can display the icons e.g. in the following way:
use kartik\icons\Icon;
...
Icon::show('trash', ['title' => 'delete'])
Icon::show('calendar', ['class'=>'fa-2x'])
You can find more examples on this demopage: http://demos.krajee.com/icons
Html button is
<button class="btn" type="submit"><i class="icon-search"></i> Go</button>
I changed it into Yii as
<?php
echo CHtml::submitButton('Go', array('id' => 'btSubmit',
'class' => 'btn',
'name' => 'files'
));
?>
How can i add to button
This will work
echo CHtml::tag('button', array(
'name'=>'btnSubmit',
'type'=>'submit'
), '<i class="icon-search"></i> Go');
will generate
<button name="btnSubmit" type="submit"><i class="icon-search"></i> Go</button>
According to the yii source code the CHtml::tag method requires a third parameter to submit internal content. The CHtml::button method does not pass this through, so you can't add internal HTML.
Using the tag method directly should work however:
echo CHtml::tag('button',[
'id'=>'btsubmit','class'=>'btn','name'=>'files','type'=>'submit'
],'<i class="icon-search"></i> Go');
<?php echo CHtml::submitButton(
CHtml::tag('i', array('class' => 'icon-search')) . ' Go',
array('id' => 'btSubmit',
'class' => 'btn',
'name' => 'files')
);
?>
Use CHtml::tag('i', array('class' => 'icon-search')) . ' Go' instead of 'Go' in your code.
I have a simple delete function. Here is where I set the custom data:
<?php echo $this->Html->link(
__('Delete'),
'#CoursesModal',
array(
'class' => 'btn-remove-modal',
'data-toggle' => 'modal',
'role' => 'button',
'data-uid' => $course['Course']['id'],
'data-uname' => $course['Course']['name']
));
?>
It shows in the element fine:
Delete
But when it comes to deleting this object (here's the code for it:)
<?php echo $this->Html->link(__('Delete'),'/courses/delete/#{uid}',array('class' => 'btn btn-danger delete-course-link')) ?>
For some reason the {uid} doesn't translate to 5 - which causes it not to work.
What am I doing wrong?