Symfony2验证 - 如何使用表单中的值?

I have a form where I have to fill in some information. For one of the fields of the form, I need to create a custom validator. In fact, I need to valdiate that the entered string is in a specific format like ab.123.cd

I am able to validate this by using regexp, but the "ab" should be eqal to another field of my form, so I need to access this other field in my validator class.

Here is my 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 the $enquiry->getPublisher()->getDoiAbbreviation()

Do you know how can I access the values of my form in the validator class?

Thank you in advance.

What you want to do is make a "CLASS CONSTRAINT" validator.

Scroll down here:

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

until you get to the class constraint section for an example.

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

I don't understand what is te "getTargets()"

Add this method to your Constraint class (not the validator class)

class MyConstraint extends Constraint
{
    public function getTargets()
    {
        return self::CLASS_CONSTRAINT;
    }

Once that has been added then your validator method will receive an object instead of a single value.

class MyValidator extends ConstraintValidator
{
    public function validate($value, Constraint $constraint)
    {
        // $value will be an object, adjust your code accordingly