0

I have class with many variables. Few of them are File types

 class Proposal {
/**
 *  @ORM\ManyToOne(targetEntity="File")
 *  @ORM\JoinColumn(name="clientCommFile_id", referencedColumnName="id")  */
private $clientCommFile;

/**
 *  @ORM\ManyToOne(targetEntity="File")
 *  @ORM\JoinColumn(name="contractFile_id", referencedColumnName="id")
 */
private $contractFile;

/**
 *  @ORM\ManyToOne(targetEntity="File")
 *  @ORM\JoinColumn(name="proposalFile_id", referencedColumnName="id")
 */
private $proposalFile;

I need do create some validation rule to validate which will inspect ids of files. The ids must be different because it referres to files in database. Is some way, how to do this?

1 Answer 1

1

So you want clientCommFile, contractFile and proposalFile to be different? Assuming one can just compare objects, here is how it goes:

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

/**
 * @Assert\Callback(methods={"areFilesValid"})
 */
class Proposal {

    // ..

    public function areFilesValid(ExecutionContext $context) {
        if($this->clientCommFile != null && $this->clientCommFile->equals($this->contractFile)){
            $propertyPath = $context->getPropertyPath() . '.options';
            $context->setPropertyPath($propertyPath);
            $context->addViolation('ClientCommFile and ContractFile are equal', array(), null);
        }
    }
}

Of course you must implement the equals method on your objects.

If it gets more complicated or you need Database access, have a look at custom validators

Sign up to request clarification or add additional context in comments.

1 Comment

Database access is not needed here. Can I just comapare somehow file ids I get from form. My intention is: add ids into array, order asc and comare wheter the i-1 index equals i index. Instead callback method, I used getter method, but it doesnt work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.