How to use FormHelper::postLink() inside of a form? - php

I want to create a Cakephp delete post link within form like the following. But the very first delete post button doesn't include delete Form when I inspect in browser and can't delete but the others include as I want and can delete.
Is it cakephp bug or something I need to change my source code?
<?php
echo $this->Form->create('Attendance', array('required' => false, 'novalidate' => true));
foreach($i = 0; $i < 10; i++):
echo $this->Form->input('someinput1', value => 'fromdb');
echo $this->Form->input('someinput2', value => 'fromdb');
echo $this->Form->postLink('Delete',array('action'=>'delete',$attendanceid),array('class' => 'btn btn-dark btn-sm col-md-4','confirm' => __('Are you sure you want to delete')));
endforeach;
echo $this->Form->button('Submit', array('class' => 'btn btn-success pull-right'));
echo $this->Form->end();
?>

Forms cannot be nested, the HTML standard forbids that by definition. If you try to, most browsers will drop the nested form and render its contents outside of the parent form.
If you need post links inside of existing forms, then you must use the inline or block options (available as of CakePHP 2.5, inline has been removed in CakePHP 3.x), so that the new form is being set to a view block that can be rendered outside of the main form.
CakePHP 2.x
echo $this->Form->postLink(
'Delete',
array(
'action' => 'delete',
$attendanceid
),
array(
'inline' => false, // there you go, disable inline rendering
'class' => 'btn btn-dark btn-sm col-md-4',
'confirm' => __('Are you sure you want to delete')
)
);
CakePHP 3.x
echo $this->Form->postLink(
'Delete',
[
'action' => 'delete',
$attendanceid
],
[
'block' => true, // disable inline form creation
'class' => 'btn btn-dark btn-sm col-md-4',
'confirm' => __('Are you sure you want to delete')
]
);
Close the main form and output post link forms
// ...
echo $this->Form->end();
// ...
echo $this->fetch('postLink'); // output the post link form(s) outside of the main form
See also
CakePHP 2.x
API > FormHelper::postLink()
Cookbook > Views > Using view blocks
CakePHP 3.x
API > \Cake\View\Helper\FormHelper::postLink()
Cookbook > Views > Helpers > Form > Creating Standalone Buttons and POST links
Cookbook > Views > Using View Blocks

Related

How to make multiple values selected in dropdown in cakephp 3.x

<div class="form-group">
<?php
echo $this->Form->input('area', array('label' => false,
'placeholder' => 'Enter Zone Name',
'type' => 'select',
'class' => 'form-control',
'id'=>'area',
'multiple' => 'multiple',
'options' => $areaList)
);
?>
</div>
This is my dropdrop for input type select on edit page.
I just want to know how can I make $arealist values to be shown selected.
I am using Cakephp 3.x. I am new to cakephp 3.x.
Pass the keys of $areaList (which should be a find('list') style resultset/array) to either the default option (which will be used unless the form context contains data for the field, for example the submitted form data), or to the value option (which will hard-select the given values, ie possible form context data will not override it).
// ...
'options' => $areaList,
'default' => array_keys($areaList)
See also
Cookbook > Views > Helpers > Form > Common Options For Specific Controls
Cookbook > Views > Helpers > Form > Creating Select, Checkbox and Radio Controls

CakePHP: how to make a submit button with link, which stores the request data

I have a Form in Cakephp(version 2.4.4):
<?php echo $this->Form->create('Servicegroup');?>
I have to find a way to submit this form using a button, which contains a link. Besides I need to have all the request data from this form when I access the link. How could I implement this in the best way?
I am tryin with:
echo $this->Html->link($this->Form->button('calculateBudget', array('class' => 'btn btn_yellow', 'type' => 'submit')), array('controller' => 'orders', 'action' => 'calculateBudget'), array('class' => 'lbOn', 'escape' => false));
but this does not submit the form

Additional button in Yii2 form submits the form instead of calling its action

