cakePHP - cakeDc-search plugin - PRG pattern - php

Following up on my previous question, I am supposedly passing the submitted post data as query strings like so:
echo $this->Form->create('Donor',array(
'url' => array_merge(array('action' =>'find'), $this->params['pass'])
));
But when I try the following within my controller's find action :
$this->Paginator->settings['conditions'] = $this->Donor->parseCriteria($this->Prg->parsedParams());
The $this->Prg->parsedParams() only consists of the criteria:
here's the var_dump
array (size=1)
'criteria' => string 'blood_group_id' (length=14)
And here is my view code :
<?php
echo $this->Form->create('Donor',array(
'url' => array_merge(array('action' =>'find'), $this->params['pass'])
));
echo $this->Form->input('criteria',array(
'label'=>'Search Criteria',
'options' => array(
'id'=> 'By ID',
'name' => 'By Name',
'blood_group_id' => 'By Blood Type',
'type' => 'By Donor Type',
'age' => 'By Age',
'gender' => 'By Gender'
)
));
?>
<?php echo $this->Form->input('query', array( 'id' => 'query', 'name' => 'query', 'label' => false, 'placeholder' => 'Search')); ?>
<?php echo $this->Form->end(__('Search'));?>
I believe that I should be receiving all submitted data and not only the criteria's value.. I do not know what is wrong, and frankly this is taking so much time to put together. I cant seem to figure out how to work with this plugin. Is it just me ? Please, if anyone could find the time to help out, I will be grateful!

The Answer was almost under my nose.. I inattentively was naming the 'query' input twice,
<?php echo $this->Form->input('query', array( 'id' => 'query', 'name' => 'query','label' => false, 'placeholder' => 'Search')); ?>
I removed the 'name' key, and everything worked fine! That was one stupid mistake, which wasted about an hour for me, so im hoping this would help someone who comes across something like this! Thanks

Related

PHP Zend 2 Framework show Textarea

I'm creating a form using Zend2 framework, and can't figure out why a simple Textarea is not showing up on the view (below you can see my code). I've tried the type Text and it shows a standard single lined text field, but got luck with Textarea. I've also tried a non existing type, and zend throws an exception, so it seems Textarea type actually exists, and I must be missing a mandatory param or something like that. Could anyone point me in the right direction?
$this->add(array(
'type' => 'Zend\Form\Element\Textarea',
'name' => 'pincodes',
'options' => array(
'label' => 'Pincodes (uno por línea)',
),
'attributes' => array(
'rows' => '10',
'cols' => '75',
)
));
SOLVED
My bad, it looks there was an intermediate layer in the project ignoring all the Textarea fields.
I just check you code and found 2 things missing first
use Zend\Form\Element;
use Zend\Form\Form;
Which I think you have used in you file.
Another mistake was you were missing comma in the code. use this below code.
$this->add(array(
'type' => 'Zend\Form\Element\Textarea',
'name' => 'pincodes',
'options' => array(
'label' => 'Pincodes (uno por línea)',
),
'attributes' => array(
'rows' => '10',
'cols' => '75',
),
));
Good luck
Try changing your 'rows' and 'cols' value from a string to integer (remove single quotes).
'attributes' = > array(
'rows' => 10,
'cols' => 75,
);

sort column by date TbJSONGridView

