Get value from form choice Symfony 1.4 - php

how I can get value from form choice widget of submitted form?
Now it returns only integer index of selected option, but I need a value.
Below is my code:
$this->setWidget('emails',new sfWidgetFormChoice([
'label' => __('Emails'),
'expanded' => true,
'multiple' => true,
'choices' => ['email#gmail.com', 'email2#gmail.com'],
]));
$this->setValidators([
'emails' => new sfValidatorChoice(
['choices' => array_keys($this->getDefault('emails')), 'multiple' => true],
['required' => __('Required')]),

The choice widget can take a choices parameter with an associative array to define both the value and the displayed text of each option.
'choices' => array('email#gmail.com' => 'email#gmail.com', 'email2#gmail.com' => 'email2#gmail.com'),
This will submit the email as the value, however now you have potential security issues. Make sure you are enabling CSRF protection and properly validating the input at a minimum.

Related

Collection type: First input is being ignored

I am having troubles with the collection type. I can't make it save the first input.
here is my form:
->add('urls', CollectionType::class, [
'entry_type' => UrlType::class,
'allow_add' => true,
'allow_delete' => true,
'error_bubbling' => false,
'label' => 'my label',
'label_attr' => ['class' => '...'],
'required' => false
])
I slightly adjusted my collection_widget and url_widget. My main concern is that the fist input in the collection has id="form_urls" name="form[urls]" as its attributes. And the following ones (I made them appear by clicking on the button +) has right attributes id="form_urls_1" name="form[urls][1]", id="form_urls_2" name="form[urls][2]".
All that results in inability to save the first input (it just ignores it). How can I solve the problem?

Zendframework 2 Checkox

im pretty new to ZF2 and i have a pretty strange problem. I´ve created a simple checkbox using arrays,
and set checked value to good and unchecked value to bad. But when i submit my form, the URL shows that when the checkbox is checked, it sends .....checkbox=bad&checkbox=good... I don´t know why.
class SearchForm extends Form {
public function __construct($name = null){
parent:: __construct('Search');
$this->setAttribute('method', 'get');
$this->setAttribute ( 'enctype', 'multipart/formdata' );
$this->add(array(
'type' => 'Zend\Form\Element\Checkbox',
'name' => 'checkbox',
'options' => array(
'label' => 'A checkbox',
'checked_value' => 'good',
'unchecked_value' => 'bad',
),
));
Because by default Zend\Form\Element\Checkbox has use_hidden_element is true.
If set to true (which is default), the view helper will generate a
hidden element that contains the unchecked value. Therefore, when
using custom unchecked value, this option have to be set to true.
You use GET method. Of couse, you see two values in query string: for checkbox and for hidden elements.
See more carefully ZF2#Checkbox.
Not 100% sure this is the answer to the issue and not in a position to test right now but it is likely to do with the hidden element that the checkbox uses try:
$form->add(array(
'type' => 'Zend\Form\Element\Checkbox',
'name' => 'checkbox',
'options' => array(
'label' => 'A checkbox',
'use_hidden_element' => false,
'checked_value' => 'good',
'unchecked_value' => 'bad'
),
));

set checked attribute for simple checkbox field

I have the following code, which I try to pre-select the 1st and 2nd option of the checkbox when rendered. However this doesn't work. Can anyone help me why?
$builder->add('preferredContactTypes', 'entity', array('class' => 'AppMainBundle:ContactType', 'property' => 'type', 'multiple' => true, 'expanded' => true, 'mapped' => false, 'data' => array(1,2)))
the entity data field expects objects to be passed in not integers,
see this line:
'data'=>$em->getReference("TestGeneralBundle:Sucursal",3)
from this question:
Symfony2 Setting a default choice field selection

Symfony2 entity form field required multichoice

i have form in Symfony2. One of field is entity type:
->add('kind', 'entity', array(
'class' => 'TestBundle:Kind',
'expanded' => true,
'multiple' => true,
'required' => true
)
)
I want extort thta user choose one of kind. But form allowed if user don't check anything. How can I fix this ?
Add constraints to this field in your entity. The 'required' => true is just a client side validation, if you have an old browser, your form will always submit, because HTML5 validations only works on browsers that support HTML5
The entity field works exactly in the same way than the "Select tag, Checkboxes or Radio Buttons".
This means you can use the same $options than the other type fields.
If you want to select just one kind from user with radio buttons use:
->add('kind', 'entity', array(
'class' => 'TestBundle:Kind',
'expanded' => true,
'multiple' => false
)
)
Or if you want to select just one kind from user with a select tag use:
->add('kind', 'entity', array(
'class' => 'TestBundle:Kind',
'expanded' => false,
'multiple' => false,
'required' => true
)
)

How to reload a form from a drop down

I would like to reload the form, or preferably just the values, by using 'onChange'. There are several elements in the form that needs to be updated when changing the project id.
$this->addElement('select', 'project_id', array(
'label' => t('Project'),
'multiOptions' => $data,
'filters' => array('Int'),
'value' => 0,
'required' => true,
'disableTranslator' => true,
'validators' => array(),
'onChange' => 'Javascript:window.location.reload(false)'
));
You can change your onChange to call a custom javascript function instead - which will change any fields you need to change, and then submmit the form.

Categories