Symfony 1.4 - How to set default values on sfWidgetFormChoice (checkboxes) - symfony-1.4

The following code creates a field with multiple checkboxes. How do I set the default values of these checkboxes?
new sfWidgetFormChoice(array(
"choices" => $choices,
"label" => "Label",
"multiple" => true,
"expanded" => true
));

You can set the defaults manually:
$countries = array(
'Spain',
'England',
'France'
);
$this->widgetSchema['countries'] = new sfWidgetFormChoice(array('choices' => $countries, 'multiple' => true, 'expanded' => true));
// Form will render with Spain and England pre-selected
$this->widgetSchema['countries']->setDefault(array(0,1));
If the object to which the form is related is not new, the defaults will pre-selected based on stored values, so you may need to add a $this->getObject()->isModified() check around the last line.

If you have a form which has a widget with select
$this->widgetSchema['example_select_export'] = new sfWidgetFormChoice(array(
'multiple' => false,
'expanded' => false,
'choices' => array("csv" => "CSV", "excel" => "Excel", "csv_mac" => "CSV mac")
);
$choices = array("csv" => "CSV", "excel" => "Excel", "csv_mac" => "CSV mac");
You can either set default in the action where you have created your form call this
$this->form->setDefault('example_select_export','csv')
Or if you like just order your choices in the order you want them to show

Related

Make Repeater beta's fields required for the first entry

So, I am working with the Repeater beta add-on for Gravity Forms (https://docs.gravityforms.com/repeater-fields/), and I need to make it required--or more specifically, the first entry within it's field. I have tried various methods to no avail and am not sure why.
I tried setting "required" to true for the fields inside the repeater
I tried setting the "required" to true for the repeater field itself
I tried having the repeater field AND the fields within it required
In the admin, every time I click the "Required" checkbox for the repeater field, the checkbox becomes unchecked after saving the form
Using $(window).load I tried adding a required=required attribute to all fields in the repeater but that prevents the only fields in the form from displaying any "required" messages to the user, and only the first empty field in the repeater is actually checked. The rest, even if empty, are not, and do no display a "required" message.
Using $(document).ready() but the results were similar to above but the rest of the form fields started validating again
Browsing the Gravity Forms forum but could not find the answer I was looking for, and I don't know how long it will take for anyone to respond to my question and this can't wait days or weeks.
This is all the code I setup for the repeater in functions.php per the field's documentation example:
// Adjust your form ID
add_filter( 'gform_form_post_get_meta_5', 'add_my_field' );
function add_my_field( $form ) {
// Create a Single Line text field for the product member's name
$purchasedFrom = GF_Fields::create( array(
'type' => 'text',
'id' => 1002, // The Field ID must be unique on the form
'formId' => $form['id'],
'required' => true,
'label' => 'Purchased From',
'pageNumber' => 1, // Ensure this is correct
) );
$itemtype = GF_Fields::create( array(
'type' => 'text',
'id' => 1007, // The Field ID must be unique on the form
'formId' => $form['id'],
'required' => true,
'label' => 'Item Type',
'pageNumber' => 1, // Ensure this is correct
) );
// Create an email field for the product s
$quantity = GF_Fields::create( array(
'type' => 'text',
'id' => 1001, // The Field ID must be unique on the form
'formId' => $form['id'],
'required' => true,
'label' => 'Quantity',
'pageNumber' => 1, // Ensure this is correct
) );
$purchasedDate = GF_Fields::create( array(
'type' => 'text',
'id' => 1003, // The Field ID must be unique on the form
'formId' => $form['id'],
'label' => 'Date of Purchase',
'required' => true,
'pageNumber' => 1, // Ensure this is correct
) );
$serviceDate = GF_Fields::create( array(
'type' => 'text',
'id' => 1004, // The Field ID must be unique on the form
'required' => true,
'formId' => $form['id'],
'label' => 'Date Out of Service',
'pageNumber' => 1, // Ensure this is correct
) );
$size = GF_Fields::create( array(
'type' => 'text',
'required' => true,
'id' => 1009, // The Field ID must be unique on the form
'formId' => $form['id'],
'label' =>'Size',
'pageNumber' => 1, // Ensure this is correct
) );
$upc = GF_Fields::create( array(
'type' => 'text',
'required' => true,
'id' => 1010, // The Field ID must be unique on the form
'formId' => $form['id'],
'label' => 'UPC/Part # (From Receipt)',
'pageNumber' => 1, // Ensure this is correct
) );
$damage = GF_Fields::create( array(
'type' => 'text',
'required' => true,
'id' => 1005, // The Field ID must be unique on the form
'formId' => $form['id'],
'label' => 'Description of Damage',
'pageNumber' => 1, // Ensure this is correct
) );
// Create a repeater for the product and add the name and email fields as the fields to display inside the repeater.
$product = GF_Fields::create( array(
'type' => 'repeater',
'required' => true,
'id' => 1000, // The Field ID must be unique on the form
'formId' => $form['id'],
'label' => 'Add Products',
'addButtonText' => 'Add Another Product',
'removeButtonText'=> 'Remove Product',
'pageNumber' => 1, // Ensure this is correct
'fields' => array( $purchasedFrom,$itemtype, $quantity, $purchasedDate,$serviceDate, $size, $upc, $damage), // Add the fields here.
) );
$form['fields'][] = $product;
return $form;
}
// Remove the field before the form is saved. Adjust your form ID
add_filter( 'gform_form_update_meta_5', 'remove_my_field', 10, 3 );
function remove_my_field( $form_meta, $form_id, $meta_name ) {
if ( $meta_name == 'display_meta' ) {
// Remove the Repeater field: ID 1000
$form_meta['fields'] = wp_list_filter( $form_meta['fields'], array( 'id' => 1000 ), 'NOT' );
}
return $form_meta;
}
I do need this field to be required somehow, specifically the first entry so that the user knows to enter at least one product. What am I missing?
Hi you simply need to flag them as:
'isRequired' => true,
rather than
'required' => true,
After the change, be sure to remove the repeater from the form and update. The repeater should appear again, click update and check the results.

Error for Constraint added to Field does not show for field

I dynamically generate a form and add constraints (i.e. Choice).
$builder->add('test', 'choice', [
'choices' => [1, 'one', 2 => 'two'],
'required' => true,
'expanded' => true,
'error_bubbling' => true,
'cascade_validation' => true,
'label' => 'this_is_a_test',
'multiple' => false,
'constraints' => [
new NotBlank([
'groups' => ['Default']
]),
new Choice([
'min' => 1,
'choices' => [1, 2],
'groups' => ['Default']
])
]
]);
When submitting the form with empty data, the error shows up for the form, not the element of the form where i added the constraint (checked in profiler as well).
There is not Option atPath for those constraints and i add them directly to the field, so i do not get why they show up for the form.
That's what the error_bubbling option does (which you set to true in your form type):
If true, any errors for this field will be passed to the parent field or form. For example, if set to true on a normal field, any errors for that field will be attached to the main form, not to the specific field.

Symfony2 - Drop down with the possibility of adding fields

Sorry for my english.
I need to create a drop down list (select option) with values ​​taken from an Entity.
I must also add options (I use select2 to do this).
When I send the form it doesn't work. How can I configure the field type to make it work?
I need the possibilities to adding multiple persons
My current code is
...
->add('person', 'entity', array(
'attr' => array(
'class' => 'tags'
),
'class' => 'AppBundle:Person',
'data_class' => null,
'label' => 'Persons',
'mapped' => false,
'multiple' => true,
'required' => false
));
Select2
$('select.tags').select2({
language: 'it',
tags: true,
tokenSeparators: [','],
width: '100%'
});
Error message
The offset "0" does not exist.

Do not show collection number

I have a form "Survey" with a collection.
->add('comments', 'collection', array(
'type' => new CommentType() ,
'allow_add' => false,
'allow_delete' => false,
'label' => false,
)
)
My Form "CommentType" have only one field for entering comments:
$builder->add('comment', 'text', array('label' => 'comment', 'translation_domain' => 'messages', 'attr' => array('maxlength' => 255)));
If i render my collection in twig template like this
{{ form_row(form.comments, {'attr': {}}) }}
Symfony/Twig always render a control label "control-label required" with a number for the rendered collection.
As example:
0 -> control-label required
Comment -> Label
[] -> Input field
1 -> control-label required
Comment -> Label
[] -> Input field
How can i disable this control label?
Update
Add options and label attributes for your collection, as follows
->add('comments', 'collection', array(
'type' => new CommentType() ,
'allow_add' => false,
'allow_delete' => false,
'options' => array(
'label' => false,
)
)
I think you disabled the wrong label , leave the label of the collection and disable the one in your ->add('comment','text'.....)

Symfony 2.6 checked by default checkboxes

How I can make checkboxes checked by default based on data from database?
Now my form looks like:
...
->add(
"role", "entity", [
"class" => "AppDefaultBundle:OptionRole",
"required" => false,
"label" => "Roles for user: ",
"property" => "name",
"expanded" => true,
"multiple" => true
]
)
...
And I want to select defaults for this checkboxes based on data from other table.
You should probably add the choices property: http://symfony.com/doc/current/reference/forms/types/choice.html#choices
In your case you should have an array with all OptionRoles relevant for the (User ?) entity you are working on (the one you create the form for).
Assuming the doctrine User model knows it's OptionRoles (most likely a ManyToMany association) the form should automatically check the checkboxes of the Users OptionRoles.
Here is one example:
[
'label' => 'Select Modules',
'class' => 'Foo\BarBundle\Entity\Module',
'choices' => $this->availableModules(),
'property' => 'name',
'multiple' => true,
'expanded' => true
]
...
public function availableModules()
{
return $this->get('doctrine')
->getManager()
->getRepository('Foo\BarBundle\Entity\Module')
->findAll();
}

Resources