src/Form/LoginType.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\Form;
  3. use App\Entity\Customer;
  4. use App\Repository\CustomerRepository;
  5. use Symfony\Component\Form\AbstractType;
  6. use Symfony\Component\Form\FormBuilderInterface;
  7. use Symfony\Component\OptionsResolver\OptionsResolver;
  8. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  9. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  10. use Symfony\Component\Validator\Constraints\NotBlank;
  11. class LoginType extends AbstractType
  12. {
  13.     private $customerRepository;
  14.     public function __construct(CustomerRepository $customerRepository
  15.             {
  16.                 $this->customerRepository $customerRepository;
  17.             }
  18.     public function buildForm(FormBuilderInterface $builder, array $options)
  19.     {
  20.         
  21.      
  22.         $customers $this->customerRepository->findBy(array('deleted_at' => NULL));
  23.             
  24.         $builder
  25.          ->add('customer'ChoiceType::class, [
  26.             'choices' => $customers,
  27.              // string value that will be submitted by the form
  28.             'choice_value' => 'customer_name',
  29.             'choice_label' => function($customer$key$index) {
  30.                // var_dump($customer);
  31.                 // Result : int(0) 
  32.                 return $customer->getCustomerName();
  33.                 // Exception !
  34.             },
  35.             'choice_attr' => function($customer$key$index) {
  36.                 return ['class' => $customer->getId()];
  37.             },
  38.             'constraints' => array(
  39.                 new NotBlank(['groups' => ['form_validation_only']]),
  40.                     ),
  41.         ])
  42.         ->add('save'SubmitType::class, array('label' => 'Login'))
  43.         
  44.             
  45.         ;
  46.     }
  47.     public function setDefaultOptions(OptionsResolver $resolver)
  48.     {
  49.         $resolver->setDefaults(array(
  50.             'validation_groups' => ['form_validation_only'],
  51.         ));
  52.     }
  53.    
  54. }