Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8214725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:33:10+00:00 2026-06-07T11:33:10+00:00

I have some problems with custom validator constraints. I want to validate a field

  • 0

I have some problems with custom validator constraints. I want to validate a field code in my entity TicketCode.

But the validator is not called when I use if(form->isValid), I don’t understand why.

My custom Validator needs the entity Manager to check with the DataBase if this code corresponds, else the validator sends message error.

My Entity

     <?php

namespace My\MyBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use My\MyBundle\Validator as TicketAssert;


/**
 * My\MyBundle\Entity\TicketCode
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="My\MyBundle\Entity\TicketCodeRepository")
 */
class TicketCode
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string $code
     *
     * @ORM\Column(name="code", type="string", length=255)
     * @TicketAssert:ValidCode(entity="TicketBundle:TicketCode", property="code")
     */
    protected $code;
 }

My Class Constraint

<?php

namespace My\MyBundle\Validator;

use Symfony\Component\Validator\Constraint;

/**
 * @Annotation
 */

class ValidCode extends Constraint
{
    public $message = 'Code Invalide';
    public $entity;
    public $property;

    public function ValidatedBy()
    {
        return 'my_ticket_validator';
    }

    public function requiredOptions()
    {
        return array('entity', 'property');
    }

    public function targets()
    {
        return self::CLASS_CONSTRAINT;
    }
}

My Validator

<?php

namespace My\MyBundle\Validator;

use Doctrine\ORM\EntityManager;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;


class ValidCodeValidator extends ConstraintValidator
{

    private $entityManager;

    public function __construct(EntityManager $entityManager)
    {
        $this->entityManager = $entityManager;
    }

    public function isValid($value, Constraint $constraint)
    {
        $todayInObject = new \DateTime();  //Renvoit la date du jour
        var_dump($todayInObject);

        $ticketCode = $this->entityManager->getRepository('MyMyBundle:TicketCode')->findOneBy(array('code' => $constraint->property));

        if ($ticketCode != null) {

            if ($ticketCode->nbMaxUse != 0 && ($todayInObject >= $ticketCode->getDateBegin && $todayInObject <= $ticketCode->getDateEnd())) {

                return true;
            }
            else {
                $this->setMessage($constraint->message);
                return false;
            }
        }
        else {
            $this->setMessage($constraint->message);
            return false;
        }

        return false;
    }
}

Finally my service.yml in my bundle.

parameters:

    my_ticket.validator.validcode.class:      My\MyBundle\Validator\ValideCodeValidator

services:

    my_ticket.validator.validcode:
       class:      %my_ticket.validator.validcode.class%
       arguments: [@doctrine.orm.entity_manager]
       tags:
            - { name: validator.constraint_validator, alias: my_ticket_validator }

If you have any idea why my validator is never called, explain to me thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T11:33:12+00:00Added an answer on June 7, 2026 at 11:33 am

    You have an upper V in your function name ValidatedBy(). It is supposed to be validatedBy().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I’m having some problems with validation. I have created a custom validator called RequiredFieldRule
I have some problems with my first Open Graph custom action: I've created a
Begining with JSP and servlet development, I have some problems with a bodyless custom
I have a FluentValidation validator that I want to use to validate a booking.
I'm trying to create a controller for a custom complex object but have some
I have some problems getting the custom formated JSON data into a dijit.form.FilteringSelect. It
I have had some problems with authentication in ASP.NET. I'm not used most of
I have problems adding a custom validation to validate fields entering time (with jQuery
I have some custom validation code, which includes a $formatter. (I store currency in
I have a form that is using the jQuery validate functionality, with some custom

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.