Symfony Callback constraints on collectiontype

Viewed 145

I want to constraint the value startTime must be lower than endTime.

Below is the inside of collectiontype form.

public function buildForm(FormBuilderInterface $builder, array $options)
{
    \Doctrine\Common\Util\Debug::dump($options);
    $builder
    ->add('startTime',TimeType::class, [
        'required' => false,
        'input' => 'string',
    ])
    ->add('endTime', TimeType::class, [
        'required' => false,
        'input' => 'string',
        'constraints'=>[
             new Assert\Callback(array($this, 'validateInterval')),
        ],
    ])
}



public function validateInterval($value, ExecutionContextInterface $context)
{
    $form = $context->getRoot();
    $data = $form->getData();

    print_r($data);

    if ($data[shoul be some number]['startTime'] >= $value) {
        $context
            ->buildViolation('The end value has to be higher than the start value')
            ->addViolation();
    }
}

$data is this below

Array ( 
    [data] => Array ( 
        [1] => Array ( 
            [id] => 1 
            [startTime] => 09:00:00 
            [endTime] => 18:00:00  
        ) 
        [2] => Array ( 
            [id] => 2 
            [startTime] => 09:00:00 
            [endTime] => 18:00:00  
        ) 
        [3] => Array ( 
            [id] => 3 
            [startTime] => 09:00:00 
            [endTime] => 18:00:00 
        ) 
        [4] => Array ( 
            [id] => 4 
            [startTime] => 09:00:00 
            [endTime] => 18:00:00  
        ) 
        [5] => Array ( 
            [id] => 5 
            [startTime] => 09:00:00 
            [endTime] => 18:00:00  
        ) 
        [6] => Array ( 
            [id] => 6 
            [startTime] => 09:00:00 
            [endTime] => 18:00:00  
        ) 
        [7] => Array ( 
            [id] => 7 
            [startTime] => 09:00:00 
            [endTime] => 18:00:00 
        ) 
    ) 
)

I want to pass the right [id] to the validateInterval constraint.

Is it possible to pass different numbers to each fields?

help me please.

0 Answers
Related