Using code that has been baked into CRUD, I have the following code for deleting an item:
<?php echo $this->Form->postLink(__('Delete'), array('controller'=>'attachments', 'action' => 'delete', $attachment['Attachment']['id']), null, __('Are you sure you want to delete "%s?"', $attachment['Attachment']['name'])); ?>
The problem is that it lies wrapped in a FORM tag, and so what ends up happening is Cake doesn't include the Form that the postLink would submit.
Is there another way that still holds true to the integrity of Cake's infrastructure that would work even when I increase the security settings? Probably needs to be a link to /attachment/delete/id, but baking for some reason chose to create a form and post it vs. creating a link, so I figured there was a reason for that and if so I want to uphold that reason.
You probably didn't read the warnings in the doc block regarding this method
(http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::postLink)
This method creates a <form> element. So do not use this method inside an existing form.
Instead you should add a submit button using FormHelper::submit()
So don't do that. You would need to write to a buffer and output later (see this closed PR).
I have the same problem in cakephp 3.0 $this->Form->postLink() was not working for first entry.
Then i do some R&D but not found any useful. Then i make some changes in form tag and $this->Form->postLink(). I remove the $this->Form->create() from the .ctp files and use only $this->Form->postLink(); and it start working.
Do not use the $this->Form->postLink() inside any other form tag
i.e $this->Form->create(null, ['url' => ['action' => 'ExportCustomers']]);
If you want both then you have to adjust the $this->Form->postLink() and form tag according to it will not effect each other.
Related
Using code that has been baked into CRUD, I have the following code for deleting an item:
<?php echo $this->Form->postLink(__('Delete'), array('controller'=>'attachments', 'action' => 'delete', $attachment['Attachment']['id']), null, __('Are you sure you want to delete "%s?"', $attachment['Attachment']['name'])); ?>
The problem is that it lies wrapped in a FORM tag, and so what ends up happening is Cake doesn't include the Form that the postLink would submit.
Is there another way that still holds true to the integrity of Cake's infrastructure that would work even when I increase the security settings? Probably needs to be a link to /attachment/delete/id, but baking for some reason chose to create a form and post it vs. creating a link, so I figured there was a reason for that and if so I want to uphold that reason.
You probably didn't read the warnings in the doc block regarding this method
(http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::postLink)
This method creates a <form> element. So do not use this method inside an existing form.
Instead you should add a submit button using FormHelper::submit()
So don't do that. You would need to write to a buffer and output later (see this closed PR).
I have the same problem in cakephp 3.0 $this->Form->postLink() was not working for first entry.
Then i do some R&D but not found any useful. Then i make some changes in form tag and $this->Form->postLink(). I remove the $this->Form->create() from the .ctp files and use only $this->Form->postLink(); and it start working.
Do not use the $this->Form->postLink() inside any other form tag
i.e $this->Form->create(null, ['url' => ['action' => 'ExportCustomers']]);
If you want both then you have to adjust the $this->Form->postLink() and form tag according to it will not effect each other.
So i have this field i want to keep hidden in my form.
For this purpose i have tried the following:
<?php echo $this->Form->input('group_id', array('hiddenField' => true, 'value'=> 2)); ?>
I also tried:
<?php echo $this->Form->input('group_id', array('options' => array('hiddenField'=> 'true'), 'value'=>2 )); ?>
How ever i still see the input field..
What am i doing wrong?
You misread the documentation, I assume.
hiddenField is to enable/disable specific hidden fields for specific form fields.
You are either looking for
$this->Form->hidden('group_id')
or
$this->Form->input('group_id', ['type' => 'hidden']);
I usually only use the latter.
See http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html
BUT - that said - you shouldnt actually use either one of those. And omit any fields that serve no real purpose for the view and its form.
Instead you should inject those fields into the data array prior to saving.
See http://www.dereuromark.de/2010/06/23/working-with-forms/
If you are looking to add a hidden field that uses a related second data array that will not be passed via post or put by default, you can use this to pass it:
echo $this->Form->hidden('Group.name');
This is useful for echoing out edit page titles when the post or put encounters an error. A dynamic title can lose Group.name data array when your form is set up such as this:
<h1>Edit Group - <?php echo h($this->request->data['Group']['name']); ?></h1>
For data that is to be saved to db however, follow Mark's suggestion above.
Try following code in cakephp 3 to set hidden field
<?php
echo $this->Form->hidden('name');
?>
I have a CListView that uses ajaxLink() in the _view file.
View (index.php)
<?php
Yii::app()->clientScript->registerScript('ajaxUpdate',
"
//javascript function to update the listview using ajax
function updateItemList(){
$.fn.yiiListView.update('itemList');
return false;
}
", CClientScript::POS_READY);
?>
<?php $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
'id'=>'itemList',
)); ?>
partial (_view.php)
<?php echo CHtml::ajaxLink('Delete',array('libdbitems/delete','id'=>$data->id),
array('type'=>'POST','success'=>'function(){updateItemList()}'),
array('confirm'=>'Are you sure you want to delete this item?',
'id'=>'delete-'.$data->id)); ?>
The controller is basically just the default actionIndex() that is generated with Gii.
Here's the problem: when I click my Delete link the first time after a page load, it behaves as expected. After that, clicking Delete does nothing. (It refreshes the ListView, but no changes are made.)
I'm pretty sure the problem lies in how Yii is binding click() events to my links in javascript, but I have no idea how to fix it. I have tried using the live=true option as others have suggested, but it does nothing.
Does anyone know how to fix this issue so that my Delete link works multiple times without having to reload the page?
Is your delete link part of the itemlist that's refreshed? If that's the case, the script won't re-register with the new link when a new link is created.
Two options:
1) Make sure your link is not being refreshed, and is a permanent aspect of the page
2) Write a custom jQuery handler rather than using Yii's ajaxLink. You'll need to use .on and delegated events. Something of the form:
$("#parentContainer").on("click", ".deleteLinkClass', updateItemList)
Where the parentContainer is a permanent item on the page, and deleteLinkClass will be a class you'll need to assign to the delete links you're using.
Eh, a stupid fix. I realized I had accidentally left a CHtml::$liveEvents = false in my controller that I put there while I was still earlier in the troubleshooting phase.
The solution is just to leave CHtml::$liveEvents = true (default) and to make sure all the links have unique IDs.
I currently have a view, manage, that lists data items. Instead of running "edit" and "add" actions with separate views, I'm having the appropriate form display inline in the page when it is needed. On the form is a "cancel" button that will hide it.
Right now I decide what, if any, form to display inline through a GET parameter called mode. So /controller/manage will display the items, while /controller/manage?mode=edit will display the items and show the edit form for the correct data item.
The action accepts various other GET parameters such as a date interval.
In order to activate the form, I'm generating a link to the current action with the mode parameter spliced in with existing parameters in $_GET. I'm using the following one-liner:
<?php echo CHtml::link('Edit', array_merge(array('/session/manage', 'mode' => 'edit', 'id' => 6), $_GET)); ?>
To implement the "cancel" link, I'm using:
<?php echo CHtml::link('Cancel', array_diff(array_merge(array('/session/manage'), $_GET), array('edit'))); ?>
My question is: is there a cleaner way to do this? While this works, it feels like a hack. I haven't found any documentation on generating URLs that include existing GET parameters. Does Yii provide a built-in method to do what I want to do? Should I re-evaluate my general approach to conditional views, which might in turn yield a cleaner solution?
One way would be to hold all of the most recent _GET variables in session state. Your controller would then act on that stored data and use new _GET variables to override it. Using that method, you wouldn't need to merge the existing _GET variables back into the new request. The tradeoff is that you end up having to manage that session data and handle possible special cases (like starting over with fresh state).
Another way would be to use Ajax to call an action that returns a partial view with your rendered edit form. Then your links would just need to call some javascript that does the Ajax call with the current ID and displays the returned form on the page. Your links would end up looking something like this:
<a href="#" onclick="showEdit(6)">
Since using Ajax wouldn't cause the page to reload, you don't need to worry about any of the _GET parameters required to show your list. And your Cancel link would become a javascript call to simply remove the edit form from the DOM. Using jQuery:
$('#editform').remove();
Another way to make it a little cleaner would be to encapsulate all of the array_merge() stuff up there into a method of a custom component or helper. Then your code could look something like:
echo CHtml::link('Edit', URLHelper::mergeGet(array('/session/manage', 'mode' => 'edit', 'id' => 6));
or even ecapsulate that in your own CHtml class:
echo MyCHtml::link('Edit', array('/session/manage', 'mode' => 'edit', 'id' => 6));
where MyCHtml::link() does everything that CHtml::link() does in addition to the array_merge stuff. MyCHtml could even extend CHtml so that it has all of CHtml's methods.
I have controller, 'products', with the actions 'add' and 'edit'.
I made the view 'add.ctp' which has a long form. Was wondering if I can use that same view for generating display 'edit'. What would I need to put in the controller, under edit() {}, to make it output to 'add.ctp' instead of 'edit.ctp'?
Thanks.
You could use:
$this->render('add');
at the end of your controller's edit function, but I wouldn't recommend it.
A better solution would be to do a small refactor to put the contents of your add.ctp into an element (e.g. app/views/elements/productForm.ctp), and then use that element from both the add.ctp and edit.ctp views.
<?php echo $this->element('productForm'); ?>
This gives you the flexibility to wrap the form with action specific elements, and do any setup that might be specific to that action. For example, under the product form you might have a different set of actions, such as "View Product" that doesn't make sense in add mode but does in edit mode.
#kaklon made a very good point, you should put a little bit of logic into the productForm element to make sure edit mode behaves correctly:
if ($this->action == 'edit') {
echo $this->Form->input('id');
}
Pseudocoder wrote a very good article on this: http://www.pseudocoder.com/archives/category/cakephp/page:4
He has since improved this even more by using routes to eliminate the add action altogether I think. You may be interested in his compilation of CakePHP tips & hacks: http://www.pseudocoder.com/free-cakephp-book/
yes you can, This is one way to do it... https://github.com/infinitas/infinitas/blob/dev/app_controller.php#L374
that allows you to use one file when there is only one available, or add/edit when you need different stuff
You are looking for elements. The view is not 100% identical, because the edit form needs to contain the id of the product you want to edit, while the add form does not have an id..