Drupal - #states visible option not working with the date type - forms

I am trying to create a filtering form for a Drupal module that show hours. I am trying to get a date field to only show if the user wants to filter by it. I have a text field further down in the code that is hiding. I am not sure what I should do. Here is my code for the field:
$form['filters']['start-do'] = array(
'#type' => 'checkbox',
'#title' => t('Filter by start date'),
);
$form['filters']['start'] = array(
'#type' => 'date',
'#title' => t('Start Date'),
'#description' => t('Show hours that started after this date.'),
'#states' => array(
'invisible' => array(
':input[name="start-do"]' => array('checked' => FALSE)
)
)
);

You have forgotten about "container" type form element in your example.
Try something like this:
$form['filters']['start-do'] = array(
'#type' => 'checkbox',
'#title' => t('Filter by start date'),
);
$form['filters']['container'] = array(
'#type' => 'container',
'#states' => array(
'invisible' => array(
'input[name="start-do"]' => array('checked' => FALSE)
)
)
);
$form['filters']['container']['start'] = array(
'#type' => 'date',
'#title' => t('Start Date'),
'#description' => t('Show hours that started after this date.'),
);
It's always worth to check Forms API reference: http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html/7#states

Related

Prestashop Module : Add multi select dropdown

I'm working on a module and I would like to know how to add multiple dropdown with fields_options.
$this->fields_options = array(
'Test' => array(
'title' => $this->l('Test'),
'icon' => 'delivery',
'fields' => array(
'IXY_GALLERY_CREATION_OCCASION' => array(
'title' => $this->l('DropdownList'),
'type' => 'select',
'multiple' => true , // not working
'identifier' => 'value',
'list' => array(
1 => array('value' => 1, 'name' => $this->l('Test 1 ')),
2 => array('value' => 2, 'name' => $this->l('Test 2)'))
)
),
),
'description' =>'',
'submit' => array('title' => $this->l('Save'))
)
);
This is how I'm doin if you're meaning that :
$combo = $this->getAddFieldsValues();
$fields_form = array(
'form' => array(
'legend' => array(
'title' => $this->l('Title'),
'icon' => 'icon-cogs'
),
'input' => array(
array(
'type' => 'select',
'lang' => true,
'label' => $this->l('Nom'),
'name' => 'nom_matiere',
'options' => array(
'query' => $combo[0],
'id' => 'id_option',
'name' => 'name'
)
),
array(
'type' => 'select',
'lang' => true,
'label' => $this->l('Nom'),
'name' => 'name',
'options' => array(
'query' => $combo[1],
'id' => 'id_option',
'name' => 'name'
)
),
),
),
'submit' => array(
'title' => $this->l('Save'),
'name' => $this->l('updateData'),
)
),
);
the answer are not correctly .. due its not only dfined the field in the database, also must capture and stored in special way the values, in this example i demostrate to store as "1,2,3,6,8" using a single field
THE COMPLETE CODE AND ALL THE STEPS ARE AT: https://groups.google.com/forum/m/?hl=es#!topic/venenuxsarisari/z8vfPsvFFjk
here i put only the most important parts..
as mentioned int he previous link, added a new fiel in the model definition, class and the table sql
this method permits to stored in the db as "1,2,3" so you can use only a single field to relation that multiple selected values, a better could be using groupbox but its quite difficult, take a look to the AdminCustomers controller class in the controllers directory of the prestachop, this has a multiselect group that used a relational table event stored in single field
then in the helper form list array of inputs define a select as:
at the begining dont foget to added that line:
// aqui el truco de guardar el multiselect como una secuencia separada por comas, mejor es serializada pero bueh
$this->fields_value['id_employee[]'] = explode(',',$obj->id_employee);
this $obj are the representation of the loaded previous stored value when go to edit ... from that object, get the stored value of the field of your multiselect, stored as "1,3,4,6"
and the in the field form helper list of inputs define the select multiple as:
array(
'type' => 'select',
'label' => $this->l('Select and employee'),
'name' => 'id_employee_tech',
'required' => false,
'col' => '6',
'default_value' => (int)Tools::getValue('id_employee_tech'),
'options' => array(
'query' => Employee::getEmployees(true), // el true es que solo los que estan activos
'id' => 'id_employee',
'name' => 'firstname',
'default' => array(
'value' => '',
'label' => $this->l('ninguno')
)
)
),
an then override the post process too
public function postProcess()
{
if (Tools::isSubmit('submitTallerOrden'))
{
$_POST['id_employee'] = implode(',', Tools::getValue('id_employee'));
}
parent::postProcess();
}
this make stored in the db as "1,2,3"

How to put a picture instead of text in radio element in ZF2?

Subject: How to put a picture instead of text in radio element in ZF2?
code:
Options field:
array(
'spec' => array(
'type' => 'radio',
'name' => 'fcaptcha',
'options' => array(
'label' => 'Капча',
'label_attributes' => array(
'class' => 'control-label',
),
),
'attributes' => array(
'required' => 'required',
),
),
)
Controller:
$temp[0] = 'Here you need to put a picture instead of text';
$temp[1] = 'Here you need to put a picture instead of text';
$form->get('fcaptcha')->setValueOptions($temp);
You can do that just with some CSS. Here's a simple example that you can adapt for your purpose :
Let say you have this Zend\Form\Element\Radio element :
array(
'type' => 'Zend\Form\Element\Radio',
'name' => 'fcaptcha',
'options' => array(
'value_options' => array(
'0' => 'something1',
'1' => 'something2',
),
),
'attributes' => array(
'id'=>"fcaptcha-id"
)
)
CSS :
radio-option1 {background-image:url(option1.png);}
radio-option2 {background-image:url(option2.png);}
Some JS :
$(document).ready(function(){
$(':radio[value="0"]').addClass('radio-option1');
$(':radio[value="1"]').addClass('radio-option2');
});