I have a typical Yii2 form for updating my model with a typical submit button. Next to it, I have a "Delete photo" button, that appears, if there is any photo to delete. The piece of view looks like that:
<?= Html::submitButton('Save changes', ['class' => 'btn btn-primary', 'name' => 'edit-button']) ?>
<?php $image = isset($page->photo) ? $page->photo->getImageUrl() : null; ?>
<?php if (isset($image)): ?>
<?= Html::a('Delete photo', ['delete-image', 'lab' => $lab->id, 'kind' => $page->kind], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => 'Do you really want to delete this photo?',
'method' => 'post'
],
]) ?>
<?php endif; ?>
When there is a photo attached to this model and these two buttons appear next to each other, I must comment out 'method' => 'post' part in second button code. Because, if I don't this this, the second button is... submitting the form (just like the first one) instead of calling lab/delete-image route.
This is the first thing, that I don't understand. The entire code is either generated by Gii or copy-pasted from some Yii tutorials. Not even a bit of my invention and yet it works strangely. What am I missing?
It seems, that normal Html::a link (only styled by Twitter Bootstrap to look like a button, but not being a button at all) is submitting a form, instead of calling its action, when it contains data-method="post" attribute in element code. Is this a bug in Yii2 or am I missing something?
You need to place the link outside of the form. For calling actions from elements with data-method attribute yii has js function handleAction, and its documentation says:
This method recognizes the data-method attribute of the element. If the attribute exists, the method will submit the form containing this element. If there is no containing form, a form will be created and submitted using the method given by this attribute value (e.g. "post", "put").
For hyperlinks, the form action will take the value of the "href" attribute of the link.
Also if you use yii2 v2.0.3 or higher you can add data-params attribute which value should be JSON representation of the data, and this data will be submitted in request. As example:
echo Html::a('Delete image', ['delete-image'], [
'data' => [
'confirm' => 'Do you really want to delete this photo?'
'method' => 'post',
'params' => [
'lab' => $lab->id,
'kind' => $page->kind,
],
],
]);
In this example params array will be json encoded by yii2 internaly

cakePHP 3.0 and bootstrap glyphicons

I want to add glyphicons instead of text in my index, add, edit views.
This works in the index.ctp
<?= $this->Html->link(__('<i class="glyphicon glyphicon-pencil"></i>'), ['action' => 'edit', $user->user_id], array('escape' => false)) ?>
But when I do it for the delete action it shows me the glyphicon but it doesn't give me the 'Are you sure you want to delete the user?' anymore
<?= $this->Form->postLink(__('<i class="glyphicon glyphicon-minus"></i>'), ['action' => 'delete', $user->user_id], array('escape' => false), ['confirm' => __('Are you sure you want to delete {0}?', $user->username)]) ?>
In the view.ctp it breaks the code that comes after so the content that comes after isn't shown. (in this example that's the content after the glyphicon-pencil. The glyphicon-pencil itself isn't shown as well.
<?= $this->Html->link(__('<i class="glyphicon glyphicon-pencil'), ['action' => 'edit', $user->user_id], ['escape' => false]) ?>
Take a closer look at the arguments that you are passing, you are passing 4, where the method only accepts 3, ie the confirm option is not passed in the actual options argument.
Proper formatting helps a lot to spot such mistakes.
<?=
$this->Form->postLink(
__('<i class="glyphicon glyphicon-minus"></i>'),
[
'action' => 'delete',
$user->user_id
],
[
'escapeTitle' => false,
'confirm' => __('Are you sure you want to delete {0}?', $user->username)
]
)
?>
And your FormHelper::link() example is missing a closing double quote for the <i> elements class attribute, as well as a closing tag for the element itself:
'<i class="glyphicon glyphicon-pencil"></i>'
Also you may want to use escapeTitle instead of escape, in order to avoid disabling escaping for attributes too.

Custom Data Attribute not Populating Value

I have a simple delete function. Here is where I set the custom data:
<?php echo $this->Html->link(
__('Delete'),
'#CoursesModal',
array(
'class' => 'btn-remove-modal',
'data-toggle' => 'modal',
'role' => 'button',
'data-uid' => $course['Course']['id'],
'data-uname' => $course['Course']['name']
));
?>
It shows in the element fine:
Delete
But when it comes to deleting this object (here's the code for it:)
<?php echo $this->Html->link(__('Delete'),'/courses/delete/#{uid}',array('class' => 'btn btn-danger delete-course-link')) ?>
For some reason the {uid} doesn't translate to 5 - which causes it not to work.
What am I doing wrong?

Categories