-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Form with radio buttons fails after Symfony 2.7 upgrade #14877
Comments
Hi @XWB, thank you for reporting this issue! I'm not sure I understand your problem correctly. Could you please upload a fork of symfony-standard that reproduces your problem? |
Hi, |
@isabellebruchet Fully correct |
@XWB we don't need your private project. What we need is your problem reproduced on a fresh Standard Edition project. While preparing it you'll be able to roll out problems specific to your project or configuration, and might be you'll even find the cause of the problem. Surely, it'll be much easier for us to debug the issue. |
Since we've updated to
$defaultData = array();
if ($order->getSubject() instanceof Subject) {
$defaultData['subject'] = $order->getSubject();
}
$form = $this->createFormBuilder($defaultData)
->add('subject', 'entity', array(
'class' => 'AppBundle:Order\Subject',
'expanded' => true,
'property' => 'name',
))
->getForm()
; The exception can be solved in two ways, by setting // ...
$form = $this->createFormBuilder($defaultData)
->add('subject', 'entity', array(
'class' => 'AppBundle:Order\Subject',
'data_class' => 'AppBundle\Entity\Order\Subject',
'expanded' => true,
'property' => 'name',
))
->getForm()
; Looks like a BC break, think |
You can install the 2.7 branch of my symfony-standard fork: Once it's done, add manually some companies and create some employees. The exception will occur in the 'edit' view once you have linked an employee with a company. Hope it will help, thank you in advance for taking a look at it! |
@pascallapointe thanks for your work. I confirmed your fork reproduces the issue. |
I looked into this a little and found the $viewData set before the exception I have had success using this workaround to avoid adding data_class everywhere in my code: // src/Acme/DemoBundle/Form/Type
namespace Acme\DemoBundle\Form\Type;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
class AcmeEntityType extends EntityType
{
public function configureOptions(OptionsResolver $resolver)
{
parent::configureOptions($resolver);
if(Kernel::MAJOR_VERSION == '2' &&
Kernel::MINOR_VERSION == '7' &&
(int) Kernel::RELEASE_VERSION <= 2) {
$dataClassNormalizer = function (Options $options, $dataClass) {
if($dataClass || $options['multiple'] || !$options['expanded']) {
return $dataClass;
}
return $options['em']->getClassMetadata($options['class'])->getName();
};
$resolver->setNormalizer('data_class', $dataClassNormalizer);
}
}
} and add a service definition like: # src/Acme/DemoBundle/Resources/config/services.yml
form.type.entity:
class: Phoenix\CmsBundle\Form\Type\PheonixEntityType
tags: [{ name: form.type, alias: entity}]
arguments: [@doctrine] This workaround is a rather ugly hack and assumes this will be fixed in 2.7.3. |
Can confirm this issue, setting "expanded" to false removes the error, but is not a solution when you need expanded rendering. The workaround with data_class only helps for fields with multiple = false, otherwise you get the error: The form's view data is expected to be an instance of class Blabla, but is a(n) array. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms a(n) array to an instance of Blabla |
Here is a test for the original @XWB's issue: sustmi@57b297e .
as viewData. |
I can confirm, that you can workaround this issue by setting the |
Thanks for the test @sustmi Ping @webmozart |
but in 2.6, it was possible to have mixed choices (strings and objects), which is not possible anymore because of this data mapper usage. |
Any updates on this issue? @webmozart |
Also ran into this issue today after upgrading from 2.6 to 2.7. Any news? :) |
Same, any news? |
As far as I understand, the real issue here are that IMO, the possible good solutions are:
|
Fixed in #16679. |
👍 |
Will it solve setting an entity as default data when |
@rvanlaak yes, it should |
👍 |
…t to null (webmozart) This PR was merged into the 2.7 branch. Discussion ---------- [Form] Disabled view data validation if "data_class" is set to null | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #14877 | License | MIT | Doc PR | - After this PR, Form::setData() does not validate the view data anymore when "data_class" is set to `null`. This way it is possible to create fields with dynamic view data types (see #14877). Commits ------- f495410 [Form] Disabled view data validation if "data_class" is set to null
This issue is closed, but I don't know if it really should be because problem @adsc mentioned still exists. $form = $this->get('form.factory')->createNamedBuilder('', 'form')
->add('entities', 'entity', [
'class' => 'Namespace\SomeBundle\Entity\SomeEntity',
'choice_label' => 'name',
'label' => 'Label',
'multiple' => true,
'expanded' => true,
'read_only' => true,
'query_builder' => function (EntityRepository $repository) use ($ids) {
return $repository->createQueryBuilder('e')
->where('e.id IN(:ids)')
->setParameter(':ids', array_values($ids))
->orderBy('e.name', 'ASC');
}
])
->getForm(); When I try to create view from that form I got 2 exeptions in stack:
in try {
$valueMap = array_flip($this->choiceList->getValuesForChoices($choices));
} catch (\Exception $e) {
throw new TransformationFailedException(
'Can not read the choices from the choice list.',
$e->getCode(),
$e and
in */
public function isScheduledForInsert($entity)
{
return isset($this->entityInsertions[spl_object_hash($entity)]);
}
/** Of course when you'll try to set data_class you got
exactly like @adsc said. I didn't found workaround for that yet. This is really pain in the ass when you have widget where user can pick specific entities in form (with checkboxes, not multiple select). Any ideas @webmozart ? |
@adampastusiak Can you create a new issue if you think that we still have a bug here? |
Imagine this form:
After upgrading to Symfony 2.7, the form throws the following exception:
Setting the
data_class
seems to work but I wonder why this has not been documented in the upgrade file?The text was updated successfully, but these errors were encountered: