3

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.

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
jnpps
  • 39
  • 4
  • Seeing your code, I'd assume you read [this](https://stackoverflow.com/q/42935452). If so, pay close attention to Will B.s comment (it's the last). It's basically exactly what you want. – Yoshi Jul 07 '21 at 11:05

0 Answers0