Don't post form input fields which are hidden? - forms

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.

Related

How do I hide a drupal 8 form element on default

I am trying to do something that should be extremely easy in drupal. I just want to simply hide a form field in the drupal admin by default.
Making the field disabled simply doesn't work. I cannot find any documentation for this. It's unbelievable that it's this hard to do something so simple in Drupal.
$form['field_name']['#states'] = [
'visible' => [
':input[name="field_foo[0][target_id]"]' => ['value' => 'blah'],
],
'invisible' => true,
];
The visible part works. If another field has a certain value, then show the form element.
But I simply cannot get it to hide this field on default when you're adding a new node.
Add into form element array $element = ['#access' => FALSE,]:
$form['field_name'] = [
'#access' => FALSE,
]
Easy and does not affect your default value (if default is set):
$form['field_name']['#attributes']['class'][] = 'hidden';
Drupal core will "display: none;" this element from core styles using the 'hidden' css class.
You don't need to set invisible, it will be hidden by default unless the visible condition is met. Also the input looks to be wrong, as it should be whatever the name attribute is set to in the element you are basing the condition on.
The drupal.org documentation with an example.
e.g.
$form['field_name']['#states'] = [
'visible' => [
':input[name="field_conditional_on"]' => ['value' => 'value_conditional_on'],
],
];
Or if you just want to hide it using CSS's display property, you could use something like below,
$form['field_name']['#states'] = [
'#attributes' => array('style' => array('display: none;')),
];

How to submit the value of a disabled form field?

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()

display radio buttons using FormHelper

I am using CakePHP3
I am unable to decipher the documentation on using radio buttons to create for a list of results.
There is create radio button.
Which I tried this way:
foreach ($userGroups as $group_id => $group) {
echo $this->Form->radio("UserGroup.$group_id.id", ['value' => $group_id, 'label' => $group]);
}
There is using select pickers.
Which says all you need to do is set the type to radio. But I checked the api and it looks like it will unset the type.
Anyway, I tried
echo $this->Form->select("user_circle_id", $userGroups, ['type' => 'radio']);
Nothing works.
Please advise.
UPDATE:
$userGroups = [1 => 'Group 1', 2 => 'Group 2']; // basically primary key is keys and the display fields are the values.
Use following syntax :
$selectedStatus = 1;
$attributes = array('legend'=>false,'value'=>$selectedStatus,'class'=>'sortByStatusCourse','name'=>'sortByStatusCourse');
$options = array("1"=>"Active","0"=>"Inactive");
echo $this->Form->radio('is_active', $options,$attributes);
Code explanation :
We are passing parameters to cakePHP core so that it will output Radio button in HTML.
echo $this->Form->radio :
Here 1st param is Name on field in Database. 2nd param is What are the options for radio buttons , its value and text .3rd param is what are its attributes like what should be its default value here I explicitely set it to 1 so it will already tick radio with Activetext.
According to your code : echo $this->Form->radio("UserGroup.$group_id.id", ['value' => $group_id, 'label' => $group]);
I think order of param is wrong as you pass VALUE in 2nd param which should be 3rd param.Check my code.

$this->request->data EMPTY - When I have disabled Fields CakePHP 2

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.

Load a Symfony sfWidgetFormDoctrineChoice select with a limited set of results

I am developing a form in a Symfony application where a user must indicate a country, a region and an optional island using HTML select elements.
I have three models: Country, Region and Island; and Symfony has auto-generated three widgets in the form using the sfWidgetFormDoctrineChoice widget:
...
'country_id' => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Country'), 'add_empty' => false)),
'region_id' => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Region'), 'add_empty' => false)),
'island_id' => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Island'), 'add_empty' => true)),
...
Since the country list is large, and so the region list, I've been thinking on filtering the options available in Region and Island according to the value selected in Country.
Doing this after the HTML document is ready is easy with jQuery's change method and a simple AJAX request. But I wonder if there's a way of doing this directly from Symfony, perhaps in form configuration, to have a default combined selection.
Any suggestions?
Thanks!
After playing around with sfDependentSelectPlugin, I ended up assigning custom queries to initialize the HTML select elements:
$countryId = $this->getObject()->getCountry()->getTable()->getDefaultCountryId();
$regionId = $this->getObject()->getRegion()->getTable()->getDefaultRegionId($countryId);
$islandId = $this->getObject()->getIsland()->getTable()->getDefaultIslandId($regionId);
$this->widgetSchema->setDefault('country_id', $countryId);
$this->setWidget('region_id', new sfWidgetFormDoctrineChoice(array(
'model' => $this->getRelatedModelName('Region'),
'query' => $this->getObject()->getRegion()->getTable()->getRegionsQuery($countryId),
'default' => $regionId,
)));
$this->setWidget('island_id', new sfWidgetFormDoctrineChoice(array(
'model' => $this->getRelatedModelName('Island'),
'query' => $this->getObject()->getIsland()->getTable()->getIslandsQuery($regionId),
'add_empty' => '---',
'default' => $islandId,
)));
And then updating the options available with AJAX requests using jQuery. The good thing is that the actions that handle the AJAX requests use the same query methods above to return a new set of results.