1

Use case

I am learning Symfony2 and am creating a Table Tennis tracking app to learn the framework. I have configured my entities as follows.

Player 1..n Result n..1 Match

On my form I'd like to validate that the scores for a match are correct.

Implementation

Match has an ArrayCollection() of results.

My MatchType and ResultType forms contain the following.

 // Form\MatchType
    $builder->add('matchType', 'entity', array(
            'class' => 'PingPongMatchesBundle:MatchType',
            'property' => 'name',
        )
    )
    ->add('results', 'collection', array(
            'type' => new ResultType(),
            'allow_add' => true,
            'by_reference' => false,
        )
    )
    ->add('notes');

 // Form\ResultType
    $builder->add('player', 'entity', array(
                'class' => 'PingPongPlayerBundle:Player',
                'query_builder' => function(EntityRepository $er) {
                    return $er->createQueryBuilder('p')
                        ->orderBy('p.firstName', 'ASC');
                },
            ))
            ->add('score');

Issue

I need to be able to validate the scores. However I'm not sure how to approach this type of validation as I need to compare two instances of my Result#score in order to know if they are valid or not.

Is anyone able to suggest a method or approach that I could use in order to be able to compare Result#score between the two different instances? Can I validate the ArrayCollection in the Match entity for example?

David Yell
  • 11,756
  • 13
  • 61
  • 100

2 Answers2

0

You could creat a custom validator constraint on Match entity. http://symfony.com/doc/2.0/cookbook/validation/custom_constraint.html

tomas.pecserke
  • 3,260
  • 25
  • 26
0

Take a look in Callback constraint:

http://symfony.com/doc/2.1/reference/constraints/Callback.html

vjnrv
  • 91
  • 1
  • 2