Paginator prev not working with Ajax View - cakephp

I'm having this issue where the previous button of the paginator is disabled (have disabled class) by default, even if I am in to second or third or any page. If I remove the 'id => prevPage' and 'id => nextPage' then it works fine but I have to use it to make another ajax request.
I'm using this in my ajax view..
echo $this->Paginator->prev('< ' . __('Zur'), array('id' => 'prevPage'), null, array('class' => 'prev disabled'));
echo $this->Paginator->next(__('Vor') . ' >', array('id' => 'nextPage'), null, array('class' => 'next disabled'));
while in my controller, I have;
if($this->RequestHandler->isAjax()) {
$this->autoRender = false;
$this->set('products',$this->paginate('Product'));
$this->set('page',$page);
$this->render('ajaxView');
}
I did a little googling which suggested me it might be bug.. is it? How do I solve?

I wasn't able to find the error, but I fixed the error by going through other way. I was using 'offset' while I was paginating in the controller before which had introduced the error, but using the other way around by requesting to '.../page:3?q=' on the ajax call, did solved the problem. Thanks to #hereshem for the quick fix.

Related

Cakephp: Escape paginator prev link

I'm using the prev link for the Paginator component like so:
<?php echo $this->Paginator->prev('‹', array('escape'=> false), null, array('escape' => false)); ?>
This produces an escaped version of of ‹ when the link is active but does not escape the HTML when the link is disabled.
I'm using CakePHP 2.4
Additional details:
My model, controller and view is in the Plugin folder
My model is not using a database i.e. var $useTable = false;. I get my data
via a web service.
I've overridden the paginate function in my model so that I can call the paginated web service.
Are you using BoostCake?
I was having the exact same problem. Active links were being escaped, but disabled ones were not.
I disabled the "BoostCake.BoostCakePaginator" plugin, and all is working fine, so I would assume a bug in that plugin. (I have no time to investigate at the moment, but if/when I do, I will report back.)
like gaurav sharma said, there is a bug.
you should replace $this->link($title) in return-value of public function prev(...) and public function next(...) in BoostCakePaginatorHelper.php (ln94 & ln121) by $this->link($title, NULL, array('escape' => false))
like this:
return parent::prev($title, $options, $this->link($title, NULL, array('escape' => false)), array_merge($options, array(
'escape' => false,
'class' => $disabled,
)));

Cake PHP event chaining with Js Helper

Hello you coders out there,
I want to do an event chaining with the Js helper but can´t figure out how to do that with the cookbook.
Scenario:
I have an input field, which is fired onkeyup with ajax. That works fine.
The goal:
The ajax call should be fired after putting 3 digits into the field, not everytime.
My thoughts:
Maybe the chaining of the helper could get me out. I thought the return true would tell the helper to go on, otherwise do nothing. But I didn´t find any info. My code so far:
<?php $check = "
if($(this).val().length >3){
return true;
};"; ?>
<?php echo $this->Js->get('#ajaxSearchSCourier')->event('keyup', $check)->request(
array('controller'=>'Posts', 'action'=>'index'),
array(
'update' => '#erfolgreich_ajax',
'before' => $before,
'success' => $success,
'async' => true,
'dataExpression' => true,
'method' => 'post',
'data'=>$this->Js->serializeForm(array('isForm'=>'false', 'inline'=>'true'))
)
);
?>
I hope you have an idea. Many thanks in advance,
Karl
Forget the JsHelper and write js code yourself. Check my related answer here.

cakePHP paginator not passing passedargs

I am using cakePHP and I am trying to get the paginator component to pass the get variables, or passedargs, when you click through to different pages. I have a variety of different search input selectors which "filters" the results returned. This works on first view, but the moment I click on a different page, it shows all of the results.
I have the following setup for my paginator:
// In my controller class:
public $paginate = array('maxLimit' => 10, 'paramType' => 'querystring');
// Within my action method:
$this->paginate = array('conditions' => array(...),
order => array('Model.field ASC'),
'limit' => 20
);
// Calling the paginator:
$results = $this->paginate('Model');
$this->set(compact('results'));
In my view file:
<div class="paging">
<?php
echo $this->Paginator->prev('< ' . __('previous'), array(), null, array('class' => 'prev disabled'));
echo $this->Paginator->numbers(array('separator' => ''));
echo $this->Paginator->next(__('next') . ' >', array(), null, array('class' => 'next disabled'));
?>
</div>
EDIT:
From my understanding it's better to use the passedArgs, but I am a little unsure as to how to do this. My $this->passedArgs returns no results, so I am creating the passed parameters within my controller example. I also changed my form from Get to Post:
$this->passedArgs["searchfield"] = $_POST["value"];
It passes the passedArgs now correctly in the pagination strip, but I am unsure as to how to build the paging conditions array now. In most cases users will not select default values example, one of the filters is date from and date to, and then a search input box, if I leave the dates it will still created the argumens and not return any results so in essence my url would be something like:
http://localhost/site/controller/action/page:3/datefrom:0/dateto:0/searchFor:survey
Any assistance?
You can pass by all parameters in the view with:
$this->Paginator->options(array('url' => $this->passedArgs));
or assign the params manually:
$this->Paginator->options(array('url' => array("0", "1")));
befor echoing the paginator
See the CakePHP Cookbook for further Examples

Validation problems with multiple checkboxes (HABTM) on CakePHP form

