This page actually a preview where user can't change anything,that he has given before.I have tried bellow code,
echo $this->Form->input('exchange_type', array(
'disabled' => 'disabled',
'empty' => '--Please Select--',
'options' => array(
'6' => 'POINT_TO_PRODUCT',
'7' => 'POINT_TO_GIFT',
'2' => 'POINT_TO_GAME'
)
));
Here field has disabled but it's sending null value to database.I am trying to send actual value that user has been selected.How can I do this ?
That's how HTML works, values of disabled elements are not being sent.
What you can do is using a hidden field, that's what the form helper automatically does when using for example checkboxes, in order to ensure that there's always a value being sent, as unchecked checkboxes do not submit any value, just like disabled inputs.
The hidden field should have the same name as the actual field, and it should be placed before the actual field, that way the hidden value will only be sent in case the following element is disabled.
echo $this->Form->hidden('exchange_type');
echo $this->Form->input('exchange_type', array(
'disabled' => true,
// ...
));
That would pick up the previously POSTed value for both the hidden input and the select input, and the hidden input would be submittable.
See also Cookbook > Helpers > FormHelper > FormHelper::hidden()
Related
In my form I have a input field with multiple checkboxes.
This works as expected.
When for some reason (eg an obligated field in the form is empty upon submit) the form after submit is not posted, all other fields maintain there input except the field below, all checkboxes become unchecked.
What can I do to make this field remember it's settings?
$options = array(
'1' => 'one',
'2' => 'two',
'3' => 'three'
);
echo $this->Form->input('checkboxes',array(
'type' => 'select',
'multiple' => 'checkbox',
'options' => $options,
'default' => array(1,2,3)
));
FormHelper uses the $this->request->data to fill the fields. You need to keep (or fill) your data in request data array and the cakephp will maintain your data.
I have created a form where input fields are shown/hidden based on selections and i like to post only the input fields which are visible. At the moment the values of the hidden input fields are also submitten
I checked the $_POST and i see
[chairs] => Array
(
[0] => chair-b-white
[1] => chair-c-black
)
[num_of_chair-a-black] =>
[num_of_chair-a-white] =>
[num_of_chair-a-gray] =>
[num_of_chair-b-black] =>
[num_of_chair-b-white] => 2
[num_of_chair-b-gray] =>
[num_of_chair-c-black] => 5
[num_of_chair-c-white] =>
[num_of_chair-c-gray] =>
Is there a way to skip posting input fields which are empty?
I assume you toggle the fields' visibility using javascript. When hiding them you can also set them to disabled. Disabled form fields are not submitted to the server.
I have a form which has some disabled fields, when the form is submitted both $this->request->data and $_POST is empty, removing the disabled fields and it is fine again. I would have though it would still pass though the non-disabled fields. I've even tried to remove the disabled field attribute when the submit button is pushed but this still returns an empty array.
Is there something cake related that might be causing this?
Thanks
// SNIPPET FROM THE VIEW CODE:
$this->Form->create('Card', array('class' => 'GeneralValidate'));
$this->Form->input('Card.property_id', array('type'=>'select', 'empty'=>true , 'class' => 'required adminOnlyField', 'div' => array('class' => 'required')));
$this->Form->input('Card.building_id', array('type'=>'select', 'empty'=>true, 'id' => 'BuildingSelector', 'class' => 'adminOnlyField', 'label' => 'Building (If Applicable)'));
$this->Form->input('Prospect.waiting_list_details', array('value' => $prospect['Prospect']['waiting_list_details']));
$this->Form->input('SaleDetail.property_sold', array('class' => 'checkbox', 'checked' => $ps_checked));
$this->Form->input('SaleDetail.date_conditions_met', array('type'=>'text', 'class' => 'text date_picker adminOnlyField', 'value' => $this->Date->format($saledetail['SaleDetail']['date_conditions_met'])));
$this->Form->button('Save & Continue', array('type'=>'submit', 'label' => 'Save', 'name' => 'quicksave' , 'class' => 'submit long clear_ready_only'));
// JS FROM THE VIEW
$(function () {
var $adminOnly = $('.adminOnlyField');
$adminOnly.prop('disabled',true).prop('readonly',true);
$adminOnly.attr("onclick","return false");
$adminOnly.attr("onkeydown","return false");
$adminOnly.removeClass('required');
$adminOnly.removeClass('date_picker');
$('.clear_ready_only').click(function(e)
{
e.preventDefault();
$adminOnly.prop('disabled',false).prop('readonly',false);
$adminOnly.attr("onclick","return true");
$adminOnly.attr("onkeydown","return true");
$('#CardModifysaleForm').submit();
});
});
That's the way HTML works, disabled don't get posted. CakePHP can't change what is sent from the browser. If you still want the value you can set it as a hidden element.
Update
Some problems I see:
Missing Form::end() in view (always a good idea to insert it).
You never said your form was submitted from JS, first test with a simple form POST then JS.
Your JS code is set to submit a form by ID CardModifysaleForm. There's no such ID in your supplied view code and you're not setting your form to that ID from the snippet you supply.
I ended up removing the disabled option from this, leaving the ready only and added some addition CSS stylings so it looked disabled to the user. This is not the exact answer to the question but works as a different approach.
In my form I've created a hidden field:
echo $this->Form->hidden('editor_rating', array('value' => 0));
Which outputs:
In my model I've created a validation rule:
'editor_rating' => array(
'rule' => array('comparison', 'greater or equal', 1),
'message' => 'Please choose a valid Editor Rating'
)
When I submit the form the hidden field has an error class added, but no visible change and no error message:
<input id="ListingEditorRating" class="form-error" type="hidden" value="0" name="data[Listing][editor_rating]">
How can I make this error message visible or even attach it to a different div?
FormHelper::error
For use cases where Form->input or Form->inputs are not used you can render errors explicitly:
if ($this->Form->isFieldError('gender')) {
echo $this->Form->error('gender');
}
OK, so it doesn't look like there is any built in method to handle what I need which is understandable, so I'm handling it manually by checking the validationErrors for the field.
Here is a cleaner example than the editor_rating field I used perviously:
(artist_picker uses jQuery autocomplete to fetch a list of matching artists. We want to display the artist name in the input, but need to submit the artist_id to the DB, hence updating the hidden field)
echo $this->Form->hidden('artist_id', array('div' => false));
echo $this->Form->input('artist_picker', array(
'label'=> false,
'div'=> (isset($this->validationErrors['Listing']['artist_id']) ? 'span4 error' : 'span4'), // Turn on error class if errors
'class' => (isset($this->validationErrors['Listing']['artist_id']) ? 'span12 form-error' : 'span12'), // Turn on form-error class if errors
'after' => (isset($this->validationErrors['Listing']['artist_id']) ? '<div class="error-message">'.$this->validationErrors['Listing']['artist_id'][0].'</div>' : ''),
'type'=>'text') // Show error message if errors
);
I'm building a multi-step form in Drupal 6. For some reason, the id attribute of the form element have an extra "-1" the first time the step 1 form is displayed.
For example, if the form name is "user-registration", the first time I access the step 1 form, the id is "user-registration-1". Then, if I go to step 2, the id is "user-registration". If I go back to step 1, the id remains "user-registration".
I'd like to know if there's a way for me to set the id attribute or to prevent Drupal from adding the extra "-1".
Thanks.
You can set the id yourself.
$form['#attributes'] = array('id' => 'user-registration');
Drupal 6.x has a form API property for both '#id' an '#attribute'. I had the same problem and found that the '#id' property was blank, which accounted for the blank 'id' in the form field. Then I used '#attribute' => array('id' => 'name of id'), which gave me a second 'id' in the form field. Remove the id in the '#attribute' and add another form API property for '#id'.
$form['foo'] = array(
'#type' => 'textfield',
'#title' => t('Foo'),
'#required' => FALSE,
'#id' => 'text-foo',
);
This worked for me:
$form = array( '#id' => 'myformid' );