Symfony2 isClicked () always false
I am trying to implement the isClicked () function of submit buttons in symfony2. I followed the documentation , but it doesn't work for me. I am always wrong.
the form:
$builder
->add(
'draft',
'submit'
);
Controller:
$form = $this->createForm(
new PageType(),
$page
);
$form->handleRequest($request);
if ($form->isValid()) {
// set status draft if draft button is clicked
if ($form->get('draft')->isClicked()) {
$page->setStatus(Page::STATUS_DRAFT);
}
}
Did I miss something?
source to share
It's good that this is stupid. I added this script a long time ago:
$("form").submit(function () {
$('[type=submit]', this).attr('disabled', true);
});
I used it to avoid multiple clicks on submit buttons. But if your button is disabled, it won't add it to the post request. So why did it fail. Xurshid29's solution would not help solve this problem as php will never know which button was clicked when it is not part of the request.
source to share
I also ran into this problem and solved it a little differently, for example:
$clickedButton = $productListForm->getClickedButton()->getName();
if ($clickedButton == 'enable') {
$product->setStatus(Product::STATUS_ENABLED);
}
if ($clickedButton == 'delete') {
$product->setDeleted(true);
}
EDIT:
Disabled items will not be published by POST or GET methods. If you want to disable double click in the view, you can create a submit button, hide it with CSS, create another span or p element, style it as a submit button, attach a litle javascrpt to it to pass the event to the actual submit button
template:
<div class="form-actions">
<div class="disable-product">
<input type="submit" <!-- other attributes --> class="element-hidden" />
<p class="button-style">Disable</p>
</div>
<div class="remove-product">
<input type="submit" <!-- other attributes --> class="element-hidden" />
<p class="button-style">Remove</p>
</div>
</div>
JS:
jQuery(document).ready(function() {
$('.button-style').on('click', function() {
$(this).siblings('input').mousedown();
});
});
source to share
It was the javascript that was confusing me. I had the following:
$( '#form_accept' ).click(function( e ) {
var confirmResponse3 = confirm('{{ 'popup.accept.body'|trans( {} , 'bundle' )|escape('js') }}');
if( confirmResponse3 == true ) {
$(this).parents( 'form' ).submit();
}
return confirmResponse3;
});
When I submitted the form manual, Symfony didn't know. But when I removed the if statement (as shown below) it works fine.
$( '#form_accept' ).click(function( e ) {
var confirmResponse3 = confirm('{{ 'popup.accept.body'|trans( {} , 'bundle' )|escape('js') }}');
return confirmResponse3;
});
source to share