0s1r1s
0s1r1s

Reputation: 1733

Symfony2 isClicked() always false

I'm trying to implement the isClicked() feature of submit buttons in symfony2. I followed the documentation but it doesn't work for me. I always get false.

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?

Upvotes: 3

Views: 2584

Answers (3)

justindgivens
justindgivens

Reputation: 11

It was the javascript that was messing 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 (like below) is worked just fine.

$( '#form_accept' ).click(function( e ) {
    var confirmResponse3 = confirm('{{ 'popup.accept.body'|trans( {} , 'bundle' )|escape('js')  }}');
    return confirmResponse3;
});

Upvotes: 0

xurshid29
xurshid29

Reputation: 4210

I've also faced this problem, and solved it with a little different way, like this:

$clickedButton = $productListForm->getClickedButton()->getName();

if ($clickedButton == 'enable') {
    $product->setStatus(Product::STATUS_ENABLED);
}

if ($clickedButton == 'delete') {
    $product->setDeleted(true);
}

EDIT:

Disabled elements wont be posted by POST or GET methods. If you want to disable double click in submission, you can create a submit button, hide it with CSS, create an another span or p element, style it like submit button, attach to it a litle javascrpt to pass an event to 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();
    });
});

Upvotes: 0

0s1r1s
0s1r1s

Reputation: 1733

Okay that's 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 the submit buttons. But if your button is disabled it won't add it to the post request. So that's why it failed. xurshid29 solution wouldn't help for this issue because php will never know which button was clicked when it's not part of the request.

Upvotes: 2

Related Questions