Convert a text box to dropdown box from Add view - cakephp

Can you help me figure this out please? I have 4 form elements on my add view (app\views\tickets\add.ctp) but the 'status' input is on a text box. I want that to be converted to drop down box populated with data from a field in a table called Status. How do I go about doing it?
echo $this->Form->input('problemno');
echo $this->Form->input('status');
echo $this->Form->input('description');
echo $this->Form->input('user_id');
Thanks,
Lyman

If you want to force the type of input field, do not use the input() method, but use the method for the type you want.
To get a drop down list, you can use the select() method:
$options = array('status1' => 'status1', 'status2' => 'status2', ...);
$this->Form->select('status', $options);
See http://book.cakephp.org/view/1430/select

If the model associated with your 'Status' table is related to you current model ('Ticket', presumably) with a hasMany,hasOne, or belongsTo (...as long as your 'Status' model shows up when you debug $this->Ticket->read(null, <some_ticket_id>)) you can do
echo $this->Form->input('StatusModel.field')
and cake will automagic that field to be whatever it needs to be.
You will have to search around for how to make cakePHP give you a dropdown selection based on database field.

This is not exactly a correct way. It might be good, or you might find changing it later on to be a problem. But if you just want to create a dropdown with some options, here you go:
echo $this->Form->input('status', array('options'=>array('status1'=>'status1','status2'=>'status2','status3'=>'status3')));

Related

CakePHP: Use current parameters to render another view

I have a searchable, sortable table in cakephp. The search and sort criteria are passed as named parameters. Now I would like to add a button which calls a method viewPdf on the very same controller to create a PDF showing the current table content. In fact, I get the correct PDF output just by replacing the action parameter in my url. But how do I get the correct link url for the button with all the current parameters?
I could implode all the values and keys of $this->params->named but I am sure there is a much better way to achieve what I want.
Regards
Alex
You are actually pretty close. You can form such a link using
$url = array('action' => 'pdf') + $this->request->params['named'];
echo $this->Html->link('Title', $url);
Assuming you are using cake2.x (which you always should mention in your question!).

How can input change between normal text type and checkbox in cakephp?

In one of my friend's cakephp project,her colleague wrote a checkbox like this :
https://pbs.twimg.com/media/BKt5D1lCAAA1ZfJ.jpg
And he created it in this way:
echo $this->Form->input('accept_twins', array('label' => 'Accept twins?'));
How could it be!?!!!
If I change anything in the fieldname param 'accept_twins',or copy it to other ctp file,it comes to this:
https://pbs.twimg.com/media/BKuAunRCEAAVAWs.jpg
How could it !!!!!!!!!!!!!!!Whether there are such rules?
Actually,I know how to create a nomarl checkbox,I just can't understand how could this happen?Does anyone know it?
妈蛋!累感不爱!蠢哭了!
The formhelper guesses the input type to use based on db field type. If it shows checkbox it means the field accept_twins is of type TINYINT(1) in the table for that particular model. If you change the field name it will change the input type based on type of that field in db or show input type text by default if the field isn't in db.

Setting the id for a checkbox causes Security error in CakePHP

I am using the Security component in my AppController. I need to build a check form input that will allow custom formatting of each item in the list. To accomplish this, I am processing each item in the $options set using a foreach and creating the new element like so:
foreach ($fileTypes as $fileType_key => $fileType_value) {
echo $this->Form->input(
'FilesIncluded.' . $fileType_key,
array(
'type' => 'checkbox',
'value' => $fileType_key,
'label' => false,
'div' => false,
'before' => '<span class="checkbox clearfix"><span class="check">',
'after' => '</span><label for="add-product-check-sub-cat">' . $fileType_value . '</label></span>',
'hiddenField' => false,
)
);
}
There is a few things to note:
I am setting each check box to data[FilesIncluded][{UUID}] (where UUID actually represents the UUID pf the FilesIncluded) instead of data[FilesIncluded][]
FilesIncluded is not part of the form model, so it will appear in $this->request->data as $this->request->data['FilesIncluded'] instead of $this->request->data['Model']['column']
What I am trying to figure out is why this throws an auth security risk? When I change the field name from 'FilesIncluded.' . $fileType_key to something with a counter in it like 'FilesIncluded.' . $count . '.id', it seems to work without throwing any security auth errors. Any ideas how to make this work the way I am expecting it to?
UPDATE:
The other issue is being able to maintain a fixed set of FileTypes. For example, I want to be able to control the HABTM records that can be selected from the checkbox. For example, I will display this list:
http://cl.ly/image/0b1Q3C0d0w1Y
And only when the user selects the records will they be stored as hasMany. Then when it comes time to edit, I want to not only be able to show the same set of records, but then have them associated to the records the user saved.
(Probable) Cause
You're probably getting the Security error, because you're disabling the hiddenField for the checkboxes. The Security components checks if a Submitted Form is valid (i.e. not tampered with) by calculating a checksum, based on the names of the Form fields, when creating the form and comparing this with the data received when submitting the form.
By suppressing the hiddenField, the checkbox will not be present in the posted data if it is not checked (Non-checked checkboxes are never sent in HTML forms). As explained above, CakePHP calculates a checksum based on the fields/inputs it expects and the actual fields (data) it receives. By disabling the hiddenField, the checksum calculated from the posted data will depend on wether a checkbox was checked or not, which will invalidate the posted data
Workarounds
There may be some Workarounds;
Do not suppress the 'hiddenField' This will make sure that the checkboxes will always be present in the posted data. If a checkbox is not checked, the value of the checkbox will be 0 (zero). If the checkbox is checked, its posted value will be the specified value of the checkbox (or 1 if no value is specified)
Exclude your custom inputs from the checksum. You can exclude fields from the checksum via $this->Form->unlockField('fieldname');. CakePHP will ignore those inputs when calculating the Security checksum.
Documentation: FormHelper::unlockField()
Notes
Although these Workarounds may help, I'd suggest to not re-invent the wheel. By changing the names of your inputs, you're no longer following the CakePHP conventions. Sticking to the conventions often saves you a lot of time.
For example, saving related data can be performed with a single Model::saveAssociated() call. If your Model-relations are properly set, for example:
Document->hasMany->UserFiles, then CakePHP will insert/update both the Document and UserFiles data automatically.
Read the documentation here Saving Related Model Data (hasOne, hasMany, belongsTo)

FormHelper - Pre-populated select drop-down - CakePHP

If I have:
// Controller
$this->Model->id = $id;
$this->request->data['Model'] = $this->Model->read();
And then:
// View (input field)
$this->Form->input('some_field'); // THE FORM FIELD WILL BE PRE-POPULATED
But if I want it to be a select box instead:
// View (with select)
$this->Form->select('some_field', $options); // THE SELECT BOX ISN'T PRE-POPULATED
Questions then:
a. Why isn't the select-box pre-populated like the input field is?
b. Do I really have to manually pre-populate like this?
// View (with select)
$this->Form->select('some_field', $options, array('value' => $this->request->data['Model']['some_field'])); // THE SELECT BOX IS PRE-POPULATED
c. Is the above method the most efficient method of pre-populating select boxes which already has a value?
no, some_field would be prepoluated by your passed form data if you would do it correctly.
did you debug what you produced there? a multi-level array which is not cake standard.
the correct approach would be:
$this->request->data = $this->Model->read();
since the array already contains the Model key (which debug() would have shown!).
but careful to do this only if not posted!
to your last question, no, if possible, use the controller or at least default. value would make your form lose the previously selected value if validation fails.
my old cake1.3 post might also shed some light on it: http://www.dereuromark.de/2010/06/23/working-with-forms/

dropdown select in cakePHP

I am using CakePHP 1.2. I have a person model that hasMany 'Document'. When I edit a document, the select box for the owning person appears (echo $form->input('person') where person has been defined in the documents_controller like this:
$allPeople = $this->Document->Person->find('list', array('fields' => array('first_name')));
$this->set('people', $allPeople);
When I edit a document's record, I want the person owning the document to be selected and displayed in the box. Right now, the app just makes the list box but doesn't highlight the correct owner (though the DB has the person's id).
Thank you,
Frank Luke
In your edit view, you should add an extra parameter to your $form->select(), called $selected. This way, you can specify which item should be selected from the list.
Example (just an example, you should rewrite it for your own situation):
<?php echo $form->select('Document.person', $allPeople, $this->data['Document']['Person']['id']); ?>
More information:
http://book.cakephp.org/view/728/select
-- Bjorn

Resources