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?
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/
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.
Try following code in cakephp 3 to set hidden field
<?php
echo $this->Form->hidden('name');
?>
Related
Using code that has been baked into CRUD, I have the following code for deleting an item:
<?php echo $this->Form->postLink(__('Delete'), array('controller'=>'attachments', 'action' => 'delete', $attachment['Attachment']['id']), null, __('Are you sure you want to delete "%s?"', $attachment['Attachment']['name'])); ?>
The problem is that it lies wrapped in a FORM tag, and so what ends up happening is Cake doesn't include the Form that the postLink would submit.
Is there another way that still holds true to the integrity of Cake's infrastructure that would work even when I increase the security settings? Probably needs to be a link to /attachment/delete/id, but baking for some reason chose to create a form and post it vs. creating a link, so I figured there was a reason for that and if so I want to uphold that reason.
You probably didn't read the warnings in the doc block regarding this method
(http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::postLink)
This method creates a <form> element. So do not use this method inside an existing form.
Instead you should add a submit button using FormHelper::submit()
So don't do that. You would need to write to a buffer and output later (see this closed PR).
I have the same problem in cakephp 3.0 $this->Form->postLink() was not working for first entry.
Then i do some R&D but not found any useful. Then i make some changes in form tag and $this->Form->postLink(). I remove the $this->Form->create() from the .ctp files and use only $this->Form->postLink(); and it start working.
Do not use the $this->Form->postLink() inside any other form tag
i.e $this->Form->create(null, ['url' => ['action' => 'ExportCustomers']]);
If you want both then you have to adjust the $this->Form->postLink() and form tag according to it will not effect each other.
Using code that has been baked into CRUD, I have the following code for deleting an item:
<?php echo $this->Form->postLink(__('Delete'), array('controller'=>'attachments', 'action' => 'delete', $attachment['Attachment']['id']), null, __('Are you sure you want to delete "%s?"', $attachment['Attachment']['name'])); ?>
The problem is that it lies wrapped in a FORM tag, and so what ends up happening is Cake doesn't include the Form that the postLink would submit.
Is there another way that still holds true to the integrity of Cake's infrastructure that would work even when I increase the security settings? Probably needs to be a link to /attachment/delete/id, but baking for some reason chose to create a form and post it vs. creating a link, so I figured there was a reason for that and if so I want to uphold that reason.
You probably didn't read the warnings in the doc block regarding this method
(http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::postLink)
This method creates a <form> element. So do not use this method inside an existing form.
Instead you should add a submit button using FormHelper::submit()
So don't do that. You would need to write to a buffer and output later (see this closed PR).
I have the same problem in cakephp 3.0 $this->Form->postLink() was not working for first entry.
Then i do some R&D but not found any useful. Then i make some changes in form tag and $this->Form->postLink(). I remove the $this->Form->create() from the .ctp files and use only $this->Form->postLink(); and it start working.
Do not use the $this->Form->postLink() inside any other form tag
i.e $this->Form->create(null, ['url' => ['action' => 'ExportCustomers']]);
If you want both then you have to adjust the $this->Form->postLink() and form tag according to it will not effect each other.
I have my form fields where I am appending an id within a for loop.
<?php echo $this->Form->input('Shipment.current_city'.$sh, array('label' => 'City')); ?>
I would like to know how to validate such dynamic fields. Currently Cake is not recognizing my validation rules from my model due to the appended id.
Thanks.
You can find the solution of your issue here.
http://php-dev-zone.blogspot.com/2013/08/dynamic-fields-validation-in-cakephp.html
I think I've found a bug in Cake's FormHelper class, but maybe I'm just not using the correct function, so hopefully somebody on here knows. Here's the relevant code:
<?php
echo $this->Form->input('first_name', array('value' =>'First Name', 'label' => false));
?>
As you can see, I'm just outputting an input text field that has a default value of First Name. The issue I'm experiencing is that if the user does not provide a valid name, then the form does not preserve the value that the user tried to submit with, instead it just shows the default value again (First Name). So basically, if I were to enter into the field "hey123" and then click submit, the form will show the appropriate validation error message next to the text field, but the text field itself won't contain my previous entry of hey123, it will show the First Name text again.
Is it possible to have the input field show the default value initially while also allowing whatever the user entered into the form to be preserved when validation errors occur? So in the previous example, instead of showing the First Name text on the error validation page, it'd show my original entry that caused the issue, hey123.
Use:
<?php
echo $this->Form->input('first_name', array('default' =>'First Name', 'label' => false));
?>
Note, use default instead of value. Default will set a 'default' value, while value will explicitly set the value of that field.
I have got a form which a user can use to create a new store and to edit an existing one. When this form is being used to edit a store there are certain fields that I want the user to see but not edit eg. store_id. I have explored the different Zend_Form_Elements hoping to find some kind of static element but with no luck.
So my question is, how do I display information using Zend_Form that a user can't edit?
Thanks.
readonly alone is not enough, because users will still be able to edit it if they really want. You should use $element->setIgnore(true) that will ensure that Zend_Form_Element won't try to populate the element from POST/GET, and I'd double check that also. You have to make sure the values you are getting into the databases can never contain this element.
Finally, if you would like your element to be displayed in a different way than just with readonly, you can do that by changing the element decorators.
I just managed to work this one out myself. The solution was to change the view helper on the elements to the formNote helper eg. $element->helper = 'formNote'. The result of this was that the value gets displayed as straight text instead of being inside a form element.
Thanks for your answers.
That's very good solution when you don't need to populate the element value when the form is submitted.
It's equivalent solution is to use the Form Element method setAttrib() and disable the form element
$formElement->setAttrib('disable','disable')
which will only freeze the element.
But if you need to populate the field, using the previous solutions you will probably need additional hidden field added, which will pass the value. Developing custom form element will be good style but that's not welcomed by each developer so you can use some tricky way to set a form element as a text only but populate its value. That way is when you create the element as a hidden field, set its value and use the Form Element method setDescription() to set and display the element text value.
$formElement = new Zend_Form_Element_Hidden( 'elName',
array( 'label' => 'elLabel', 'value' => 'elValue' ) );
$formElement->setDescription( 'elValue' );
Then you can render that hidden element and display the value with the
$formElement->getDescription().
$element->setAttrib('readonly', 'true');
http://www.w3.org/TR/html401/interact/forms.html#adef-readonly
According to Amr Mostafa, if you use:
$element->setAttrib('readonly', 'true');
OR
$element->setAttribs(array('disabled' => 'disabled'));
User still send values by POST/GET and they are stored in DB.
The only way for me to don't taking into account the values from POST/GES is:
$element->setIgnore(true)
Example:
$element = new Zend_Form_Element_Text('element');
$element->setIgnore(true);