10

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?

3 Answers 3

26

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/

3
  • please add ")" at the end just before ";", in the second code line after OR. Commented Aug 16, 2017 at 13:06
  • Thx, fixed it by using PHP5.4 [] :)
    – mark
    Commented Aug 16, 2017 at 14:12
  • Aditionally in CakePHP 3.x use 'echo $this->Form->hidden( "any_name", ["value" => $some_value] );' to pass value in model-less forms Commented Jan 5, 2020 at 16:53
0

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.

0

Try following code in cakephp 3 to set hidden field

<?php
        echo $this->Form->hidden('name');
 ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.