I have the following class that I intend to validate by passing it in the symfony validator.

<?php
namespace Shop;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints as Assert;
use Shop\Validation\Constraint as ShopAssert;

class Country {

    /**
     *
     * @var int
     */
    public $id;

    /**
     *
     * @var string
     */
    public $name;

    /**
     *
     * @param ClassMetadata $metadata           
     */
    public static function loadValidatorMetadata(ClassMetadata $metadata) {

        $metadata->addPropertyConstraint('name', new Assert\NotBlank(array(
            'message' => 'Country name should not be blank.' 
        )));

        $metadata->addPropertyConstraint('name', new ShopAssert\CountryUniqueName(array(
            'country_id' => null //I NEED ACCESS TO THE COUNTRY ID FROM HERE
        )));
    }
}

The "CountryUniqueName" constraint is supposed to check the uniqueness of a given country instance's name. It also has the option to skip a given country , by Id, in the uniqueness check. My problem is how to access the object (country) being validated from inside the "loadValidatorMetadata" static method through the "ClassMetadata" class parameter. All the examples i have come across manually pass in values. None of them shows how to access the object being validated.

I solved my own problem. The solution is to create a custom Class Constraint Validator.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.