CakePHP FormHelper multiple select does not remember settings - forms

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.

Related

symfony2: selected value of choices fields

I get something strange with Symfony2 forms. I create a form with a propel entity, values are fine except the "select" (choices) field, that have no selected value.
I tried few tricks like:
$params['choices'] = array('N/A'=> 'N/A');
$params['data'] = array('N/A');
$params['preferred_choices'] = array('N/A');
Even with this, there is no preselected value. What's wrong ?
You can use data attribute for default selected item.
$param['data'] = 'N/A'
This is part of the Abstract "field" type ?
Fore example form,
$form = $this->createFormBuilder()
->add('category', 'choice', array(
'choices' => array(
0 => 'Books',
1 => 'Electronics',
2 => 'Hardware`
),
'data' => 1
))
->getForm();
In this example when the form loads the option Electronics should be selected as default
'empty_value' => 'Select Choice',
$builder->add('gender', 'choice', array(
'choices' => array('m' => 'Male', 'f' => 'Female')
'empty_value' => 'Select Choice',
));
I finally solved my problem. There were 2 things:
* reloading with Firefox, the previously selected value seems to be kept, so I coulnd't really test the changes until I closed the current tab and reopened one;
* I passed a single dimension array as values for the "choice" field, so Symfony2 re-indexed it with integer and my entity string value couldn't be hydrated to the symfony2 integer value of the field.

Symfony2 what is the best way to make this kind of form

i am making a shopping website and i got a probleme with some form. it's about the size part. I am gonna show some screenshots it's gonna be easier to understand :
what type of field is this kind of form ?
and then i would like the get this to show a list for client
i dont ask you to do the job for me but if you had some stuff (like a tutorial or some documentation about this kind of form) or some cluee i would really appriciate
Thx !
I'm not sure, but I think you are looking for Symfony's Form Types Reference.
In this case, you are probably talking about a choice field. A choice field can be rendered as a set of checkboxes / radio buttons (as in the first image), or as a <select> element (dropdown list), as in the second image, depending on wether the expanded option is set to true or false.
Radio buttons:
$builder->add('size', 'choice', array(
'choices' => array('s' => 'S', 'm' => 'M', 'l' => 'L', 'xl' => 'XL'),
'expanded' => true,
));
Dropdown list:
$builder->add('size', 'choice', array(
'choices' => array('s' => 'S', 'm' => 'M', 'l' => 'L', 'xl' => 'XL'),
'expanded' => false,
));
The default value of expanded is false, so if you don't specify it, the field will be rendered as a dropdown list.
If you have all available sizes stored in a table in your database, you might want to look at the entity field type as well. The entity type basically extends the choice type, but retrieves the available choices from the database:
$builder->add('size', 'choice', array(
'class' => 'MyWebshopBundle:Size',
'property' => 'name',
'expanded' => false,
));
This will fetch all Size entities from the database, and uses their name property in the dropdown list.
It's a choice type field
$form = $this->createFormBuilder($data)
->add('size', 'choice',
array('choices' => array(
1 => 'X',
2 => 'XL',
...
),
'data' =>'selectionnez la taille'
))
->getForm();
The option multiple can switch between radio button (false) and checkboxes ( true)
If you prefer a list like this you have to set the expanded attribute to false, true gives checkboxes or radios.
http://symfony.com/doc/current/reference/forms/types/choice.html

symfony2 form field label as an array

I'd like to have 3 separate texts for each field in my form as a label. They are separate, because they need to be styled differently. I tried this:
$builder->add('total_sales', 'text', array(
'label' => array('num' => '1', 'descr' => 'Total sales', 'category' => 'A'),
'required' => false,
'attr' => array(
'class' => 'field numeric_field',
'maxlength' => 10,
)));
Obviously the above don't work; it will display 'Array' in place of label.
How can I achieve desired effect?
first you'll need to create a custom form type that extends the text type, the reason for this is so you don't mess up other text types you might have elsewhere. After doing that you'll need to style it using a form_div_layout. you can see the details here:
http://symfony.com/doc/current/cookbook/form/form_customization.html

Drupal 7: Show fields on a form based on another field and a database lookup

