Bootstrap pagination with CakePHP pagination helper - cakephp

I'm trying to get CakePHP's pagination helper to play nicely with bootstrap. That is, I want my pagination elements to look like bootstrap's but generated by CakePHP.
At the moment I've got this on my view page:
<?php
echo $this->Paginator->numbers(array(
'before' => '<div class="pagination"><ul>',
'separator' => '',
'currentClass' => 'active',
'tag' => 'li',
'after' => '</ul></div>'
));
?>
Which produces the following markup:
<div class="pagination">
<ul>
<li class="active">1</li>
<li>2</li>
<li>3</li>
</ul>
</div>
The problem is, because the active page (1 in this case) doesn't have an <a> element in the <li> tag, it's not displaying correctly on the page (see here: http://i.imgur.com/OczPh.png).
I can't seem to find anything on the Cookbook that mentions anything that would fix this.
Can this even be fixed?

I've used the generic functions of cake php html needed to bootstrap.
Gist code: https://gist.github.com/jruzafa/5302941
<div class="pagination pagination-large">
<ul class="pagination">
<?php
echo $this->Paginator->prev(__('prev'), array('tag' => 'li'), null, array('tag' => 'li','class' => 'disabled','disabledTag' => 'a'));
echo $this->Paginator->numbers(array('separator' => '','currentTag' => 'a', 'currentClass' => 'active','tag' => 'li','first' => 1));
echo $this->Paginator->next(__('next'), array('tag' => 'li','currentClass' => 'disabled'), null, array('tag' => 'li','class' => 'disabled','disabledTag' => 'a'));
?>
</ul>
</div>

This is actually specifically mentioned in the "Creating page number links" section of the "Paginator" documentation:
currentTag Tag to use for current page number, defaults to null.
This allows you to generate for example Twitter Bootstrap like links
with the current page number wrapped in extra ‘a’ or ‘span’ tag.
In your case, you'll want to use 'currentTag' => 'a'. However, keep in mind that this option was added in CakePHP 2.3, so if you're using a version older than that, it won't work.

All you really need to do is add a CSS class for the current and disabled items to match. Here's one I use for my project (it's basically copied and pasted from the bootstrap CSS file).
.pagination .current,
.pagination .disabled {
float: left;
padding: 0 14px;
color: #999;
cursor: default;
line-height: 34px;
text-decoration: none;
border: 1px solid #DDD;
border-left-width: 0;
}
This gives it the same style as the a tags.

The best I could get:
PHP:
<div class="paging btn-group page-buttons">
<?php
echo $this->Paginator->prev('< ' . __d('users', 'previous'), array('class' => 'btn btn-default prev', 'tag' => 'button'), null, array('class' => 'btn btn-default prev disabled', 'tag' => 'button'));
echo $this->Paginator->numbers(array('separator' => '', 'class' => 'btn btn-default', 'currentClass' => 'disabled', 'tag' => 'button'));
echo $this->Paginator->next(__d('users', 'next') . ' >', array('class' => 'btn btn-default next', 'tag' => 'button'), null, array('class' => 'btn btn-default next disabled', 'tag' => 'button'));
?>
</div>
CSS:
button:hover > a {
text-decoration: none;
}
Result:
.paging {margin: 10px;}
button:hover > a {text-decoration: none;}
<link href="http://getbootstrap.com/dist/css/bootstrap.min.css" rel="stylesheet"/>
<link href="http://getbootstrap.com/dist/css/bootstrap-theme.min.css" rel="stylesheet"/>
<div class="paging btn-group page-buttons">
<button class="btn btn-default prev disabled">< anterior</button>
<button class="disabled btn btn-default">1</button>
<button class="btn btn-default">2</button>
<button class="btn btn-default next">próximo ></button>
</div>

You need to add an anchor tag between "li" tags for active page, try this code :
<nav>
<ul class="pagination">
<?php
echo $this->Paginator->prev('«', array('tag' => 'li', 'escape' => false), '«', array('class' => 'prev disabled', 'tag' => 'li', 'escape' => false));
$numbers = $this->Paginator->numbers(array('separator' => '', 'tag' => 'li', 'currentLink' => true, 'currentClass' => 'active', 'currentTag' => 'a'));
$numbers = preg_replace("#\<li class=\"active\"\>([0-9]+)\<\/li\>#", "<li class=\"active\"><a href=''>$1</a></li>",$numbers);
echo $numbers;
echo $this->Paginator->next('»', array('tag' => 'li', 'escape' => false), '»', array('class' => 'prev disabled', 'tag' => 'li', 'escape' => false));
?>
</ul>
</nav>

Try this if you are using Twitter Bootstrap 3.0 and CakePHP 2.0 or 2.1:
css (somewhere in your css, not in the bootstrap css!)
ul.pagination li.active {
position: relative;
float: left;
padding: 6px 12px;
margin-left: -1px;
line-height: 1.428571429;
text-decoration: none;
background-color: #fff;
border: 1px solid #ddd;
}
CakePHP View (where you want to display the pagination bar)
<ul class="pagination">
<?php
echo ($this->Paginator->hasPrev()) ? $this->Paginator->prev('«', array('tag' => 'li'), null, null) : '<li class="disabled">«</li>';
echo $this->Paginator->numbers(array('separator' => false, 'tag' => 'li'));
echo ($this->Paginator->hasNext()) ? $this->Paginator->next('»', array('tag' => 'li'), null, null) : '<li class="disabled">»</li>';
?>
</ul>

For bootstrap 2 with font awesome and full pagination you can use that :
And add a little hack to your css or less
<div class="pagination">
<ul>
<?php
## PAGINATION
echo $this->Paginator->first('<i class="fa fa-angle-double-left"></i>', ['escape' => false, 'tag' => 'li']);
//
//
echo $this->Paginator->prev('<span><i class="fa fa-angle-left"></i></span>', [
'class' => 'prev enabled',
'tag' => 'li',
'escape' => false,
], null, [
'class' => 'prev disabled',
'tag' => 'li',
'escape' => false,
]);
echo $this->Paginator->numbers(
[
'separator' => '',
'tag' => 'li',
'modulus' => 20,
'escape' => false,
'currentTag' => 'span',
'currentClass' => 'active',
]);
//
echo $this->Paginator->next('<span><i class="fa fa-angle-right"></i></span>', [
'class' => 'next enabled',
'tag' => 'li',
'escape' => false,
], null, [
'class' => 'next disabled',
'tag' => 'li',
'escape' => false,
]);
echo $this->Paginator->last('<i class="fa fa-angle-double-right"></i>', ['escape' => false, 'tag' => 'li']);
?>
</ul>
<div class="pull-right paginationCounter">
<?php
echo $this->Paginator->counter(
[
'class' => 'pull-right',
'format' => '{:page} / {:pages} pages, {:count} results',
]);
?>
</div>
</div>
/* Pagination bootstrap 2 add */
.pagination ul > li.active{
float: left;
-moz-border-bottom-colors: none;
-moz-border-left-colors: none;
-moz-border-right-colors: none;
-moz-border-top-colors: none;
background-color: #fff;
border-color: #ddd;
border-image: none;
border-style: solid;
border-width: 1px 1px 1px 0;
line-height: 20px;
padding: 4px 12px;
text-decoration: none;
cursor: default;
}
.pagination ul > li a[rel='first'],
.pagination ul > li a[rel='last'],
.pagination ul > li.prev a,
.pagination ul > li.next a {
height: 17px;
padding-top: 7px;
}

<ul class="pagination">
<?php
echo $this->Paginator->prev('«', array('tag' => 'li', 'escape' => false), '«', array('class' => 'prev disabled', 'tag' => 'li', 'escape' => false));
echo $this->Paginator->numbers(array('separator' => '', 'tag' => 'li', 'currentLink' => true, 'currentClass' => 'active', 'currentTag' => 'a'));
echo $this->Paginator->next('»', array('tag' => 'li', 'escape' => false), '»', array('class' => 'prev disabled', 'tag' => 'li', 'escape' => false));
?>
</ul>