Short version
I have some HABTM checkboxes on a form. Validation is working correctly (at least one checkbox needs to be checked for validation to pass) but the CakePHP error message divs aren't being generated as they should be.
Long Version
I have a from which allows users to fill in their name and email address and then choose from a list of brochures (checkboxes) they'd like to receive.
The form looks like this:
<?php
echo $this->Form->create('Request',array('action' => 'index'));
echo $this->Form->input('id');
echo $this->Form->input('name');
echo $this->Form->input('email');
echo $this->Form->input('Brochure',array(
'label' => __('Information Required:',true),
'type' => 'select',
'multiple' => 'checkbox',
'options' => $list,
'selected' => $this->Html->value('Brochure.Brochure'),
));
echo $this->Form->submit('Submit');
echo $this->Form->end();
?>
In my controller, $list is set as like this:
$this->Request->Brochure->find('list',array('fields'=>array('id','name')));
After reading the 2nd answer (posted by user448164) in HABTM form validation in CakePHP on Stack Overflow, I set my Request model up like this:
<?php
class Request extends AppModel {
var $name = 'Request';
function beforeValidate() {
foreach($this->hasAndBelongsToMany as $k=>$v) {
if(isset($this->data[$k][$k]))
{
$this->data[$this->alias][$k] = $this->data[$k][$k];
}
}
}
var $validate = array(
'name' => array(
'rule' => 'notEmpty',
'message' => 'Please enter your full name'
),
'email' => array(
'rule' => 'email',
'message' => 'Please enter a valid email address'
),
'Brochure' => array(
'rule' => array('multiple', array('min' => 1)),
'message' => 'Please select 1'
),
);
?>
This actually works 99% well. If none of the checkboxes are checked, validation fails as it should do. However, the only problem is that Cake isn't setting the "error" class on the <div>, nor is it creating the <div class="error-message">Please select 1</div> as it should.
For name and email, there is no problem - the error divs are being created properly.
So, to clarify, validation is working for my HABTM checkboxes. The only problem is that the error divs aren't being generated.
I'm posting this here as this is actually a much better question than the related question you found.
I was banging my head against a wall trying to handle the same problem of getting the validation error to show up in the page. I'm using CakePHP v1.2 and I hit a similar problem although I have actually split out the HABTM into the individual tables i.e. Request->BrochuesRequest->Brochure. This is because I can't have it deleting and re-adding the joining table rows at will.
Firstly I think the accepted answer from your linked question assumes that you are doing a save / saveAll when the beforeValidate call is triggered, however I was doing it through a validates call. The difference is that you need to call the Request->set method first. It was an article by Jonathan Snook on Multiple Validation Sets pointed me to that issue.
The second issue is actually getting the error message to appear was down to the $field value you use when calling invalidate. For ages I was including the model as well as the field assuming that this was how it matched the invalidate call to the input, i.e. you have $form->input('BrochuresRequest.brochures_id') so you need $this->BrochuresRequest->invalidate('BrochuresRequest.brochures_id').
However that is wrong you just want $this->BrochuresRequest->invalidate('brochures_id').
<?php
// requests/add view
echo $form->input('BrochuresRequest.brochures_id', array('multiple' => true));
// requests_controller
function add() {
if (!empty($this->data)) {
$this->Request->create();
// critical set to have $this->data
// for beforeValidate when calling validates
$this->Request->set($this->data);
if ($this->Request->validates()) {
$this->Request->saveAll($this->data);
}
}
}
// request model
function beforeValidate() {
if (count($this->data['BrochuresRequest']['brochures_id']) < 1) {
$this->invalidate('non_existent_field'); // fake validation error on Project
// must be brochures_id and not BrochuresRequest.brochures_id
$this->BrochuresRequest->invalidate('brochures_id', 'Please select 1');
return false;
}
return true;
}
?>
A few of the other things that I picked up on the way through:
You don't need a separate $form->error in the view
I couldn't for the life of me get the 'multiple' validation rule to work in the model
The accepted answer checks for an isset but I believe that this isn't required and masked the problem of there being no $this->data being passed through.
The beforeValidate should return false if you want it to prevent any save action.

CakePHP - radio button not showing error message

I'm unable to get the error message to show up when creating a radio form using the CakePHP form helper.
This is what I have now.
$options=array('active'=>'Active','inactive'=>'Inactive');
echo $form->input('Status', array(
'type' => 'radio',
'id' => 'EntryStatus',
'name' => 'data[Entry][status]',
'options' => $options
));
What am I missing?
I'm using CakePHP 1.2.7 and this is what I have in the validation
'status' => array(
'notempty' => array(
'rule' => 'notempty',
'required' => true,
'message' => 'yo'
)
)
Tried the answer from Form helper for creating Radio button in Cakephp and it's giving me a select option form instead.
Thanks,
Tee
had same problem, and i fount this, and it works:
http://book.cakephp.org/view/204/Form-Element-Specific-Methods
you need
if ($form->isFieldError('gender')){
echo $form->error('gender');
}
... in your code. this works in case that your field is named as gender.
I had the same issue and I added:
<?php echo $form->error('currentStatus');?>
below the radio button and it worked fine.
Try taking a look at $form->input('Status' ... (capital 'Status') versus the DB column name (which might or might not be capitalized versus 'name' => 'data[Entry][status]' (not capital 'status').
Cake's form helper is picky about inserting error messages when it can't figure out what things go to what.
Have you tried using the explicit $form->radio() method instead of the general input() method?
You need to manually add in an error form helper.
echo $form->error('status');
You need to add error condition in case of radio button
<?php
$options=array('active'=>'Active','inactive'=>'Inactive');
echo $form->input('Status', array(
'type' => 'radio',
'id' => 'EntryStatus',
'options' => $options
)
);
if ($form->isFieldError('Status')){
echo $form->error('Status');
}
?>

Resources