Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP hidden _method POST

Tags:

html

cakephp

When using the FormHelper->create(...), the HTML that gets rendered looks like this:

<form action="/blogs/add" method="post" accept-charset="utf-8">
    <div style="display:none;">
        <input type="hidden" name="_method" value="POST">
    </div>

    <!-- omitted: form inputs -->
</form>

Why is that div with the display:none; style there? How do I make it not show up?

UPDATE: Just to be clear, I'm wondering why both the div AND the hidden input inside the div show up. They don't seem to be necessary, and therefore I don't want them to be there.

like image 855
Matthew Groves Avatar asked Jun 19 '26 07:06

Matthew Groves


1 Answers

For anyone coming to this recently, there is a simple solution to this now that doesn't involve a custom helper. Using the FormHelper templates, the block of code in question is generated by the 'hiddenBlock' template. (See the full list of default templates here: https://api.cakephp.org/3.2/class-Cake.View.Helper.FormHelper.html#%24_defaultConfig).

So, to amend the example given in CakePHP's documentation to match this case and remove the wrapping <div> around the hidden <input> for _method (assuming HTML5):

// In your View class
$this->loadHelper( 'Form' , [ 'templates' => 'app_form' ] );


// in config/app_form.php
return [
    'hiddenBlock' => '{{ content }}'
];

I was confronted with this problem because I recently implemented a Content Security Policy that doesn't allow inline styling, and I thought I should share my working solution.

like image 166
mesertes Avatar answered Jun 21 '26 22:06

mesertes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!