Closed alfabetagama closed 4 years ago
Can you please explain a little bit more what you did?
I was migrating from fos user. I have Sonata admin for user model. When initializing user create form this error was thrown.
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->with('Credentials')
->add('email', EmailType::class, array('attr' => array("autocomplete" => "false")))
->add(...
As a workaround i overrode getEmail() method
/*
* workaround for nucleos
*/
public function getEmail(): string
{
if (isset($this->email)) {
return $this->email;
}
return '';
}
Sadly, this is a known problem I found when migrating the FOSUserBundle / SonataUserBundle to this projects: https://github.com/nucleos/NucleosUserAdminBundle/blob/main/src/Admin/Model/UserAdmin.php#L48-L62
I wanted to make the username and email required, because many other components rely on a valid data. There is currently no good solution for non-nullable entity fields in sonata admin and even symfony forms. You could use a data mapper on your admin class to solve this problem. You could also have a look at https://github.com/sensiolabs-de/rich-model-forms-bundle.
Environment
Symfony 4.4
PHP version
PHP 7.4.3
Steps to reproduce
Create sonata admin for user model. Try to create user.
Expected results
Empty form should be displayed
Stack trace
DomainException: Email cannot be null
at vendor/nucleos/user-bundle/src/Model/User.php:207 at Nucleos\UserBundle\Model\User->getEmail() (vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php:393) at Symfony\Component\PropertyAccess\PropertyAccessor->readProperty() (vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php:93) at Symfony\Component\PropertyAccess\PropertyAccessor->getValue() (vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php:97) at Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper->getPropertyValue() (vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php:51) at Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper->mapDataToForms() (vendor/symfony/symfony/src/Symfony/Component/Form/Form.php:378) at Symfony\Component\Form\Form->setData() (vendor/sonata-project/admin-bundle/src/Controller/CRUDController.php:573) at Sonata\AdminBundle\Controller\CRUDController->createAction() (vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php:158) at Symfony\Component\HttpKernel\HttpKernel->handleRaw() (vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php:80) at Symfony\Component\HttpKernel\HttpKernel->handle() (vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php:201) at Symfony\Component\HttpKernel\Kernel->handle()