I want to let user to publish or unpublish some articles at once. So, in articles list view, I want to put a checkbox beside each row (article) and send these checkboxes to controller.
I tried to use $this->Form->input('status') after each row in my loop, but it created same checkbox for each article. (inputs name and id are the same)
How to create an array of checkboxes or something like this? And how to check them in controller?
Note: Each article has a status field, which is a tinyint 1 character field. (so Cake can understand it's a checkbox)
You will need to specify an 'index' for each input.
In stead of this:
$this->Form->input('Article.status');
Use this:
$this->Form->input('Article.0.id');
$this->Form->input('Article.0.status');
$this->Form->input('Article.1.id');
$this->Form->input('Article.1.status');
// ....
$this->Form->input('Article.xxx.id');
$this->Form->input('Article.xxx.status');
It's the 'index' is just a counter to make sure that 'unique' inputs are generated. However, it is important that each row contains an input for the id of that row; CakePHP will need that to determin which record it should update the status for.
Further reading
Documentation on the naming of fields/inputs can be found here:
FormHelper field naming conventions
CakePHP - Create a form which edits multiple rows of the same model
Related
I have three tables/models:
User(id)
Map(id, user_id, attribute_id)
Attribute (id, name)
Map belongsTo the others, the others hasMany Map.
I'd like the user (via user controller and user view) to create many links to associations at once. How can I do this, assuming that the user forms (add/edit) have 10 attribute fields, all linking to the same table?
I'd need to save up to 10 records in Map in one go. To start with, I'm unsure what the field should be - create('Attribute.name')? Also, cake outputs the same input name to each input as they point to the same field - what's the best way to fix this?
I have already read the relevant documentation, but didn't get much from it.
Thanks!
You can use saveAssociated() method. Prepare an input array according to the instructions given in the link and directly use $this->User->saveAssociated($your_array, array('deep' => true));
I have a multiple items form which does not mention the ID of the record. I have a button for each row which, when clicked, opens up a new form containing more details.
In the past, I've added a field for the ID but made it invisible, but this seems silly - the ID is a unique field of the original query, so I should be able to access purely with code somehow.
Does anybody know how to do this? Let me know if you want more clarification.
Update
The fastest way for me to do this is to add a field in the form for the field in the query, call it the query field name, save it all and close it, then delete the field. Though of course I'd rather just be able to do Me.ID without any of that prior nonsense.
In some cases, you may have to refer to Me!ID, rather than Me.ID because the field has not been added as a property of the form (discussion: http://tek-tips.com/viewthread.cfm?qid=1127364 )
When you add the ID as as control and then delete it, it becomes a property of the form, which is why your work-around works.
If a form's record source includes a field named "ID", you can access the field's value as a property of the form:
Debug.Print Me.ID
That works without a control bound to the field.
you probably need to edit the recordsource / query.. and add that column as an available field.
I have scenario where I have 3 different models data being saved from one form, by means of the saveAll pass through method in CakePHP 2.x
The models on the form are:
Project
ProjectImage
ProjectAttachment
1 and 2 are saving perfectly, but I am having problems with the ProjectAttachment fields. If you look at the following image, the fields in question are at the bottom right, highlighted by a red frame: http://img.skitch.com/20120417-bnarwihc9mqm1b49cjy2bp13cf.jpg
Here is the situation:
I have named the fields as follows: *ProjectAttachment.0.project_id*, ProjectAttachment.0.name .... *ProjectAttachment.6.project_id* etc where the numbers are relative to the already present amount of attachments the user has added, as there is no limit. So if the user has ALREADY added 6 documents, the field would be called ProjectAttachment.7.id and so on. This is because of the naming convention when using the saveAll method.
I have made use of two hidden fields, one to store the user ID, the other to store the project ID.
The problem is that if the user does not fill in the Document Title and select a file to upload, the form fails! If I remove all validation rules, the form no longer fails BUT a blank ProjectAttachment record is inserted.
I suspect the problem may also be that the project_id and user_id fields (that are hidden) already have values in them?
I gave it some thought, and came up with a simple concept: In my controller, before the saveAll call, I would check to see if a blank Document Title field was submitted, and if so, I would completely eliminate the relevant array entry from the $this->request->data['ProjectAttachment'] array, but this did not seem to work.
To clarify, I am not looking for validation rules. If the user decides they only want to upload images and not touch the ProjectAttachment form, them the saveAll operation must not fail, it must simple not attempt to save the blank fields in the form, if this is at all possible.
Any suggestions?
Kind regards,
Simon
Well it seems that the solution was far simpler than I had initially thought! I was partially on the right track, and I had to unset the variable ProjectArray key in the $this->request->data array if I had encountered any blank fields.
I did this as follows:
if(!empty($this->request->data['ProjectAttachment'])){
foreach($this->request->data['ProjectAttachment'] as $key => $value){
if(is_array($value) && array_key_exists('upload_file', $value)){
if($value['upload_file']['name'] == ''){ // Blank document file
unset($this->request->data['ProjectAttachment']);
}
}
}
}
I hope someone finds this somehow useful in some form or another.
Kind regards,
Simon
I'm creating a form in cakephp that has a typical HABTM relationship. Let's say that I'm developing an order form with a coupon code. From a logic point of view, I need to accept a text-entry for the coupon code, so the incoming data would not be the proper primary key, but rather a different field.
I would then need to validate that data, and retrieve the proper primary key id, and then update the orders_coupons table with that coupon_id, and order_id.
When using a select box the value would always be the proper coupon_id, but where is the appropriate location for me to put the logic to handle this? Should I modify the data using beforeSave?
your question isn't very clear b/c it sounds like you can both specify the coupon via a select box or in just a free form text box.
My inclination is to add a new method to the Model that would update the record with the "human readable key". So the function would first read the coupon_id from the DB, and then do the update.
As you said, you'll just have to look up the coupon id...
// assuming $data looks like:
// array('Order' => array(...), 'Coupon' => array('code' => ...))
$coupon_id = $this->Order->Coupon->field('id', array('code' => $data['Coupon']['code']));
if (!$coupon_id) {
// user put in a non-existing coupon code, punish him!
} else {
$data['Order']['coupon_id'] = $coupon_id;
$this->Order->save($data);
}
If I get this correct, this is pretty much the same as tagging?! (There is a text input box for habtm items and the string is submitted to the controller without the ids).
If so, I would recommend to split the processing. Submit the data to the controller and then pass the coupon-string to a proper function in the coupon-model, that gets the coupon-ids (saves new items) and returns them back to the controller to save the complete habtm-data.
I am a Cake PHP novice.
I want to edit the table "Issue". This table contains a field "priority_id" related to another table called "Priority" by an foreign key. This table contains three values "Severe", "Disaster", "ToDo". User can select the priority using a combobox (input select).
The priorities are loaded like this:
$priorities = $this->Issue->Priority->find('list');
This works for me.
I need to add a fourth option to the combobox called "Choose". This value will be a default one. The user cannot submit the form when this value is selected. The motivation is to force the user to select one of the meaningful values instead of submitting the first one randomly.
1) How can I fill the array $priorities ?
2) How can i validate the form?
Thanks
In the issues/add.ctp and issues/edit.ctp views, add an empty key to the options array sent as the 2nd param to the $form->input() method, e.g.
echo $form->input('priority_id', array('empty' => 'Choose'));
This will add an option at the top of the combo box with text 'Choose' and the value of the option will be an empty string.
The in your Issue model, you can add a validation for the priority_id field, e.g.
var $validate = array(
'priority_id' => array('numeric')
);