Closed mikemix closed 3 years ago
Why not using Annotation and a validation group ?
Do you have a repository to reproduce the bug ? Did you tried to debug the validation ? Do you know where the problem is coming from ?
Why not using Annotation and a validation group ?
Because I'm not using annotations at all.
Do you have a repository to reproduce the bug ?
Not public so no.
Did you tried to debug the validation ? Do you know where the problem is coming from ?
Tried, but failed, there's so many things going under the hood :)
Why not using Annotation and a validation group ?
Because I'm not using annotations at all.
I think you still can add constraints like you would have done in a FormType:
->add('email', TextType::class, [
'constraints' => new UniqueEntity(),
])
The validate method will/should be deprecated in the next major.
Did you tried to debug the validation ? Do you know where the problem is coming from ?
Tried, but failed, there's so many things going under the hood :)
Yeah, that's not gonna be easy.
I think you still can add constraints like you would have done in a FormType:
No. UniqueEntity
is a class constraint.
This should work fine, this is a bug.
I think you still can add constraints like you would have done in a FormType:
No.
UniqueEntity
is a class constraint.
Then I recommend you to add the constraint directly on the class, instead of relying on the admin.
The validate
method will be removed in the next major of Sonata.
This should work fine, this is a bug.
I never say it wasn't. But if you look at the interface:
/**
* NEXT_MAJOR: remove this method.
*
* @param object $object
*
* @deprecated this feature cannot be stable, use a custom validator,
* the feature will be removed with Symfony 2.2
*/
public function validate(ErrorElement $errorElement, $object);
Then I recommend you to add the constraint directly on the class, instead of relying on the admin.
This is why I'm relying on the admin in the first place – separation of concerns.
This is why I'm relying on the admin in the first place – separation of concerns.
I'm not sure I would call this a separation of concern. Your validation is database-related. And you're making this database-related validation in the configuration of the admin, which is just a configuration to display a FormType.
If you have another form on some page, since the validation is database-related, you'll want to have this UniqueEntity validation too.
Where do you describe your database schema ? In your entity ? Then a database-related constraint should be there. There is multiple way to add this validation: https://symfony.com/doc/current/validation.html#constraint-configuration
Anyway that's not the question. If you interested by fixing this bug, you're welcome. I was just warning you that since we're trying to release the next major release relatively soon, I'm not sure we'll spend time to fix a bug on a deprecated feature which does have others solutions.
Couldn't \Sonata\AdminBundle\Admin\AbstractAdmin::$formOptions
be used in this case as well?
Couldn't
\Sonata\AdminBundle\Admin\AbstractAdmin::$formOptions
be used in this case as well?
Since the form is created this way
$formBuilder = $this->getFormContractor()->getFormBuilder(
$this->getUniqid(),
$this->formOptions
);
You should be able to use $formOptions = [ 'constraints' => ... ]
, indeed.
Will try this, thanks.
Thanks, this works:
public function configure(): void
{
$this->formOptions['constraints'] = [
new UniqueEntity([
'fields' => ['country', 'taxNumber'],
]),
new UniqueEntity([
'fields' => ['slug'],
]),
];
}
This not work for me
sonata admin version: 3.55.0
Thanks, this works:
public function configure(): void { $this->formOptions['constraints'] = [ new UniqueEntity([ 'fields' => ['country', 'taxNumber'], ]), new UniqueEntity([ 'fields' => ['slug'], ]), ]; }
So in the meantime, $this->formOptions
is deprecated and you should now use:
protected function configureFormOptions(array &$formOptions)
{
parent::configureFormOptions($formOptions);
$formOptions['constraints'] = [
new UniqueEntity([
'fields' => [ ..... ],
]),
];
}
So in the meantime,
$this->formOptions
is deprecated and you should now use:protected function configureFormOptions(array &$formOptions) { parent::configureFormOptions($formOptions); $formOptions['constraints'] = [ new UniqueEntity([ 'fields' => [ ..... ], ]), ]; }
Very grateful for your reply! Thank you so much
Environment
Docker
php:7.3-apache
Sonata packages
Symfony packages
PHP version
Subject
Steps to reproduce
Expected results
Slug is validated to be unique, then email is validated to be unique.
Actual results
Slug is validated correctly, email validation is skipped which results in
Failed to update object
exception, because of theSQLSTATE[23000]: Integrity constraint violation
.If I replace the order of constraints, email is validated correctly, but slug's validation is skipped.