Unit tests: manipulate dropdown options to pass form validation - forms

I have a form with a mandatory dropdown, but initially is empty. I populate it using jquery (based by an autocomplete field results, I add the option on PRE_SUBMIT event) and is working fine.
The problem is that I can't submit the form in units tests, the list of possible values for my dropdown is empty and I got this error:
InvalidArgumentException : Input "myform[parent_id]" cannot take "10"
as a value (possible values: ).
This is my code in unit tests:
$form = $crawler->filter('#myForm')->form();
$params = array(
'myform[title]' => 'sample title',
'myform[parent_id]' => '10',
'myform[date]' => $date->format('Y-m-d')
);
$form->setValues($params);
$client->submit($form);
There is any way to manipulate / populate this dropdown to pass the validation?
Thanks!

Related

How do I show a field based on the selection of another field

I have a form field, when I select a car, I want to get the type of car or the result of the model.
$this->crud->addField([
'name' => 'car_id',
'label' => 'Car',
'type' => 'select2_from_ajax',
'attribute' => 'name',
'model' => Car::class,
'data_source' => url('admin/api/cars'),
'placeholder' => 'Search and select a car',
'minimum_input_length' => 2,
]);
And how do I use the result to determine which other fields to show in the form?
You can do that in Backpack v5 using the included CrudField JS Library. To hide/show other fields depending on card_id, you should:
Step 1. In your setupCreateOperation() and/or setupUpdateOperation() load a new JS file:
Widget::add()->type('script')->content('assets/js/admin/forms/product.js');
Step 2. Create that JS file. Inside it, you can now easily select and get the value of Backpack fields. To show/hide other fields depending on car_id:
crud.field('car_id').onChange(function(field) {
// eg. show "car_model" if "car_id" is 1
crud.field('car_model').show(field.value == 1);
}).change();
For more things you can do with the CrudField JS Library, check out its docs.

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.

Don't post form input fields which are hidden?

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.

Arbitrary Form Processing with Drupal

I am writing a module for my organization to cache XML feeds to static files to an arbitrary place on our webserver. I am new at Drupal development, and would like to know if I am approaching this the right way.
Basically I:
Expose a url via the menu hook, where a user can enter in a an output directory on the webserver and press the "dump" button and then have PHP go to drupal and get the feed xml. I don't need help with that functionality, because I actually have a prototype working in Python (outside of Drupal)..
Provide a callback for the form where I can do my logic, using the form parameters.
Here's the menu hook:
function ncbi_cache_files_menu() {
$items = array();
$items['admin/content/ncbi_cache_files'] = array(
'title' => 'NCBI Cache File Module',
'description' => 'Cache Guide static content to files',
'page callback' => 'drupal_get_form',
'page arguments' => array( 'ncbi_cache_files_show_submit'),
'access arguments' => array( 'administer site configuration' ),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
I generate the form in:
function ncbi_cache_files_show_submit() {
$DEFAULT_OUT = 'http://myorg/foo';
$form[ 'ncbi_cache_files' ] = array(
'#type' => 'textfield',
'#title' => t('Output Directory'),
'#description' => t('Where you want the static files to be dumped.
This should be a directory that www has write access to, and
should be accessible from the foo server'),
'#default_value' => t( $DEFAULT_OUT ),
'#size' => strlen( $DEFAULT_OUT ) + 5,
);
$form['dump'] = array(
'#type' => 'submit',
'#value' => 'Dump',
'#submit' => array( 'ncbi_cache_files_dump'),
);
return system_settings_form( $form );
}
Then the functionality is in the callback:
function ncbi_cache_files_dump( $p, $q) {
//dpm( get_defined_vars() );
$outdir = $p['ncbi_cache_files']['#post']['ncbi_cache_files'];
drupal_set_message('outdir: ' . $outdir );
}
The question: Is this a decent way of processing an arbitrary form in Drupal? I not really need to listen for any drupal hooks, because I am basically just doing some URL and file processing.
What are those arguments that I'm getting in the callback ($q)? That's the form array I guess, with the post values? Is this the best way to get the form parameters to work on?
Thanks for any advice.
You can process forms in two stages, validate and submit.
Validate is for when you want to validate some user provided and raise form errors if some user input was invalid (like an invalid url or email address)
Submit which is the one you use is called if a form passes all of its validations, so at that point if you made a proper validation you will know that the data supplied by the user is okay.
Your submit function should look like this:
function ncbi_cache_files_dump(&$form, &$form_state) {
// $form: an array containing the form data
// $form_state: data about the form, like the data inputted in the form etc.
// code...
}
I think you need 2 separate forms here:
for setting the directory (the one you have now);
for making a dump (another form that would use the configured path).
Also it seems logical to publish the previously saved path as the default value in the settings form (instead of a hard-coded path).
And in general you should check the form input data from the second parameter of the submit callback:
function ncbi_cache_files_dump(&$form, &$form_state) {
$outdir = $form_state['values']['ncbi_cache_files'];
// ...
}