choice_translation_domain not working in symfony 2.7 - forms

I just upgraded my project to Symfony 2.7. One of the features why I wanted to upgrade as soon as possible, was the choice_translation_domain option for form fields.
But somehow there are still more then 3000 warnings for missing translations.
Here is how I added one field where the translation is already handled by doctrine:
$builder->add('product', 'entity', array(
'class' => 'MyProject:Product',
'required' => false,
'multiple' => false,
'empty_value' => '',
'choice_translation_domain' => false,
'label' => 'label.product',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('product')
->orderBy('product.title', 'ASC');
},
));
In translation debugging, I still see, that Symfony tries to translate the already translated product titles.

My problem was that we overwrite the default form_div_layout from Symfony.
So I had to change our custom form layout .twig file in order to make the option choice_translation_domain work.

Related

Propel form type model w. group_by is rendered without property display

Env: Symfony2 2.7 / Propel 1.6
I've created a choice form type like that:
$builder->add('mychoice', 'model', array(
'class' => 'Foo\\Bar',
'query' => FooBarQuery::create()->filterBySomething(true),
'group_by' => 'example',
'property' => 'title',
'multiple' => false,
'expanded' => false,
));
The rendering choice list is ok with good optgroup select options but the title's property is not displayed - id's property instead. If I remove the group_by option, the title property is well displayed.
What's wrong?
Would this work?
$builder->add(
'mychoice',
'entity',
array(
'class' => 'Foo\\Bar',
'choice_label' => 'title',
'multiple' => false,
'expanded' => false,
)
);
Set the type to entity and add a choice_label property and the property you want to be displayed.

How can I Validate All Children of a Form Collection?

I have spent a long time without making progress on what seems to be a simple problem. I need to allow the user to add up to three references to a form. Each reference will have a name and phone number (two text inputs). I would like to do some simple validation on each one. I have created my ReferenceType and nested it into the main form with the following (note, I created the PhoneNumber validator and added the Null() just for testing):
$builder->add('references', 'collection', array(
'type' => new ReferenceType(),
'by_reference' => false,
'allow_add' => true,
'label' => false,
'required' => false,
'attr' => array('class' => 'reference'),
'options' => array(
'required' => false,
'label' => false,
),
'cascade_validation' => true,
'constraints' => array(
new Null(),
new PhoneNumber(),
)
));
ReferenceType:
$builder->add('name', 'text', array(
'label' => 'Reference',
'required' => false,
'cascade_validation' => true,
'attr' => array(
'placeholder' => 'Name'
)
));
$builder->add('phone', 'text', array(
'label' => false,
'required' => false,
'attr' => array(
'placeholder' => 'Phone Number'
),
'cascade_validation' => true,
'constraints' => array(
new Length(array('max' => 14)),
new PhoneNumber(),
)
));
All of that is quite simple and straight forward. However, I have been unable to get the form to throw an error for any reason related to those references. I use Propel for my ORM and have a lot of validation which works correctly on the main form. These references also display and function correctly except for validation. They are all mapped to one column (references) and I have tried Propel's type="array" as well as defining my own getter and setter which serializes the array of items. The type=array doesn't work at all with the collection, and serializing it seems to work ok.
I have searched SO and symfony.com docs for an answer but have found nothing that would actually cause either name or phone to throw an error. I have tried adding validation to my validation.yml file without success (3 chars only for testing):
references:
- Valid: ~
- Collection:
fields:
name:
- Length:
max: 3
maxMessage: Please limit your reference's name to 3 characters or less.
I'm probably missing something very obvious here, but I can't seem to grasp what. I have also dug through the Profiler and everything appears correct in there as well.
Could any provide hints on things to look for? It seems that creating a new database just for references is a little overkill (they don't need to be searchable or anything). Propel has some information on collections, but it didn't seem to make a difference. Or am I entirely missing the idea here and perhaps I should implement the fields an entirely different way?
Symfony 2.6
Propel 2

zend addElements form onchange select

I try to add dinamically a form input Elemnt on onchange of select element.
I have this code:
$this->addElement('select', 'nationality', array(
'multiOptions' => array('CH' => 'Choose','IT' => 'Italy', 'other' => 'Other'),
'required' => true,
'label' => 'Nation',
'filters' => array('StringTrim'),
'onChange' => '???'
));
I try to paste my new element instead "???"
$this->addElement('select', 'nationality', array(
'multiOptions' => array('CH' => 'Choose','IT' => 'Italy', 'other' => 'Other'),
'required' => true,
'label' => 'Nation',
'filters' => array('StringTrim'),
'onChange' => '$this->addElement(
'text', 'codice_fiscale', array(
'required' => true,
'label' => 'Codice fiscale',
'required' => true
))'
));
I know is not correct, but I dont' found any documentation about it.
How can I add an element onchange select value?
Thanks in advance
I would approach the problem in a different way, if it's possible for you:
javascript functions to call on the onChange. It would be syntax correct (and, as you said, what you wrote is not correct, it's going to be printed out entirely on the onChange attribute, not interpreted through php).
so, i would write
'onChange' => 'addElement()'
and then declare the javascript function which handles new elements. Or, you can prepare the elements before (if you have a fixed number), and show / hide them. It may be convenient if you don't have much elements, but if they're too many, just add through javascript the needed one.
PseudoCode Javascript
function addElement()
{
// add your element here, or show / hide it
}
As far as I saw from my experience and similar cases
(Zend_form_element_select onchange in zend framework,
Zend Form: onchange select load another view content)
you won't escape from Javascript if you want to handle the "onChange"

Symfony - Combine two properties in a single entity form field

I have an entity for publications with many different fields as booktitle, conference etc. I want to build a search form and one of the feature requests is to combine two search parameters in a single choice field. So far I have something like this in the form builder:
$builder->add('booktitle', 'entity', array(
'required' => false,
'label' => 'Conference/Booktitle',
'property' => 'booktitle',
'class' => 'indPubBundle:Publication',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('p')
->groupBy('p.booktitle')
->orderBy('p.booktitle', 'ASC');
}
));
Basically I am displaying all booktitles as a choice field. What I want now is to have the conferences as well in the same choice field. Is there a way to achieve that?
The Entity field type is a child of the Choice field type. So you can provide Data via the "choices" parameter. Combine that with a (e.g. repository) method that returns an array with the data you need might be a solution that works for you.
$builder->add('booktitle', 'entity', array(
'required' => false,
'label' => 'Conference/Booktitle',
'class' => 'indPubBundle:Publication',
'choices' => $this->getDoctrine()->getRepository('indPubBundle:Publication')->getData(),
));

Disable some checkboxes in form

I was wondering if there is an easy way of how to disable one checkbox from modifying it by user (Symfony 2.1). I was trying something like this:
$builder->add('adminRoles', 'entity', array(
'property' => 'roleName',
'class' => 'MyBundle:Role',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('r')
->orderBy('r.roleName', 'ASC');
},
'disabled' => $this->disabledRoles,
'expanded' => true,
'multiple' => true
));
By $this->disabledRoles I meant an array of IDs of Role entities or entities themselves, but it seems that it just accepts boolean value which is applied for all entities (checkboxes). Thanks for your advice :-)
You will need to add a form listener in order to customize individual elements.
http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html
It might seem like a lot of work but it's easy enough once you work through the examples. You will end up passing disabledRoles to the listener and then setting the disabled flag accordingly.