php - Symfony2 validation - How to use a value from the form? -


i have form have fill in information. 1 of fields of form, need create custom validator. in fact, need valdiate entered string in specific format ab.123.cd

i able validate using regexp, "ab" should eqal field of form, need access other field in validator class.

here validator:

public function validate($value, constraint $constraint) {            preg_match('/[^\/]+/i', $value, $publisherdoiabbr);     if($publisherdoiabbr[0] !== $enquiry->getpublisher()->getdoiabbreviation()) {         $this->context->addviolation($constraint->message_publisher_doi);     }     $this->context->addviolation($constraint->message_journal_doi); } 

i need here $enquiry->getpublisher()->getdoiabbreviation()

do know how can access values of form in validator class?

thank in advance.

what want make "class constraint" validator.

scroll down here:

http://symfony.com/doc/current/cookbook/validation/custom_constraint.html

until class constraint section example.

================================================================

i don't understand te "gettargets()"

add method constraint class (not validator class)

class myconstraint extends constraint {     public function gettargets()     {         return self::class_constraint;     } 

once has been added validator method receive object instead of single value.

class myvalidator extends constraintvalidator {     public function validate($value, constraint $constraint)     {         // $value object, adjust code accordingly 

Comments