I'd like to insert a icon of Twitter Bootstrap into this specific button.
The previous mentioned method to add 'escape' => false (here: CakePHP & Twitter Bootstrap CSS button icons) does not work for me.
I tested it succesfully with a simple html-bootstrap-code - but with cakePHP-specific code it does not work.
echo $this->Form->postLink(__('set User'),
array('controller' => 'institutions', 'action' => 'setAssignee', $user['User'] ['institution_id'], $user['User']['id']),
array('class' => 'btn btn-warning icon-plus'),
__('Are you sure?', $user['User']['last_name'], $institution['Institution']['name']));
Won't work, the icon-plus class will be overridden by the background applied to btn.
To get this to work, use an <i> like this:
echo $this->Form->postLink('<i class="icon-plus"></i> ' . __('set User'),
array('controller' => 'institutions', 'action' => 'setAssignee', $user['User'] ['institution_id'], $user['User']['id']),
array('class' => 'btn btn-warning', 'escape'=>false),
__('Are you sure?', $user['User']['last_name'], $institution['Institution']['name']));
Related
I have two form on a single page: login form and register form. When I submit the register form, it validates both: form fields that are in login and registeration. How can I handle it if both form have the same model (user model)
Register form
<?php echo $this->Form->create('User', array('url' => array('controller' => 'users', 'action' => 'add'))); ?>
<?php echo $this->Form->input('username', array('label' => false, 'div' => false, 'class' => 'reg_input'));?>
<?php echo $this->Form->input('email', array('label' => false, 'div' => false, 'class' => 'reg_input'));?>
<?php echo $this->Form->input('password', array('label' => false, 'div' => false, 'class' => 'reg_input'));?>
<?php echo $this->Form->input('confirm_password', array('type' => 'password', 'label' => false, 'div' => false, 'class' => 'reg_input'));?>
<?php echo $this->Form->submit(__('Submit', true), array ('class' => 'reg_button', 'div' => false));
echo $this->Form->end();?>
and Login form is below
<?php echo $this->Form->create('User', array('controller' => 'users', 'action' => 'login'))?>
<?php echo $this->Form->input('User.username',array('label'=>false,'div'=>false, 'class' => 'reg_input'));?>
<?php echo $this->Form->input('User.password',array('label'=>false,'div'=>false, 'class' => 'reg_input'));?>
<?php echo $this->Form->submit(__('Log in', true), array ('class' => 'reg_button', 'div' => false)); ?>
<?php echo $this->Form->end();?>
When I submit registration form it validates both forms, I want to validate only the registration form.
How can I handle that?
I've come up with a "solution" (I find the approach dirty, but it works) for a different question (very similar to this). That other question worked with elements and views, though. I'll post the entire solution here to see if it helps someone (though I rather someone else comes with a different approach).
So, first: change the creation names for the two forms.
//for the registration
<?php echo $this->Form->create('Registration',
array('url' => array('controller' => 'users', 'action' => 'add'))); ?>
//for the login
<?php echo $this->Form->create('Login',
array('controller' => 'users', 'action' => 'login'))?>
The forms should work, look and post to the same actions, so no harm done.
Second step: I don't have your action code, so I'm going to explain what needs to be done in general
public function login() {
if ($this->request->is('post')) {
//we need to change the request->data indexes to make everything work
if (isset($this->request->data['Login'] /*that's the name we gave to the form*/)) {
$this->request->data['User'] = $this->request->data['Login'];
unset($this->request->data['Login']); //clean everything up so all work as it is working now
$this->set('formName', 'Login'); //we need to pass a reference to the view for validation display
} //if there's no 'Login' index, we can assume the request came the normal way
//your code that should work normally
}
}
Same thing for the registration (only need to change 'Login' to 'Registration').
Now, the actions should behave normally, since it has no idea we changed the form names on the view (we made sure of that changing the indexes in the action). But, if there are validation errors, the view will check for them in
$this->validationErrors['Model_with_errors']
And that 'Model_with_errors' (in this case 'User') won't be displayed in the respective forms because we've changed the names. So we need to also tweak the view. Oh! I'm assuming these both forms are in a view called index.ctp, for example, but if they are on separate files (if you're using an element or similar) I recommend add the lines of code for all the files
//preferably in the first line of the view/element (index.ctp in this example)
if (!empty($this->validationErrors['User']) && isset($formName)) {
$this->validationErrors[$formName] = $this->validationErrors['User'];
}
With that, we copy the model validation of the User to the fake-named form, and only that one. Note that if you have a third form in that view for the same model, and you use the typical $this->form->create('User'), then the validation errors will show for that one too unless you change the form name for that third one.
Doing that should work and only validate the form with the correct name.
I find this a messy approach because it involves controller-view changes. I think everything should be done by the controller, and the view shouldn't even blink about validation issues... The problem with that is that the render function of Controller.php needs to be replaced... It can be done in the AppController, but for every updgrade of Cakephp, you'll have to be careful of copying the new render function of Controller.php to the one replacing it in AppController. The advantage of that approach, though, is that the "feature" would be available for every form without having to worry about changing the views.
Well, it's just not that maintainable anyway, so better to leave it alone if it's just for this one case... If anyone is interested on how to handle this just in the controller side, though, comment and I'll post it.
You can duplicate your model and change his name and define $useTable as the same table name.
Example :
class Registration extends AppModel {
public $useTable = 'users';
You define the action in form->create like Nunser for your login form
<?php
echo $this->Form->create('User',array(
'url' => array(
'controller' => 'Users',
'action' => 'login',
'user' => true
),
'inputDefaults' => array(
'div' => false,
'label' => false
),
'novalidate'=>true,
));
?>
and your registration form
<?php
echo $this->Form->create('Registration',array(
'url' => array(
'controller' => 'Users',
'action' => 'validation_registration',
'user' => false
),
'inputDefaults' => array(
'div' => false,
'label' => false
),
'novalidate'=>true,
));
?>
In your controller define a method for registration validation and the most important define the render
public function validation_registration(){
$this->loadModel('Registration');
if($this->request->is('post')){
if($this->Registration->save($this->request->data)){
--- code ---
}else{
--- code ---
}
}
$this->render('user_login');
}
Sorry for my english ! Have a nice day ! :D
The create method on your login form is missing the 'url' key for creating the action attribute. I tried to re-create this once I fixed this and could not. Maybe that will fix it?
I am new in php and cakephp. Just trying to make simple navigation menu.
<li><?php
//pr($this->Html-);
echo $this->Html->link('Entertainment', array(
'controller' => 'UpcommingEntertainments',
'action' => 'index'
));
?></li>
its works fine if I am at www.example.com. The problem is if I am at /admin/* and i click this link, it takes me to www.example.com/admin/Entertainment, I want to go to www.wxample.com/Entertainment.
What should be my link code?
Please try:
echo $this->Html->link('Entertainment', array(
'controller' => 'UpcommingEntertainments',
'action' => 'index',
'admin' => false, // thats what you need
'plugin' => false, // could be helpful if you plan using plugins
));
I included the plugin parameter, because you could encounter the same issue, if you use plugins.
Hope that helps.
<li><?php
//pr($this->Html-);
echo $this->Html->link('Entertainment', array(
'admin' => false, // add this
'controller' => 'UpcommingEntertainments',
'action' => 'index'
));
?></li>
There is a good answer here offering a bit more indepth explanation.
I am trying to find a way to set attributes to the link URL generated by the image method of CakePHP's HtmlHelper. I'm using CakePHP 2.2.1
For example, the following code:
echo $this->Html->image("recipes/6.jpg", array(
"alt" => "Brownies",
'url' => array('controller' => 'recipes', 'action' => 'view', 6)
));
generates:
<a href="/recipes/view/6">
<img src="/img/recipes/6.jpg" alt="Brownies" />
</a>
How can I add attributes to the href tag. Say, for example, class='picture' to look like:
<a href="/recipes/view/6" class='picture'>
<img src="/img/recipes/6.jpg" alt="Brownies" />
</a>
You cannot add HTML attributes to the Anchor tag via the Html->image method - the way to do it is to put the Html->image within the Html->link method like so:
echo $this->Html->link(
$this->Html->image("recipes/6.jpg", array('alt' => 'Brownies')),
array('controller' => 'recipes', 'action' => 'view', 6, array('escape'=>false', 'class'=>'picture')
);
You must also include the 'escpape'=>false - otherwise your <img...> will be escaped, and it will show up like <img ... >
If you want to add any attribute to HTML anchor tag using HtmlHelper, then you can use it via:
<?php echo $this->Html->link(
$this->Html->image("loading.gif", array('alt' => 'Brownies', 'border' => '0')),
array('controller' => 'recipes', 'action' => 'view', 6), array('class' => 'picture', 'escape' => false));
In case you are still looking for an answer, what you have to do is instead of using $this->Html->image with the optional URL attribute, you need to use $this->Html->link with the optional escape = false attribute where the title of the link is your image, utilizing $this->Html->image. What escape = false does is unescape special characters in the title of the link allowing you to use an image or other html element.
Here is an example
echo $this->Html->link(
$this->Html->image($image['Image']['file'], 'class' => 'image', 'alt' => $image['Image']['title'])),
'path/to/image', // or an array('controller' => 'mycontroller', 'action' => 'myaction')
array('escape' => false));
You can add more image attributes such as the class and alt I have listed as well as more link attribute such as the escape I have listed.
I have created a route which looks like this
Router::connect('/:slug', array('controller' => 'categories', 'action' => 'view'),
array('pass' => array('slug')));
Until here, everything works okey, visiting the link http://example.com/animals-and-pets, works perfect.
On this page I have a pagination and this gives me e big problem, the links for the pages, are generating wrong, like this: http://example.com/categories/view/animals-and-pets/page:2.
The result that I want to obtain is example.com/animals-and-pets/2.
Thanks for your help in advance!
I once did it this way:
change CakePhp1.3 paginator destination url?
However, it could get much easier if you use \page:2 instead of \2
Now in cake 2.0 I call the $this->Paginator->options() to set the correct url in the view before the rest of the pagination options. Something like:
//Set the correct url for the pagination, cake will add the "page:" and "sort:" variables to this url
$this->Paginator->options(array('url'=> array('controller' => 'categories', 'action' => 'view', 'slug' => $this->params['pass'][0])));
//now display the pagination
echo $this->Paginator->counter(array('format' => __('Page {:page} of {:pages}')));
echo $this->Paginator->prev('«', array(), null, array('class' => 'prev disabled'));
echo $this->Paginator->numbers(array('separator' => ''));
echo $this->Paginator->next('»', array(), null, array('class' => 'next disabled'));
Hope this helps
I was messing around with the cakePHP link tag...
And, the span is located inside the link.
I know about escape => false, but seems to me, this doesn't really work.
The php part is embedded within the 'li' as below:
<?php echo $this->Html->link($html->tag('span','Hello World'),
array('controller'=>test,
'action'=>index),
array('class' => 'class_b'),
array('escape' => false)
)
?>
My problem here is, the 'span' tag isn't 'eliminated' from the view. What am I doing wrong?
Thanks.
It's rather simple
<?
echo $this->Html->link($this->Html->tag('span',__('News',true)),array('controller'=>'news','action'=>'index'),array('escape'=>false,'class'=>'news'));
?>
you just need to add third parameter to Html link escape=>false
I think this is what you might be looking for, then:
<?php
echo $this->Html->link(
$this->Html->tag('span', 'Hello World.', array('class' => 'class_b')),
array(
'controller' => 'test',
'action' => 'index'
)
);
?>
Found in this reference (near the bottom of the page):
http://book.cakephp.org/1.3/view/1442/link
I was searching answer and for me works this piece of code:
<?php echo $this->Html->link(
$this->Html->tag('span', 'Hello world', array('class' => 'class_a')),
array('controller' => 'test', 'action' => 'index'),
array('escape' => FALSE)
); ?>