php - Symfony validation -
i'm working in bundle user creates "comision" using form, , i'm trying check user still have "credit". created custom validator thas queries past comisions ant throw error if credit not enough
my problem if user submits date wrong format in "date" field (i.e. 32-13-20122 24:05) symfony still tries run custom validation , sort of errors (because $comision->getdate() null , not valid datetime object)
i'm getting error: clone method called on non-object
i can check if value of $comision->getdate() valid datetime in custom validator, seems me should not necessary since added rules in date property.
this entity (simplified)
/** * @myvalidation\totalhours() */ class comision { /** * @orm\column(type="datetime") * @assert\datetime() * @assert\notnull() */ protected $date; /** * @orm\column(type="decimal", nullable=false, scale=1) * @assert\notblank() */ 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 , 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 cutom validator checks past comisions find if user still has "credit"
public function validate($comision, constraint $constraint) { $from = clone $comision->getdate(); $from->modify('first day of month'); $to = clone $comision->getdate(); $to->modify('last day of month'); $credit = $this->em->getrepository("comisionsbundle:comision")->comisionsbydate($comision,$from, $to); ...
one way group constraints described in docs.
this way can define 2 groups of contraints whereas second group validated if constraints in first group valid.
regarding use case, put custom constraint in different group default 1 sure have proper $comision datetime object.
Comments
Post a Comment