I work in a bundle where the user creates an “agreement” using the form, and I'm trying to verify that the user still has “credit”. Therefore, I created a custom validator that requests past messages and throws an error if the loan is not enough.
My problem is that if the user sends a date with the wrong format in the "date" field (i.e. 32-13-20122 24:05), Symfony still tries to do my own check, and I get all kinds of errors ( because $comision->getDate() is null , not a valid DateTime object).
I get this error:
clone method called by non-object
I can also check if the value of $comision->getDate() valid date-time in my custom validator, but it seems to me that this is not necessary since I added these rules to the date property.
This is my essence (simplified)
class Comision { protected $date; protected $hours; ...
My form class ...
class NewComisionType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('date', DateTimeType::class, array( 'widget' => 'single_text', 'label' => 'Starting date and time', 'format' => 'dd/MM/yyyy HH:mm' )) ->add('hours', ChoiceType::class, array( 'label'=> 'How many hours', 'choices' => array( '1:00' => 1, '1:30' => 1.5, '2:00' => 2, '2:30' => 2.5, '3:00' => 3 ) )) ...
And my crit validator, which checks past agreements to find if the user still has a “credit”
public function validate($comision, Constraint $constraint) { $from = clone $comision->getDate(); $from->modify('first day of this month'); $to = clone $comision->getDate(); $to->modify('last day of this month'); $credit = $this->em->getRepository("ComisionsBundle:Comision")->comisionsByDate($comision,$from, $to); ...