Custom validator not call #service_container - php

Symfony version: 2.5
Error
"Catchable Fatal Error: Argument 1 passed to Intermedius\UserBundle\Validator\Constraints\RegisteredEmailValidator::__construct() must implement interface Symfony\Component\DependencyInjection\ContainerInterface, none given, called in D:\Projektek\pricing_tool\backend\vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Validator\ConstraintValidatorFactory.php on line 71 and defined in D:\Projektek\pricing_tool\backend\src\Intermedius\UserBundle\Validator\Constraints\RegisteredEmailValidator.php line 22"
RegisteredEmail.php
class RegisteredEmail extends Constraint
{
public $message = "MSG";
}
RegistereEmailValidator.php
class RegisteredEmailValidator extends ConstraintValidator{
public $containerInterface;
function __construct(ContainerInterface $containerInterface)
{
$this->containerInterface = $containerInterface;
}
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof RegisteredEmail) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\RegisteredEmail');
}
}
public function validateBy()
{
return "registered_email";
}
}
services.yml
services:
intermedius.user.validator.registered_email:
class: Intermedius\UserBundle\Validator\Constraints\RegisteredEmail
arguments: [ #service_container ]
tags:
- { name: validator.constraint_validator, alias: registered_email }

The class of your validator service definition should be set to the validator class, not the constraint.
class: Intermedius\UserBundle\Validator\Constraints\RegisteredEmailValidator

You should not inject the service_container in your services. In your example you should rather inject only the services you need instead. See for example the "Avoiding your Code Becoming Dependent on the Container" section of http://symfony.com/doc/current/components/dependency_injection.html.

Related

Symfony validator with dependencies

I want to validate an object with constraint annotations, and use dependencies (entityManager) at the validator.
The validator does not work, if it has dependencies (eg. entityManager) in the constructor.
I followed the docs, but it does not work:
https://symfony.com/doc/current/validation/custom_constraint.html#constraint-validators-with-dependencies
"ClassNotFoundException
Attempted to load class "validator_question_exists" from the global namespace.
Did you forget a "use" statement?"
I try to validate the 'Question' object like this (maybe here is the problem):
$validator = Validation::createValidatorBuilder()
->enableAnnotationMapping()
->getValidator()
;
$question = new Question();
$errors = $validator->validate($question);
Question (the object to validate)
/** #App\Validator\Constraint\Question\QuestionExists() */
class QuestionReadInput{
....
}
services.yaml
services:
validator.unique.question_exists:
class: App\Validator\Constraint\Question\QuestionExistsValidator
tags:
- { name: validator.constraint_validator, alias: validator_question_exists}
Constraint
namespace App\Validator\Constraint\Question;
use Symfony\Component\Validator\Constraint;
/**
* #Annotation
*/
class QuestionExists extends Constraint
{
public $message;
public function getTargets()
{
return self::CLASS_CONSTRAINT;
}
public function validatedBy()
{
//if i delete this function, symfony cant autowire the entitymanager to the validator
//this throws an error, wants to make a new validator_question_exists(), which not exists, because its a service alias, the docs said it should be okay
return 'validator_question_exists';
}
}
Validator
class QuestionExistsValidator extends ConstraintValidator
{
private $entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
public function validate($value, Constraint $constraint)
{
die('I dont see this message...');
}
debug:container
Information for Service "validator.unique.question_exists"
---------------- -------------------------------------------------------------------
Option Value
---------------- -------------------------------------------------------------------
Service ID validator.unique.question_exists
Class App\Validator\Constraint\Question\QuestionExistsValidator
Tags validator.constraint_validator (alias: validator_question_exists)
validator.constraint_validator
Public no
Synthetic no
Lazy no
Shared yes
Abstract no
Autowired yes
Autoconfigured yes
If you are declaring the service yourself, then you should also be adding it's arguments:
services:
validator.unique.question_exists:
class: App\Validator\Constraint\Question\QuestionExistsValidator
arguments: ['#doctrine.orm.entity_manager']
tags:
- { name: validator.constraint_validator }
Other than that it should work without any tag aliases or validatedBy method.
Sidenote: for quite a while now in Symfony it's recommended to name (id) your services by the class names, that way auto-wiring could handle your service, and you wouldn't need the class parameter, i.e.:
services:
App\Validator\Constraint\Question\QuestionExistsValidator:
arguments: ['#doctrine.orm.entity_manager']
tags:
- { name: validator.constraint_validator }
I just used the default symfony Validator, as #xabbuh mentioned, and it worked.
__construct(ValidatorInterface $validator){
$question = new Question();
$errors = $validator->validate($question);
}
I should have not create my own validator with Validaton::createValidatorBuilder.
It does not matter if you use this config:
App\Validator\Constraint\Question\QuestionExistsValidator:
tags: ['validator.constraint_validator']
with no validatedBy()
or that:
validator.unique.question_exists:
class: App\Validator\Constraint\Question\QuestionExistsValidator
tags:
- { name: validator.constraint_validator, alias: validator_question_exists}
with:
public function validatedBy() {
return 'validator_question_exists';
}

UnexpectedTypeException when trying to create a custom validation constraint

I'm trying to create a custom validation constraint, this is the relevant code:
ValidCoupon.php
<?php
namespace Bcg\UtilsBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
/**
* #Annotation
*/
class ValidCoupon extends Constraint
{
public function validatedBy()
{
return 'valid_coupon';
}
public $message = 'The coupon is not valid.';
}
class ValidCouponValidator extends ConstraintValidator
{
public function validate($value, Constraint $constraint)
{
var_dump($value);
if (true) {
$this->context->addViolation(
$constraint->message,
array()
);
}
}
}
I call the service in the config.yml like this:
services:
validator.unique.valid_coupon:
class: Bcg\UtilsBundle\Validator\Constraints\ValidCoupon
tags:
- { name: validator.constraint_validator, alias: valid_coupon }
The validation.yml looks like this:
Bcg\UtilsBundle\Entity\Order:
properties:
coupon:
- Bcg\UtilsBundle\Validator\Constraints\ValidCoupon: ~
And the error I get is the following:
Expected argument of type
"Symfony\Component\Validator\ConstraintValidatorInterface",
"Bcg\UtilsBundle\Validator\Constraints\ValidCoupon" given 500 Internal
Server Error - UnexpectedTypeException
Full stack trace here.
I'm pretty stuck, it doesn't seem to find ValidCouponValidator I don't really know how to continue from here, I know that the public function validateBy() is executed, so it should be correctly overridden but it doesn't seem so...
Seems like you have a type in your validator service configuration :
You declare your ValidCoupon class as a validator instead of your ValidCouponValidator (which indeed implements the ConstraintValidatorInterface as the error complains about).
Try this:
services:
validator.unique.valid_coupon:
class: Bcg\UtilsBundle\Validator\Constraints\ValidCouponValidator
tags:
- { name: validator.constraint_validator, alias: valid_coupon }

#doctrine.orm.entity_manager not being given to a class registered as a service

I am trying to create a service in Symfony2 to automatically pass Doctrine\ORM\EntityManager to __construct to avoid having to pass it each time I instantiate the class, i.e.
// use this
$TestClass= new TestClass;
// instead of this
$entityManager = $this->getDoctrine()->getEntityManager();
$TestClass= new TestClass($entityManager);
I created a class EntityManagerUser, tried to register it as a service and TestClass extends that.
services.yml is included, as another service works, and I've double-checked by adding (then removing) a syntax error.
I read the docs, this, this and this and I've ended up with the code below, which doesn't pass #doctrine.orm.entity_manager. However, the controller_listener service does receive #templating.
I've cleared cache via the console and manually deleted app/cache but I still see this error:
ContextErrorException: Catchable Fatal Error: Argument 1 passed to Test\TestBundle\ServiceUser\EntityManagerUser::__construct() must be an instance of Test\TestBundle\ServiceUser\Doctrine\ORM\EntityManager, none given, called in D:\Documents\www\Test\live\src\Test\TestBundle\Controller\MyController.php on line 84 and defined in D:\Documents\www\Test\live\src\Test\TestBundle\ServiceUser\EntityManagerUser.php line 14
services.yml
services:
# this one doesn't throw an error and passes #templating to __construct
test.eventlistener.before_controller_listener:
class: Test\TestBundle\Eventlistener\BeforeControllerListener
arguments: [ #templating ]
tags:
- { name: kernel.event_listener, event: kernel.controller, method: onKernelController }
# the following one doesn't pass #doctrine.orm.entity_manager
test.service_user.entity_manager_user:
class: Test\TestBundle\ServiceUser\EntityManagerUser
arguments: [ #doctrine.orm.entity_manager ]
src/Test/TestBundle/ServiceUser/EntityManagerUser.php
namespace Test\TestBundle\ServiceUser;
use Doctrine\ORM\EntityManager;
class EntityManagerUser{
protected $entityManager;
public function __construct(EntityManager $entityManager){
$this->entityManager = $entityManager;
// N.B. it's not possible to do it this way:
// $this->entityManager = new EntityManager;
}
// also tried public function __construct($entityManager){
// and public function __construct(Doctrine\ORM\EntityManager $entityManager){
}
src/Test/TestBundle/Classes/TestClass.php
namespace Test\TestBundle\Classes\TestClass;
use Test\TestBundle\ServiceUser\EntityManagerUser;
class TestClass extends EntityManagerUser{
/* currently no functions */
}
In my controller, line 84
$test= new TestClass;
// I tested that this throws the same error, it does // $test= new EntityManagerUser;
What have I missed?
Services only get their arguments if they are called through the service constructor:
$this->get('test.service_user.entity_manager_user');
Declaring the class as a service doenst make a difference if you create a new class and extend the original.
What you could do is also declare this new class as a service and still have it extend the base class.
test.classes.test_class:
class: Test\TestBundle\Classes\TestClass\TestClass
arguments: [ #doctrine.orm.entity_manager ]
then you dont have to define the constructor in the extended class because it is the parent.
then get the class by doing:
$testClass = $this->get('test.classes.test_class');
//will be instanceof Test\TestBundle\Classes\TestClass\TestClass
I worked out how to do what I wanted, which was not define entityManager each time I was required in an instanciated class.
It made sense to rename EntityManagerUser to ContainerListener, a static class, and inject #service_container into it through services, so it can then also return other classes.
namespace Test\TestBundle\EventListener;
class ContainerListener{
static $container;
// knock out the parent::onKernelRequest function that we don't want
public function onKernelRequest($event){
return;
}
public function __construct($container){
self::$container = $container;
}
static function twig(){
return self::$container->get('twig');
}
static function entityManager(){
return self::$container->get('doctrine')->getEntityManager();
}
static function entityManagerConnection(){
$entityManager = self::$container->get('doctrine')->getEntityManager();
return $entityManager->getConnection();
}
}
services.yml
services:
test.event_listener.container_listener:
class: Test\TestBundle\EventListener\ContainerListener
arguments: [ #service_container ]
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
BaseClass.php gets entityManager
namespace Test\TestBundle\Class;
use Test\TestBundle\EventListener\ContainerListener;
class BaseClass{
public function __construct(){
$this->entityManager = ContainerListener::entityManager();
}
}
TestClass.php extends BaseClass as do others
class TestClass extends BaseClass(){
function someFunction(){
// etc etc
// $this->entityManager exists with no construct and without passing it
$stmt = $this->entityManager->getConnection()->prepare( $some_sql );
// etc etc
}
}
Somewhere in DefaultController.php
# nope # $entityManager = $this->getDoctrine()->getEntityManager();
# nope # $TestClass= new TestClass($entityManager);
$TestClass= new TestClass; # win!

Symfony2 - Custom validator and dependancy injection

I am trying to use dependancy injection for a custom validator, in order to be able to use the entityManager.
I followed the Symfony Example: Dependency Injection, but I am allways getting this error message:
FatalErrorException: Error: Class 'isdoi' not found in
/home/milos/workspace/merrin3/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Validator/ConstraintValidatorFactory.php
line 68
Here are my classes:
1. The IsDOI class:
<?php
namespace Merrin\MainBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
/**
* #Annotation
*/
class IsDOI extends Constraint
{
public $message_publisher_DOI = 'The Publisher DOI abbreviation does not correspond to the DOI you filled in !';
public $message_journal_DOI = 'No journal found with the DOI you filled in !';
public $journal;
public $doiAbbreviation;
public function validatedBy() {
return "isdoi";
}
public function getTargets()
{
return self::CLASS_CONSTRAINT;
}
}
2. The IsDOIValidator class:
<?php
namespace Merrin\MainBundle\Validator\Constraints;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class IsDOIValidator extends ConstraintValidator
{
private $entityManager;
public function __construct(EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}
public function validate($value, Constraint $constraint)
{
$em_mdpipub = $this->entityManager('mdpipub');
//Do some tests here...
}
}
3. Service:
merrin.main.validator.isdoi:
class: Merrin\MainBundle\Validator\Constraints\IsDOIValidator
arguments:
entityManager: "#doctrine.orm.entity_manager"
Where am I wrong? Thank you for your help.
You have wrong service file, when You add tags and alias you could use "isdoi" name
merrin.main.validator.isdoi:
class: Merrin\MainBundle\Validator\Constraints\IsDOIValidator
arguments:
entityManager: "#doctrine.orm.entity_manager"
tags:
- { name: validator.constraint_validator, alias: isdoi }
You're telling Symfony2 that the validator class for your constraint is isdoi (validateBy method). However, your validator is IsDOIValidator.
You must use :
public function validateBy()
{
return "IsDOIValidator";
}
However, if your Constraint class name is IsDOI, Symfony will automatically look for IsDOIValidator as a ConstraintValidator. The default behavior for validateBy is to append "Validator" to the constraint name, and look for the class with this name. So if you do not overload validateBy, Symfony2 will automatically search for IsDOIValidator.

catchable fatal error in passed argument (services) [duplicate]

I'm trying to build a custom validator running as a service (mainly for getting the entity manager).
I followed the doc and some blog posts but can't make it working. I have this error
Catchable Fatal Error: Argument 1 passed to
D\AjaxBundle\Validator\Constraints\SelectTypeValidator::__construct() must implement
interface Doctrine\Common\Persistence\ObjectManager, none given, called in
/AJAX/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Validator/ConstraintValidatorFactory.php on line 67 and defined in
/AJAX/src/D/AjaxBundle/Validator/Constraints/SelectTypeValidator.php line 14
services.yml
validator.selectType:
class: D\AjaxBundle\Validator\Constraints\SeletTypeValidator
arguments: ["#doctrine.orm.entity_manager"]
tags:
- { name: validator.selectType, alias: selectType }
SelectTypeValidator:
namespace D\AjaxBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Doctrine\Common\Persistence\ObjectManager;
class SelectTypeValidator extends ConstraintValidator
{
private $om;
public function __construct(ObjectManager $om)
{
$this->om = $om;
}
public function validate($value, Constraint $constraint)
{
$fieldOne = $this->om->getRepository('DAjaxBundle:City')->findOneBy(array('id' =>
$value->getId()));
if ($fieldOne == null) {
$this->context->addViolation($constraint->message, array('%string%' => $value->
getId()));
}
}
}
SelectType
namespace D\AjaxBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
class SelectType extends Constraint
{
public $message ='jakis text';
public function validateBy()
{
return 'selectType';
}
}
The service tag name should be validator.constraint_validator instead of validator.selectType
I'm not sure if this'll make a difference but it's worth a try...I never wrap service arguments with quotes:
arguments: [#doctrine.orm.entity_manager]

Categories