php - Symfony custom constraint to Reset Password function form -
i have implemented password reset page project. working, try use best practices , reusable code. here problem. have formtype password reset form , resetting controller. know validating means mapping fields of form type object of class , having validation rules on properties of object. password reset, think using "non mapped" fields required. now, submit form , values in resettingcontroller. there check myself if user email, birthday , security answer exists. working think not implemented right way. possible add custom constraint whole resetformtype , when submitting form, should call constraint checks if user exists. want use symfony validation system, not "if, else , if..." wrote in controller.
here code:
resetpwformtype:
public function buildform(formbuilderinterface $builder, array $options) { $builder ->add('username', null, array( 'label' => 'resetting.request.username', 'translation_domain' => 'fosuserbundle', 'mapped' => false )) ->add('birthday', 'date', array( 'years' => range(date('y') - 74, date('y') - 18), 'label' => 'register.user.contact.birthdayblank', 'translation_domain' => 'startup', 'mapped' => false )) ->add('plainsecurityanswer', 'text', array( 'label' => 'register.user.security.answer', 'translation_domain' => 'startup', 'mapped' => false )); }
resettingcontroller:
$form = $this->container->get('form.factory')->create(new resetpwformtype()); $form->handlerequest($this->container->get('request')); $username = $form->get('username')->getdata(); $birthday = $form->get('birthday')->getdata(); $securityanswer = $form->get('plainsecurityanswer')->getdata(); /** @var $user userinterface */ $user = $this->container->get('doctrine')->getrepository('userbundle:user')->finduserforpasswordreset($username, $birthday, $securityanswer); $jsonreturn = array(); if($user !== null) { // etc. }
i'd use here "$form->isvalid()" method, not saving each field of form in variable, passing repository method , checking there if user exists manually. , that, think constraint whole form required ..?
update: think need model class reset process, , whole class, can add custom validation constraint.
regards.
you can use 'constraints' option in form builder:
public function buildform(formbuilderinterface $builder, array $options) { $builder ->add('username', null, array( 'label' => 'resetting.request.username', 'translation_domain' => 'fosuserbundle', 'mapped' => false, 'constraints' => [ new yourcustomconstraint() ] )); }
Comments
Post a Comment