I want to sort a column in TbJSONGridView by date , I have this code
$this->widget(
'bootstrap.widgets.TbJsonGridView', array(
'dataProvider' => $model->searchPending(),
'type' => 'striped bordered condensed',
'summaryText' => false,
'cacheTTL' => 10, // cache will be stored 10 seconds (see cacheTTLType)
'cacheTTLType' => 's', // type can be of seconds, minutes or hours
'enablePagination' => true,
'columns' => array(
array(
'name' => 'Pickup Date',
'value' => '$data->carShippeds[0]->pickup_date',
'type'=>'date',
),
))
but the column I get it is from a related model, and the sorting is not working, when I clicked on the column header it does nothing.
what's wrong with the code???
Gabriel
$data->carShippeds[0]->
This shows us that the current model has many cars shipped. You are only showing the first one in there. While that works, Yii has no way of knowing that you are only showing the first value so your sorting has no chance of working. Create a view that only selects your first pickup date, make a join in your criteria with the main model, show the field without [0]-> and it will work.
Change the below section:
array(
'name' => 'Pickup Date',
'value' => '$data->carShippeds[0]->pickup_date',
'type'=>'date',
),
To this:
array(
'name' => 'pickup_date',
'header' => 'Pickup Date',
'value' => '$data->carShippeds[0]->pickup_date',
'type'=>'date',
),
Then in your 'form' when you want to return DataProvider write the specific sorting code for this association like below:
return new DataProvider($query, [
'pagination' => [
'pagesize' => 20,
],
'sort' => [
'sortVar' => 'sort',
'defaultOrder' => ['t.carShippeds.pickup_date asc'],
'attributes' => [
'pickup_date' => [
'asc' => 't.carShippeds.pickup_date',
'desc' => 't.carShippeds.pickup_date DESC',
],
'*',
],
],
]);
Please notice that I did not know the join alias you have for your t.carShippeds or even whether it is t or another entity. replace it or give me more information about your DataProvider and I can help more.

CakePHP- cakeDC search plugin implementation

I am currently trying to figure out a way to implement cakeDC's search plugin within my application, but I am finding it quite difficult to understand the plumbing that needs to be done before I can get it to work(nicely) with my app.
Things to consider:
the search needs to be a 'live search'
Records retrieved need to be paginated
The search will be done using a selected criteria (id,name,etc the actual key not value)
and will require a user entry which we will call 'query' for now..
here is my code so far.
Model Code :
public $filterArgs = array(
'query' => array('type' => 'query', 'method' => 'filterQuery'),
);
public function filterQuery($data = array()) {
$filter = $data['query'];
$criteria = $data['criteria'];
if(empty($filter)){
return array();
}
$cond = array(
'OR' => array(
$this->alias . $criteria. 'LIKE' => '%' . $filter . '%',
//ie. criteria represents a field $ filter is the data to search/match
));
return $cond;
}
So what I am having trouble with is, how will my filterQuery method receive the $data argument.. Is it a normal request data ? I want to access both values submitted.
Here is the relevant code for the view:
<div id="search-container">
<?php
//echo $this->Form->create(false,array('type'=>'post','default'=>false));
echo $this->Form->input('criteria',array(
'label'=>'Search Criteria',
'options' => array(
'id'=> 'By ID',
'name' => 'By Name',
'blood_group_id' => 'By Blood Type',
'type' => 'By Donor Type',
'age' => 'By Age',
'gender' => 'By Gender'
)
));
?>
<?php echo $this->Form->input('query', array('type' => 'text', 'id' => 'query', 'name' => 'query', 'label' => false, 'placeholder' => 'Search')); ?>
[EDIT]
ofcourse in my controller I also have this setup
Search.Prg Component is loaded
public $presetVars = array(
'query' => array('type' => 'value'),
'criteria' => array('type' => 'value'),
);
Any help is appreciated, even if its just a link to a tutorial. Thanks
When I wrote the plugin a lot of useful examples I put directly into test cases of the plugin. So take a look into behavior test file to see how to use query type method.

How do I use a view helper inside a form in zend framework 2?

I have a view helper that returns an array called $this->getTypes();
I've set it up as an invokable:
'view_helpers' => array(
'invokables' => array(
'getTypes' => 'Account\View\Helper\GetTypes',
),
),
If I echo it in the view can see the array, but in the form it fails.
I would like something like:
$this->add(array(
'name' => 'type_id',
'type' => 'Zend\Form\Element\Select',
'attributes' => array(
'required' => 'required',
),
'options' => array(
'label' => 'Type *',
'value_options' => $this->getTypes(),
),
));
Any ideas?
The short answer is: you don't
The medium answer is: to get DB-Values into your Zend\Form\Element\Select you have to inject your DB-/Service-Layer into your Form.
The long answer is: written in my Blog post Zend\Form\Element\Select and Database Values

CakePHP error validation

I have a cakephp form that has validation. The validation itself works BUT when an error shows up after clicking submit, it just produces some text.
Why am I getting no colour. eg Its meant to display errors in red.
Controller
<div class="users form">
<?php echo $this->Form->create('Ticket'); ?>
<fieldset>
<legend><?php echo __('Purchase'); ?></legend>
<?php
echo $this->Form->input('first_name');
echo $this->Form->input('last_name');
echo $this->Form->input('email');
echo $this->Form->input('phone');
echo $this->Form->input('date', array('options'=> $dates));
echo $this->Form->input('quantity', array('options' => $maxAmount, 'default' => '1'));
?>
</fieldset>
<?php
echo $this->Form->end(__('Purchase'));
?>
</div>
Model
public $validate = array(
'first_name' => array(
'rule' => '/^[a-zA-Z]{1,}$/i',
'message' => 'Alphabets only',
'required' => true
),
'last_name' => array(
'rule' => '/^[a-zA-Z]{1,}$/i',
'message' => 'Alphabet only',
'required' => true
),
'phone' => array(
'rule' => 'numeric',
'message' => 'numbers only please',
'required' => true
),
'email' => array(
'rule' => 'email',
'message' => 'Your email is not valid',
'required' => true
),
'quantity' => array(
'rule' => 'numeric',
'message' => 'numbers only please',
'required' => true
)
);
Did you include a stylesheet in your default.ctp? If you removed the default CakePHP stylesheet from your default.ctp layout, the default colours will no longer be there.
You need to either include the CakePHP stylesheet again in your layout (here you can see how it was in the original default.ctp: https://github.com/cakephp/cakephp/blob/master/app/View/Layouts/default.ctp#L33)
Or create your own CSS styles in your stylesheet. You can use the styles from the default CakePHP stylesheet as an example;
https://github.com/cakephp/cakephp/blob/master/app/webroot/css/cake.generic.css#L371
There is nothing wrong with your code. That is just how CakePHP is handling the error reporting. The red stuff is reserved for major errors like missing view, or a missing function, or cant connect to the database. Basically stuff that would generate a status code that is in the range of 400.
I did some searching to answer your question better, but i stumbled on this page.
CakePHP 2.0 - How to make custom error pages?
Its all about what status code CakePHP will generate when u do something wrong.
Validation errors will I think throw even an OK (200) but wont write anything to the database. Happened a couple a times to me.

Categories