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
500 Error: The required option "class" is missing when using form with EntityType as parent #19517
Comments
@pascalwacker The problem comes from your type which has When you set |
@HeahDude I have the The point about the [...]
->add('logo')
[...] it is able to auto detect the necessary |
Where does it come from? I think your statement is wrong, isn't it? |
@pascalwacker The Otherwise you need to specify all the required options of the form type. |
I see, then it's my fault and not a bug! A little off topic, but since I already explained, what I try to achieve and what the related code is, is there a way to not pre load all possible entities for the form? With files (images in the most cases), I fear, that there will soon be a few thousand entries in the DB. The Javascript part of the frontend will render a modal, which has some paging to select one/multiple |
Closing as this is a user error after all. |
@pascalwacker For example, you can use form events to populate only the choices that match the user's choice. |
@xabbuh I've solved it in the meantime using this bundle: https://github.com/Alsatian67/FormBundle and the solution here: http://stackoverflow.com/q/38792355/2989952 |
Landing here from a search, back to the OP title:
How can we extend class EntityActiveType extends AbstractType
{
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'some_new_options' => 'some_value',
'class' => guessSomehow(),
));
}
public function getParent()
{
return EntityType::class;
}
} If there is a simple way to do it or any example somewhere I'm interested. |
@Glideh just don't define it (if it's variable), the guesser acts on runtime (if possible) and injects the entity class in your |
I'm trying to extend |
you cannot guess in defaults. Defaults are not related to a single usage of the type. They are reused between places using the type. |
My example was wrong indeed, it was just to give the idea
Yes I imagine the guesser is not used when you specify the type. Actually I already encountered this issue when trying to make a "HiddenEntityType" in the past |
Guessing the class when the form is building is only possible if the data is predefined (not null), using: $factory->create(EntityType::class, $entity); But when using as nested form, the initial data is always null until it's mapped (when the parent is initialized), to hack this the option $factory->create(MyParentEntityType::class, $parentEntity); // $parent must not be null
// in MyParentType.php
public function buildForm(FormBuilderInterface $builder, array $options)
{
$parentEntity = $builder->getData();
$builder->('entity', MyEntityType::class, [
'data' => $parentEntity->getEntity(),
]);
}
// in MyEntityType
public function getParent()
{
return EntityType::class;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefault('class', function (Options $options) {
return isset($options['data']) ? get_class($options['data']) : DefaultEntity::class,
});
} but this looks very fragile. In such cases, it would be more efficient to create a special type with a post set data listener that could add a child with a type depending on the data and the A type like this would act as a proxy and the child inheriting its parent data, everything should work fine. |
@wouterj I think, I found the same bug as mentioned in #17299 in Symfony 3.1.3
What I try to do, is to have a custom file uploader.
FormField:
My Entity contains:
And the view file looks like this
The Form:
When rendered with a
ChoiceType
as parent, it renders the form, but the controller says, that the form isn't valid. While when I change the parent toEntityType
, I get an "The required option "class" is missing." 500 error.When I add a
class
entry to the form, it works.As far, as I can tell, the problem is, that a normal
EntityType
field guesses the class, based on the annotations in the Entity, while a field, that has theEntityType
as parent can't do this.The text was updated successfully, but these errors were encountered: