4

I have a class

Class SubjectSegment{ 
 /**
 *@CustomAssert\MyCitizenshipNumber()
 */ 
 private $citizenshipNumber; 
 /**
 *@CustomAssert\MyDate()
 */ 
 private $citizenshipNumberIssuedDate;
}

what i really want to do is valid citizenshipNumberIssuedDate if citizenshipNumber is present...

What is the best way to achieve this

Madhab452
  • 303
  • 4
  • 13

3 Answers3

4

Hi you should look on http://symfony.com/doc/current/reference/constraints/Callback.html

use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\ExecutionContext;

/**
 * @Assert\Callback(methods={"isCitizenDateValid"})
 */

Class SubjectSegment{ 
    /**
    *@CustomAssert\MyCitizenshipNumber()
    */ 
    private $citizenshipNumber; 
    /**
    *@CustomAssert\MyDate()
    */ 
    private $citizenshipNumberIssuedDate;

    public function isCitizenDateValid(ExecutionContext $context)
    {
        //Do your validation here for your exemple :
        if(null === $this->citizenshipNumber && null === $this->citizenshipNumberIssuedDate) {
            $context->addViolationAtSubPath('citizenshipNumberIssuedDate', 'The date is required', array(), null);
        }
    }
}
pietro
  • 902
  • 10
  • 22
  • What i really required is to escape @CustomAssert\MyDate when (null == $this->citizenshipNumber ). i am using symfony 2.6 – Madhab452 Feb 04 '15 at 05:49
  • Your comment reply to your question `if(null === $this->citizenshipNumber && null === $this->citizenshipNumberIssuedDate) { // put $context->addViolation... } ` – pietro Feb 04 '15 at 09:49
  • In both cases if(null === $this->citizenshipNumber ) and if(null != $this->citizenshipNumber){} @CustomAssert\MyDate() is validating. – Madhab452 Feb 05 '15 at 04:58
  • Is there any way that i can use @Assert\Expression{ this.citizenshipNumber != null , @Assert\MyDate() }... that would be great if we have such feature in symfony 2. – Madhab452 Feb 05 '15 at 05:34
  • So remove your `/** @CustomAssert\MyDate() */` and add the validation in `isCitizenDateValid` method with your custom Assert Class. – pietro Feb 05 '15 at 07:42
  • I used group validation .. and it worked. Thanks paistra anyways – Madhab452 Feb 05 '15 at 10:22
3

You can archive this in two way:

1) callback validator (validated by a class method)

2) With a class constraint validator

I suggest the second way, so you must configure/set the validator to class scope like:

/**
 * @Annotation
 */
class MyCitizenshipNumber  extends Constraint {

...

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

} 

so you can access to all class field member

Remember to put the validator on top of the class definition like:

/**
 *@CustomAssert\MyCitizenshipNumber()
 */ 
 Class SubjectSegment{ 

 private $citizenshipNumber; 

 /**
 *@CustomAssert\MyDate()
 */ 
 private $citizenshipNumberIssuedDate;
}

Hope this help

Matteo
  • 37,680
  • 11
  • 100
  • 115
  • what i need is to call constriaint MyDate only when citizenshipNumber is not blank.. class constraint validator. – Madhab452 Feb 02 '15 at 09:32
  • You want to call the date validator inside the citizenshipNumber validator? if so take a look at [this](http://stackoverflow.com/questions/17337427/symfony2-call-emailvalidator-inside-a-custom-validator). If this is your solutions probably you must inject the `validator` component inside the citizenshipNumber validator – Matteo Feb 02 '15 at 09:43
3
Class SubjectSegment{   

    /**  
    *@CustomAssert\MyCitizenshipNumber()  
    */  
    private $citizenshipNumber;   

    /** 
    *@CustomAssert\MyDate(group={"is_my_citizenship_number"})  
    */   
    private $citizenshipNumberIssuedDate;

    public function getGroupSequence(){ 
        $sequence = ['SubjectSegment']; 
        if(null != $this->CitizenshipNumber){ 
          $sequence[] = 'is_my_citizenship_number'; 
        } 
        return $sequence; 
    } 
}

validator:

$violations = $container->get('validator')->validate($segment = new SubjectSegmet(), null,$segment->getGroupSequence() );
pietro
  • 902
  • 10
  • 22
Madhab452
  • 303
  • 4
  • 13