if ($this->Paginator->hasPage(null, 2)) {
echo '<tfoot>';
echo '<tr>';
echo '<td colspan="7" class="text-right">';
echo ' <ul class="pagination pagination-right">';
echo $this->Paginator->first('<span class="glyphicon glyphicon-fast-backward"></span> First', array('escape' => false, 'tag' => 'li'), null, array('escape' => false, 'tag' => 'li', 'class' => 'disabled', 'disabledTag' => 'a'));
echo $this->Paginator->prev('<span class="glyphicon glyphicon-step-backward"></span> Prev', array('escape' => false, 'tag' => 'li'), null, array('escape' => false, 'tag' => 'li', 'class' => 'disabled', 'disabledTag' => 'a'));
echo $this->Paginator->numbers(array(
'currentClass' => 'active',
'currentTag' => 'a',
'tag' => 'li',
'separator' => '',
));
echo $this->Paginator->next('Next <span class="glyphicon glyphicon-step-forward"></span>', array('escape' => false, 'tag' => 'li', 'currentClass' => 'disabled'), null, array('escape' => false, 'tag' => 'li', 'class' => 'disabled', 'disabledTag' => 'a'));
echo $this->Paginator->last('Last <span class="glyphicon glyphicon-fast-forward"></span>', array('escape' => false, 'tag' => 'li', 'currentClass' => 'disabled'), null, array('escape' => false, 'tag' => 'li', 'class' => 'disabled', 'disabledTag' => 'a'));
echo ' </ul>';
echo '<p>'.$this->Paginator->counter(array('format' => 'Page {:page} of {:pages}, showing {:current} records out of {:count} total.')).'</p>';
echo '</td>';
echo '</tr>';
echo '</tfoot>';
}

you can achieve this without any css to add :
<?php
echo $this->Paginator->numbers(array(
'before' => '<ul class="pagination">',
'separator' => '',
'currentClass' => 'active',
'currentTag' => 'a',
'tag' => 'li',
'after' => '</ul>'
));
?>

#pagination > li.current {
z-index: 2;
color: #ffffff;
position: relative;
float: left;
padding: 6px 12px;
line-height: 1.428571429;
text-decoration: none;
border: 1px solid #dddddd;
margin-left: 0;
color: #999999;
z-index: 2;
color: #ffffff;
cursor: default;
background-color: #428BCA;
border-color: #428BCA;
}
#pagination > li.prev.disabled,#pagination > li.next.disabled {
position: relative;
float: left;
padding: 6px 12px;
line-height: 1.428571429;
text-decoration: none;
border: 1px solid #dddddd;
margin-left: 0;
color: #999999;
cursor: not-allowed;
background-color: #ffffff;
border-color: #dddddd;
}
.pagination > li > a{
color: #428BCA;
}

So many answers, but non handle ellipsis. Below you can find full version, no custom CSS needed.
CakePHP v2, Bootstrap v3
<ul class="pagination">
<li><?php echo $this->Paginator->prev('Previous', array('escape' => false, 'tag' => 'li'), null, array('escape' => false, 'tag' => 'li', 'class' => 'disabled', 'disabledTag' => 'a')); ?></li>
<li><?php echo $this->Paginator->numbers(array('separator' => '', 'currentTag' => 'a', 'currentClass' => 'active', 'tag' => 'li', 'first' => 1, 'ellipsis' => '<li class="disabled"><a>...</a></li>')); ?></li>
<li><?php echo $this->Paginator->next('Next', array('escape' => false, 'tag' => 'li', 'currentClass' => 'disabled'), null, array('escape' => false, 'tag' => 'li', 'class' => 'disabled', 'disabledTag' => 'a')); ?></li>
</ul>
The resulting code:
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<ul class="pagination">
<li class="prev">
Previous
</li>
<li>
1
</li>
<li class="disabled">
<a>...</a>
</li>
<li>
5
</li>
<!-- more numbers here... -->
<li class="active">
<a>9</a>
</li>
<!-- more numbers here... -->
<li class="next">
Next
</li>
</ul>