I have a form I created in a custom module using the Form API. It is a fairly basic form with only 4 fields. It basically signs a user up for a job alert system. We are basing it only by email address with a few search parameters. We want people to be able to setup a search agent quickly and anonymously meaning they will NOT be creating a Drupal user account as we don't want them to have to deal with a password etc. They will just put in their email address, check off a few preferences and we will save the data.
Now the issue I need to deal with is allowing the user to edit their preferences later on and/or unsubscribe. Again this is not high security and it doesn't need to be. What I would like to do is initially ask ONLY for their email address in the form and allow them to submit it. I would then check the database to see if we already have an entry for that email address and if so, display the pre-filled form for them to edit or unsubsribe, other wise just show them the blank form. So I am just trying to figure out the best way to go about this. I'm thinking I just have one form with all of the fields including email address, but somehow only display the other fields besides the email address after a successful call to the database. I'm just tripping up on how to accomplish this.
EDIT:
I'm wondering if I can use Drupal's AJAX functionality to accomplish this? I tried this, but I couldn't get it to work. I put an Ajax attribute on my submit button with a wrapper ID and a callback function. I created a form element in my form with blank markup and used a prefix and suffix that created a wrapper div with the ID I used in my AJAX parameter. Then I am thinking in my callback function I can do the database lookup and then return the form elements I need either pre-filled or not into the wrapper div that was created, but when I do this, the form does submit via AJAX and I get the spinning wheel, but no matter what I return in my callback, it does not appear in my output wrapper div. Am I going about this the right way? I also made sure I have $form_state['rebuild'] = TRUE; on my original form.
Here is what I tried and it didn't work.
/**
* Implements hook_form().
*/
function _vista_form($form, &$form_state) {
$form = array();
$form_state['rebuild'] = TRUE;
$form['email'] = array(
'#type' => 'textfield',
'#title' => 'Email',
'#required' => TRUE,
);
$form['render_area'] = array(
'#type' => 'markup',
'#markup' => '',
'#prefix' => '<div id="job-agent-form">',
'#suffix' => '</div>',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
'#attributes' => array('class' => array('submit')),
'#ajax' => array(
'callback' => '_display_form',
'wrapper' => 'job-agent-form',
'method' => 'replace',
'effect' => 'fade',
),
return $form;
}
function _display_form($form, &$form_state) {
// there are other form elements that would go here also, I just added two for example
$type_options = array(
'VISTA-HealthCare-Partners-Government' => 'Vista Healthcare Partners',
'International' => 'International Locum Tenens',
'Permanent' => 'Permanent Physician',
'US-Locum-Tenens' => 'US Locum Tenens',
);
$form['job_type'] = array(
'#type' => 'checkboxes',
'#multiple' => TRUE,
'#title' => 'Type of Job',
'#options' => $type_options,
'#empty_option' => 'Choose a placement type',
'#empty_value' => 'all',
//'#default_value' => $type_selected,
);
$form['active'] = array(
'#type' => 'checkbox',
'#title' => 'Subscribe/Unsubscribe',
'#default_value' => 1,
);
return $form;
}
I would go for creating all fields in the form than use hook_form_alter() to hide the unneeded ones with ['#access'] = FALSE.

Zend: Form validation: value was not found in the haystack error

I have a form with 2 selects. Based on the value of the first select, it updates the values of the second select using AJAX. Doing this makes the form not being valid. So, I made the next change:
$form=$this->getAddTaskForm(); //the form
if(!$form->isValid($_POST)) {
$values=$form->getValues();
//get the options and put them in $options
$assignMilestone=$form->getElement('assignedMilestone');
$assignMilestone->addMultiOptions($options);
}
if($form->isValid($_POST)) {
//save in the database
}else {
//redisplay the form
}
Basically, I check if it is valid and it isn't if the user changed the value of the first select. I get the options that populated the second select and populate the form with them. Then I try to validate it again. However this doesn't work. Anybody can explain why? The same "value was not found in the haystack" is present.
You could try to deactivate the validator:
in your Form.php
$field = $this->createElement('select', 'fieldname');
$field->setLabel('Second SELECT');
$field->setRegisterInArrayValidator(false);
$this->addElement($field);
The third line will deactivate the validator and it should work.
You can also disable the InArray validator using 'disable_inarray_validator' => true:
For example:
$this->add( array(
'name' => 'progressStatus',
'type' => 'DoctrineModule\Form\Element\ObjectSelect',
'options' => array(
'disable_inarray_validator' => true,
),
));
Additionaly you should add you own InArray Validator in order to protect your db etc.
In Zend Framework 1 it looks like this:
$this->addElement('select', $name, array(
'required' => true,
'label' => 'Choose sth:',
'filters' => array('StringTrim', 'StripTags'),
'multiOptions' => $nestedArrayOptions,
'validators' => array(
array(
'InArray', true, array(
'haystack' => $flatArrayOptionsKeys,
'messages' => array(
Zend_Validate_InArray::NOT_IN_ARRAY => "Value not found"
)
)
)
)
));
Where $nestedArrayOptions is you multiOptions and $flatArrayOptionsKeys contains you all keys.
You may also add options to select element before checking for the form validation. This way you are insured the select value is in range.