Closed 7system7 closed 4 years ago
I created the global validation.yaml config/validator/validation.yaml
App\Application\Sonata\UserBundle\Entity\User:
constraints:
- App\Application\Sonata\UserBundle\Validator\Constraints\ChecksPartnerEmail: ~
properties:
username:
- NotBlank: { groups: [PartnerValidation] }
- Length: { min: 5, max: 10, groups: [PartnerValidation] }
I created the (class) Constraint \App\Application\Sonata\UserBundle\Validator\Constraints\ChecksPartnerEmail
namespace App\Application\Sonata\UserBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
/**
* @Annotation
*/
class ChecksPartnerEmail extends Constraint
{
public $message = 'The string "{{ string }}" contains an illegal character: it can only contain letters or numbers.';
public function getTargets()
{
return self::CLASS_CONSTRAINT;
}
public function validatedBy()
{
return get_class($this).'Validator';
}
}
And I created the Validator class \App\Application\Sonata\UserBundle\Validator\Constraints\ChecksPartnerEmailValidator
namespace App\Application\Sonata\UserBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class ChecksPartnerEmailValidator extends ConstraintValidator
{
public function validate($protocol, Constraint $constraint)
{
dump(
'bazmeg'
);
die();
...
}
}
And "just in case" I added the Constraint to the User entity as an Annotation also \App\Application\Sonata\UserBundle\Entity\User
use App\Application\Sonata\UserBundle\Validator\Constraints as BackendAssert;
...
/**
* This file has been generated by the SonataEasyExtendsBundle.
*
* @link https://sonata-project.org/easy-extends
*
* References:
* @link http://www.doctrine-project.org/projects/orm/2.0/docs/reference/working-with-objects/en
*
* @BackendAssert\ChecksPartnerEmail
*/
class User extends BaseUser
The Symfony's Basic Constraints are working. The custom Constraint (ChecksPartnerEmail) is not.
Nevemind. I solved it w/ Expression.
same issue here
same issue here
This is not how Open Source work... Maybe you could investigate a little bit and provide a PR with a fix.
Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
Nevemind. I solved it w/ Expression.
Hello. facing the same issue, we would have loved to know how...
Please @7system7 could you explain what you did, roughly at least?
Regards
@smilearric
This is a symfony way to solve the problem. You have 2 good options. Constraints or expressions. I am not sure, you have to do every step, I just copy-paste my solution to here.
config/packages/framework.yaml
framework:
validation: { enabled: true }
config/validator/validation.yaml
App\Entity\Deal:
constraints:
- App\Validator\Constraints\DealStageConstraint: ~
App\Entity\DealActivity:
properties:
activityType:
- Expression: { groups: [DealActivityValidation], expression: "this.activityTypeValidation()", message: <your-message-for-validation-error>}
src/Admin/DealActivityAdmin.php
class DealActivityAdmin
{
...
/**
* {@inheritdoc}
*/
// ----> IMPORTANT! Overridden method from Sonata\AdminBundle\Admin\AbstractAdmin
public function getFormBuilder()
{
$this->formOptions['data_class'] = $this->getClass();
$options = $this->formOptions;
// ----> IMPORTANT! That is the point
$options['validation_groups'] = array('Default', 'DealActivityValidation');
$options['attr'] = array('novalidate' => 'novalidate');
$formBuilder = $this->getFormContractor()->getFormBuilder($this->getUniqid(), $options);
$this->defineFormBuilder($formBuilder);
return $formBuilder;
}
...
}
src/Entity/DealActivity.php
class DealActivity
{
...
/**
* Validation for activityType
*
* @return bool
*/
public function activityTypeValidation(): bool
{
return <your-validation-conditions>;
}
...
{
src/Validator/Constraints/DealStageConstraint.php
class DealStageConstraint extends Constraint
{
...
public string $message = '<your-validation-message>';
/**
* {@inheritDoc}
*/
public function getTargets()
{
return self::CLASS_CONSTRAINT;
}
...
}
src/Validator/DealStageConstraintValidator.php
final class DealStageConstraintValidator extends ConstraintValidator
{
...
public function validate($protocol, Constraint $constraint): void
{
...
// example for a validation message to Deal/dealStage property (input)
$this->context
->buildViolation($constraint->message)
->atPath('dealStage')
->addViolation();
...
}
...
}
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Just met same problem and get solved after a try.
Then the validate function will works properly.
Hope this would help someone in the future :)
Any way:
I add groups={"Profile"}
into my annotation for my entity and validate function will works.
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
.....
@UniqueEntity(fields={"terminalPassword"}, ignoreNull=true, groups={"Profile"})
I would like to create a custom validation method in my UserAdmin. It checks a field (property) that depends on another field. The UserAdmin was created w/ "easy-extends" command:
Every validation method in the administration area are working properly, except the generated ones, include the SonataUserBundle It is completely ignore the validation method in Admin class... I tried everything; Documentation, I was read the code deeeeeeeeply, Google, DuckDuckGo, Github issues, stackoverflow, cache-invalidation, avada-kedavra.... And sadly, (I think) the documentation is very poor.
I think it is a bug but if I miss something thank you for your co-operation in advance. (And sry for my English.)
Environment
Linux AMBER 4.18.0-13-generic #14-Ubuntu SMP Wed Dec 5 09:04:24 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
Sonata packages
Symfony packages
PHP version
Subject
Below, I will show my all methods I tried.
Steps to reproduce
So, I override the validate method in UserAdmin \App\Application\Sonata\UserBundle\Admin\Model\UserAdmin::validate
Method 1
It works alone w/ all Admin that I created by hand. (for example: DealAdmin) But I was read some similar issues. Everybody says I must create a validation group. So I created it in \App\Application\Sonata\UserBundle\Admin\Model\UserAdmin::getFormBuilder
AND I was override the $formOptions property
I was override the User:username property and I was add an assertion. \App\Application\Sonata\UserBundle\Entity\User::$username
Method 2
I created a service: config/services.yaml
I was add the InlineConstraint class constraint to my bundle’s validation configuration: src/Application/Sonata/UserBundle/Resources/config/validation.yaml I tried w/ all of these src/Application/Sonata/UserBundle/Resources/config/validation.yml src/Application/Sonata/UserBundle/Resources/config/validation.xml (and the xml code - naturally - does not look like that below)
I was create the service where the validation method defined: \App\Application\Sonata\UserBundle\Entity\Block\PartnerBlockService
Expected results
It runs into validate or validateBlock method and dies.
Actual results
Absolutely nothing. There is no any error message or exception.