Related

How to get url params in cakephp?

I am using cakephp 2.6.7:
I have a password reset url like:
http://www.jegeachi.com/resellers/resetpw/9610f4e33d96ad6189c5a779ab90f10aBXX1066056669XXB12/
Here resellers is the controller and resetpw is the action and 9610f4e33d96ad6189c5a779ab90f10aBXX1066056669XXB12 is the argument I want to pass into resetpw function.
Now in resetpw.ctp:
<style type="text/css">
.alert {
padding: 6px;
margin-bottom: 5px;
border: 1px solid transparent;
border-radius: 4px;
text-align: center;
}
.alert.alert-error {
background: #A9B0B5;
color: #EF5858;
font-weight: normal;
}
</style>
<div class="page-content-wrapper">
<div class="page-content">
<!-- BEGIN PAGE CONTENT-->
<div class="content">
<!-- BEGIN LOGIN FORM -->
<?php
echo $this->Form->create('Reseller', array(
'inputDefaults' => array(
'label' => false,
'div' => false
),
'class' => 'login-form',
'url' => array('controller' => 'resellers', 'action' => 'resetpw')
)
);
?>
<h3 class="form-title">Type your new password</h3>
<div class="alert alert-danger display-hide">
<button class="close" data-close="alert"></button>
<span>
Enter your new password first. </span>
</div>
<div class="form-group">
<label class="control-label visible-ie8 visible-ie9">New Password</label>
<div class="input-icon">
<i class="fa fa-lock"></i>
<?php
echo $this->Form->input(
'password', array(
'class' => 'form-control placeholder-no-fix',
'type' => 'password',
'autocomplete' => 'off',
'placeholder' => 'New Password'
)
);
?>
</div>
</div>
<div class="form-actions">
<?php
echo $this->Form->button(
'Reset Password <i class="m-icon-swapright m-icon-white"></i>', array(
'class' => 'btn blue pull-right',
'type' => 'submit',
'escape' => false
)
);
?>
</div>
<?php echo $this->Form->end(); ?>
<!-- END LOGIN FORM -->
</div>
<!-- END PAGE CONTENT -->
</div>
</div>
<!-- END CONTENT -->
Inside resellers action:
function resetpw($a){
$this->layout = "public-login";
$this->loadModel('Reseller');
if ($this->request->is('post')) {
echo 'a : '.$a;
pr($this->params['url']); exit;
$a = func_get_args();
$keyPair = $a[0];
$key = explode('BXX', $keyPair);
$pair = explode('XXB',$key[1]);
$key = $key[0];
$pair = $pair[1];
$password = $this->request->data['Reseller']['password'];
unset($this->request->data['Reseller']['password']);
$uArr = $this->Reseller->findById($pair);
if($uArr['Reseller']['resetkey'] == $key) {
$this->Reseller->read(null, $pair);
$this->Reseller->set('password', $password);
if($this->Reseller->save()) {
$msg = ' <div class="alert alert-success">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>Your password has been reset</strong>
</div>';
} else {
$msg = ' <div class="alert alert-error">
<button type="button" class="close" data-dismiss="alert">×</button>
Something has gone wrong. Please try later or <strong>sign up again</strong>
</div>';
}
} else {
$msg = ' <div class="alert alert-error">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>Invalid link.Please check your reset link</strong> Invalid link.Please check your reset link
</div>';
}
$this->Session->setFlash($msg);
$this->redirect(array('controller'=>'resellers', 'action' => 'login'));
}
}
Here I want to catch the url data 9610f4e33d96ad6189c5a779ab90f10aBXX1066056669XXB12
But none of the following printing my data:
echo 'a : '.$a;
pr($this->params['url']); exit;
How can I get this url value.
As I said in one of my comments on your previous questions, you should first read the docs. What you are doing in this action is wrong on couple of levels, in cakephp sense. Normally you would get to this action by url I suppose
.../resellers/resetpw/9610f4e33d96ad6189c5a779ab90f10aBXX1066056669XXB12/
and by that you would already have that action argument in your controller:
public function resetpw($a = null)
Then you would check if $a is not null since you can't reset password without some kind of token. In your action $a already has this for value 9610f4e33d96ad6189c5a779ab90f10aBXX1066056669XXB12.
No need to use something like this:
$a = func_get_args();
Second thing, you should name arguments to be meaningful, $a doesn't mean nothing, but $token or something similar would make much difference.
There are multiple ways of doing it. In your function below (not writing entire one) you are trying to get the url argument in $a but you are passing parameter instead. Hence $a has no value
function resetpw($a){
$this->layout = "public-login";
$this->loadModel('Reseller');
if ($this->request->is('post')) {
echo 'a : '.$a; //$a will have no values here
pr($this->params['url']); exit;
In your case you need catch params like below
$a = $this->params['passed'][0];
Or to get argument your URL should be like below
http://www.jegeachi.com/resellers/resetpw?9610f4e33d96ad6189c5a779ab90f10aBXX1066056669XXB12/
so that you can collect it in $a. Hope this helps to you!
I solved this by removing 'url' in Form.
I changed
<?php
echo $this->Form->create('Reseller', array(
'inputDefaults' => array(
'label' => false,
'div' => false
),
'class' => 'login-form',
'url' => array('controller' => 'resellers', 'action' => 'resetpw')
)
);
?>
To
<?php
echo $this->Form->create('Reseller', array(
'inputDefaults' => array(
'label' => false,
'div' => false
),
'class' => 'login-form',
)
);
?>
Now it works. When you set url then parameter in url is removed.

Why my variables are undefined i click on the submit button?

I have fields in a form with variables from the Controller TownsController, I use "set" to be able to affchés in sight. I arrive at the desired result, the problem is that when I want to send my information by clicking on the submit button, I have several errors telling me that the variable is undefined.
Before i click on the submit button :
After i click on the submit button :
The view :
<h2>Informations de votre ville</h2>
<?php echo $this->Form->create('Town'); ?>
<div class="form-group">
<div class="col-xs-6">
<?php echo $this->Form->input('statut',array(
'label' => 'Statut',
'type' => 'select',
'options' => array(
'Village'=>'Village',
'Ville'=>'Ville'),
'selected' => $selectedstatut,
'class' => 'form-control',
'id' => 'town_statut_municipal',
'empty' => __('Statut municipal')
));
?>
</div>
</div>
<div class="form-group">
<div class="col-xs-6">
<?php echo $this->Form->input('name', array('type' => 'text', 'id' => 'town_name', 'class' => 'form-control', 'placeholder' => 'Choisir un statut municipal', 'label' => 'Nom')); ?>
</div>
</div>
<div class="form-group">
<div class="col-xs-6">
<?php echo $this->Form->input('country', array('type' => 'select', 'class' => 'form-control', 'label' => 'Pays', 'id' => 'town_country', 'onchange' => "print_state('state',this.selectedIndex);", 'selected' => $selectedcountry, 'empty' => false,
'options' => array(
'Canada'=>'Canada',
'USA'=>'USA'))); ?>
</div>
</div>
<div class="form-group">
<div class="col-xs-6">
<?php
echo $this->Form->input('localisation', array('type' => 'select', 'class' => 'form-control', 'label' => 'Localisation', 'id' => 'state', 'empty' => $namelocalisation,
'options' => $localisations)); ?>
</div>
</div>
<div class="form-group">
<div class="col-xs-12">
<br>
<?php
$options = array('label' => 'Sauvegarder', 'class' => 'btn', 'div' => false, 'class' => 'btn btn-lg btn-success');
echo $this->Form->end($options);
?>
</div>
</div>
</form>
TownsController (edit view) :
public function edit(){
$town_id = $this->Auth->user('town_id');
$user_id = $this->Auth->user('user_id');
if(!$town_id && !$user_id) {
$this->redirect('/');
die();
}
$this->Town->id = $town_id;
if($this->request->is('put') || $this->request->is('post')) {
}
else {
$this->request->data = $this->Town->read(array('name', 'country', 'localisation', 'statut'), null);
$selectedstatut = $this->Session->read('Auth.Town.statut');
$this->set(compact('selectedstatut'));
$selectedcountry = $this->Session->read('Auth.Town.country');
$this->set(compact('selectedcountry'));
$countryuser = $this->Session->read('Auth.Town.country');
if($countryuser = 'Canada') {
$namelocalisation = 'Province';
$localisations = array(
'Alberta'=>"Alberta",
'British Columbia'=>"British Columbia",
'Manitoba'=>"Manitoba",
'New Brunswick'=>"New Brunswick",
'Newfoundland'=>"Newfoundland",
'Northwest Territories'=>"Northwest Territories",
'Nova Scotia'=>"Nova Scotia",
'Nunavut'=>"Nunavut",
'Ontario'=>"Ontario",
'Prince Edward Island'=>"Prince Edward Island",
'Quebec'=>"Quebec",
'Saskatchewan'=>"Saskatchewan",
'Yukon Territory'=>"Yukon Territory");
}
if($countryuser = 'USA') {
$namelocalisation = 'État';
$localisations = array(
'Alabama'=>"Alabama",
'Alaska'=>"Alaska",
'Arizona'=>"Arizona",
'Arkansas'=>"Arkansas",
'California'=>"California",
'Colorado'=>"Colorado",
'Connecticut'=>"Connecticut",
'Delaware'=>"Delaware",
'District Of Columbia'=>"District Of Columbia",
'Florida'=>"Florida",
'Georgia'=>"Georgia",
'Hawaii'=>"Hawaii",
'Idaho'=>"Idaho",
'Illinois'=>"Illinois",
'Indiana'=>"Indiana",
'Iowa'=>"Iowa",
'Kansas'=>"Kansas",
'Kentucky'=>"Kentucky",
'Louisiana'=>"Louisiana",
'Maine'=>"Maine",
'Maryland'=>"Maryland",
'Massachusetts'=>"Massachusetts",
'Michigan'=>"Michigan",
'Minnesota'=>"Minnesota",
'Mississippi'=>"Mississippi",
'Missouri'=>"Missouri",
'Montana'=>"Montana",
'Nebraska'=>"Nebraska",
'Nevada'=>"Nevada",
'New Hampshire'=>"New Hampshire",
'New Jersey'=>"New Jersey",
'New Mexico'=>"New Mexico",
'New York'=>"New York",
'North Carolina'=>"North Carolina",
'North Dakota'=>"North Dakota",
'Ohio'=>"Ohio",
'Oklahoma'=>"Oklahoma",
'Oregon'=>"Oregon",
'Pennsylvania'=>"Pennsylvania",
'Rhode Island'=>"Rhode Island",
'South Carolina'=>"South Carolina",
'South Dakota'=>"South Dakota",
'Tennessee'=>"Tennessee",
'Texas'=>"Texas",
'Utah'=>"Utah",
'Vermont'=>"Vermont",
'Virginia'=>"Virginia",
'Washington'=>"Washington",
'West Virginia'=>"West Virginia",
'Wisconsin'=>"Wisconsin",
'Wyoming'=>"Wyoming");
}
$this->set('localisations', $localisations);
$this->set('namelocalisation', $namelocalisation);
}
}
The reason is that you set those variables IF the request is not POST. When you click on Submit your code will enter in the "isPost" condition, hence the variables localisations, namelocalisation, selectedcountry and selectedstatut are never set.
Here's what I would do (pseudo code)
if ($this->request->is(array('post', 'put'))) {
if ($this->Post->save($this->request->data)) {
$this->Session->setFlash(__('Your post has been updated.'));
return $this->redirect(array('action' => 'index'));
}
}
$selectedstatut = $this->Session->read('Auth.Town.statut');
etc...
Notice I am setting the view variables outside the if statement so that if save fails (because validation) then we set the variables again.

Register button in cakephp

I made a login page in CakePHP and need to add a register button, this is the code of \View\Users\login.ctp:
<div class="container">
<div class="login-content">
<?php echo $this->Html->image('logo1.png', array('alt' => 'fivassist Logo')); ?>
<?php echo $this->Form->create('User'); ?>
<div class="login-form">
<h2>Bem-vindo!</h2>
<ul>
<li>
<label>E-mail:</label>
<?php echo $this->Form->input('email',
array('label' => false, 'autocomplete' => 'off')); ?>
</li>
<li><label>Password:</label>
<?php echo $this->Form->input('password',
array('label' => false, 'autocomplete' => 'off')); ?>
</li>
</ul>
</div> <!-- end login-form -->
<?php echo $this->Session->flash(); ?>
<?php echo $this->Form->submit('Login', array('id' => 'login-bt')); ?>
Esqueceu-se da sua password?
<?php echo $this->Form->button('Register'); ?>
</div> <!-- end login-content -->
</div> <!-- end container -->
How i redirect the register button to /user/add.ctp?
Can you help me please?
Thank you.
You need to point your form to your "register" action: I would advise making this action separate from your "New User" action- sometimes you want to have different logic.
This should submit to /users/register. Note the parameters on $this->Form->create:
echo $this->Form->create('User', array('url' => array('controller' => 'users', 'action' => 'register')));
echo $this->Form->input('email', array('class' => 'form-control', 'label' => false,'placeholder' => 'Email'));
echo $this->Form->input('pwd', array('class' => 'form-control', 'label' => false, 'placeholder' => 'Password'));
echo $this->Form->input('pwd_repeat', array('class' => 'form-control', 'label' => false, 'placeholder' => 'Password'));
echo $this->Form->submit('Register', array('class' => 'btn btn-large btn-primary'));
echo $this->Form->end();
I'd also suggest you read the docs for HtmlHelper, they can help a lot.
You want to when click button login that will submit to action login and click button register it's submit to action users/add ?
if so that you can use javascript or jQuery
$(function () {
// when click button login
$('#btn-login').on('click', function () {
// #form: id of form, you should named it
$('#form').atrr('action', 'login');
});
// when click button register
$('#btn-register').on('click', function () {
$('#form').atrr('action', 'register');
});
$('#form').submit();
});
And if you want to click button register it's will redirect to view add.ctp
So that you can use HtmlHelper
echo $this->Html->link('Register', array('action' => 'add'), array('class' => 'btn-register', 'id' => 'btn-register', 'escape' => false));
and then you define css for btn-register
.btn-register {
display: inline-block;
padding: 6px 12px;
margin-bottom: 0;
font-size: 14px;
font-weight: normal;
line-height: 1.42857143;
text-align: center;
white-space: nowrap;
vertical-align: middle;
cursor: pointer;
background-image: none;
border: 1px solid transparent;
border-radius: 4px;
color: #333;
background-color: #fff;
border-color: #ccc;
}

How to create link make cakephp with twitter bootstrap

example:
<a class="btn btn-mini" href="#"><i class="icon-star"></i> Star</a>
i try by Html::Helpers like
echo $this->Html->link($this->Html->tag('i','',
array('class' => 'btn')), array('action' => '../link'));
but it not work !
try:
echo $this->Html->link(
$this->Html->tag('i', '', array('class' => 'icon-star')) . " Star",
array('action' => 'your_action'),
array('class' => 'btn btn-mini', 'escape' => false)
);

Form checkbox and label with CakePHP and Bootstrap

I'm having some issues trying to get checkbox inputs and labels to output the correct HTML using CakePHP and Twitter Bootstrap.
The Bootstrap specific output should be:
<div class="control-group">
<div class="controls">
<label class="checkbox">
<input type="checkbox"> Keep me logged in
</label>
</div>
</div>
However, using the inputDefaults mentioned here (http://stackoverflow.com/a/9496242/1247225), this Cake form input:
echo $form->input('auto_login', array(
'type' => 'checkbox',
'label' => __('Keep me logged in', true)));
Outputs this:
<div class="control-group">
<input type="hidden" name="data[User][auto_login]" id="UserAutoLogin_" value="0" />
<input type="checkbox" name="data[User][auto_login]" class="" value="1" id="UserAutoLogin" />
<div class="controls">
<label for="UserAutoLogin">Keep me logged in</label>
</div>
</div>
Any ideas how to adjust this individual input so it outputs the correct Bootstrap HTML, like above?
The best way to control the form output, or the format of the output, is by passing a 'format' argument. Try playing with this:
'format' => array('before', 'label', 'input', 'between', 'after', 'error')
I don't think it is document, but by reading the code, you can find it in there.
the prettiest way to do it is this:
<?php
echo '<div class="col-lg-2">';
echo $this->Form->input('Content.checkbox', array(
'div' => array(
'class' => 'input-group',
),
'label' => false,
'type' => 'checkbox',
'before' => '<span class="input-group-addon">',
'after' => '</span><span class="input-group-addon"><label for="ContentCheckbox">What does the fox say?</label></span>',
));
echo '</div>';
I using :
<?php
echo $this->Form->input('coupDeCoeur',
array('div' => false,
'label' => false,
'type' => 'checkbox',
'before' => '<label class="checkbox">',
'after' => '<i></i>coupDeCoeur</label>'
));
?>
You need to build the form widget manually for checkboxes instead of using FormHelper::input.
For example:
echo '<div class="control-group">';
echo $this->Form->label('Model.field', null, array('class' => 'control-label'));
echo '<div class="controls">';
echo $this->Form->checkbox('Model.field');
echo '</div>';
echo '</div>';
Try following code:
<div class="control-group">
<div class="controls">
<label class="checkbox">
<?php echo $this->Form->input('auto_login', array('type'=>'checkbox','label' => false, 'div' => false,'class'=>false,'after'=>__('Keep me logged in')));?>
</label>
</div>
</div>
If you're using security component, you may have problems if you create your inputs without FormHelper::input. If that's the case, you should try with this:
echo "
<div class='control-group'>
<div class='controls'>
<label class='checkbox'>";
echo $this->Form->input('Model.field', array('label' => false, 'after' => 'Model.field'))."
</label>
</div>
</div>";
Here is the ready-to-use solution:
App::uses('FormHelper', 'View/Helper');
class InputHelper extends FormHelper {
// var $helpers = array('Form', 'Html');
public function create($model, $options = array()) {
$options['class'] = (isset($options['class']) && $options['class']) ? $options['class'] : 'form-horizontal table';
$options['inputDefaults'] = (isset($options['inputDefaults']) && $options['inputDefaults']) ? $options['inputDefaults'] : array(
'div' => 'control-group',
'label' => array('class' => 'control-label'),
'between' => '<div class="controls">',
'after' => '</div>'
);
return parent::create($model, $options);
}
public function input($fieldName, $options = array()) {
$this->setEntity($fieldName);
$options = $this->_parseOptions($options);
if ($options['type'] == 'checkbox') {
$options['format'] = array('before', 'label', 'between', 'input', 'after', 'error');
}
fdebug($options);
return parent::input($fieldName, $options);
}
}
If you want a "one line" answer i got it:
echo $this->Form->input('Model.Field', array('class'=>'form-inline', 'after'=>'</br>'));
In my case, I used AdminLTE template with Bootstrap 3 and this code was working for me:
<?php
echo $this->Form->input('Model.fieldname', array(
'label' => false,
'type' => 'checkbox',
'before' => '<label>',
'after' => 'Field name label here</label>',
));
?>
Good luck here!!!
Here's a simple solution that works for me:
The CSS (LESS):
input.form-control[type="checkbox"] {
width: auto;
height: auto;
margin-right: 5px;
display: inline;
}
Then use the Form helper:
echo $this->Form->input(
'field_name',
array(
'div' => 'form-group',
'class' => 'form-control',
'type' => 'checkbox',
'label' => array(
'text' => 'Your label',
'class' => 'label-checkbox'
),
'format' => array('input', 'label')
)
);

Resources