language settings in Zend Framework 2 new DateSelect/MonthSelect Form

i have quick questions: i need to set the month dates on my form to English settings; they are curretnly set in Romanian Langugauge
i followed the example set by bakura10 / gist:3705417 and set my dates as such:
$this->add(array(
'type' => 'Zend\Form\Element\DateSelect',
'name' => 'birthDate',
'options' => array(
'label' => 'Date',
'create_empty_option' => true,
'day_attributes' => array(
'data-placeholder' => 'Day',
'style' => 'width: 20%',
),
'month_attributes' => array(
'data-placeholder' => 'Month',
'style' => 'width: 20%',
),
'year_attributes' => array(
'data-placeholder' => 'Year',
'style' => 'width: 20%',
)
)
));
i then set my view helper as such;
echo $this->formDateSelect($this->form->get('birthDate'), IntlDateFormatter::LONG, 'ro_RO');
the problem is that this obviously gives me romanian setting; i dont know where to get the settings for English; i tried this but it does not work;
echo $this->formDateSelect($this->form->get('birthDate'), IntlDateFormatter::LONG, 'en');

Drupal form API checkboxes hierarchy

I am trying to setup a configuration page consisting of hierarchical checkboxes like the following:
-Parent#1
--option#1
--option#2
-Parent#2
--option#1
--option#2
To achieve the above layout, I am using the following code:
$form['categories']['templates']['parent1'] = array(
'#type' => 'checkboxes',
'#title' => t('Select the appropriate actions for the form based on the indicated template'),
'#options' => array("Parent#1"),
'#multiple' => TRUE,
);
$form['categories']['templates']['parent1']['actions'] = array(
'#type' => 'checkboxes',
'#options' => array("P1 - option#1", "P1 - option#2"),
'#multiple' => TRUE,
);
$form['categories']['templates']['parent2'] = array(
'#type' => 'checkboxes',
'#title' => t('Select the appropriate actions for the form based on the indicated template'),
'#options' => array("Parent#2"),
'#multiple' => TRUE,
);
$form['categories']['templates']['parent2']['actions'] = array(
'#type' => 'checkboxes',
'#options' => array("P2 - option#1", "P2 - option#2"),
'#multiple' => TRUE,
);
But the effect I am getting to not as desired. I attached an image of what the code is generating:
You can use the new Form API feature, #states to achieve this.
Note that, for ease of use, I'm wrapping the conditional checkboxes with a fieldset.
<?php
$form['categories']['templates']['parent1'] = array(
'#type' => 'checkboxes',
'#title' => t('Select the appropriate actions for the form based on the indicated template'),
'#options' => array(1 => 'Parent#1'), // Note this! You can't use 0 as a key in #options.
'#multiple' => TRUE,
);
$form['categories']['templates']['parent1']['wrapper'] = array(
'#type' => 'fieldset',
'#title' => t('Options for #option', array('#option' => 'Parent#1')),
'#states' => array(
'visible' => array(
':input[name="parent1[1]"]' => array('checked' => TRUE), // We use "parent1" and its Nth option. in above field, See #options in above checkboxes field.
),
),
);
$form['categories']['templates']['parent1']['wrapper']['actions'] = array(
'#type' => 'checkboxes',
'#options' => array("P1 - option#1", "P1 - option#2"), // This is also wrong. You must define a key => value pair for #options or somehow avoid using 0 as the key.
'#multiple' => TRUE,
);
?>
You might want to use #tree => TRUE in the fieldsets to avoid Drupal from merging same keys' values together.
Also, you won't need #multiple in checkboxes field type.
Update
Example with node types:
<?php
$node_types = node_type_get_names();
$form['categories']['templates']['parent1'] = array(
'#type' => 'checkboxes',
'#title' => t('Select the appropriate actions for the form based on the indicated template'),
'#options' => $node_types,
);
foreach ($node_types as $type => $label) {
$form['categories']['templates']['node-options'.$type] = array(
'#type' => 'checkboxes',
'#title' => t('Options for #type', array('#type' => $label)),
'#options' => array("Parent#1"),
'#multiple' => TRUE,
'#states' => array(
'visible' => array(
':input[name="parent1['.$type.']"]' => array('checked' => TRUE), // We use "parent1" and its Nth option. in above field, See #options in above checkboxes field.
),
),
);
}

How to filter zf2 forms?

I'm trying to add filters for my form elements
$this->add(array(
'name' => 'name',
'attributes' => array(
'type' => 'text',
'required' => true,
),
'options' => array(
'label' => 'Name',
),
'filters' => array(
array('name' => 'StringTrim'),
array('name' => 'StripTags'),
)
));
but I still can add some tags in this element, what am I doing wrong?
Create two classes one for form another for filter and set filter for your form.
$form = new Form\CommentForm();
$form->setInputFilter(new Form\CommentFilter());
return $form;
for more info see https://github.com/nsenkevich/comment/tree/master/Comment