0

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.

Edited by cossay

1
Contributor
1
Reply
24
Views
8 Months
Discussion Span
Last Post by cossay
1

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.