Advertisement
Sajgoniarz

Syfony 3 FormType refactor

May 17th, 2018
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.62 KB | None | 0 0
  1. //SomeType
  2.  
  3. public function buildForm(FormBuilderInterface $builder, array $options)
  4. {
  5.     $model = $builder->getData();
  6.     $builder
  7.         ->add('someField', null, [
  8.             'label' => 'Some Field',
  9.             'choice_label' => 'name',
  10.             'disabled' => $options['preview_only'],
  11.             'attr' => [
  12.                 'tabindex' => 8
  13.             ],
  14.             'query_builder' => function (EntityRepository $er) use ($model){
  15.                 $id = $model->getRelatedEntity()->getId();
  16.                 $q =  'u.isDeleted = 0';
  17.                 $q = $id ? $q.' OR u.id = '.$id : $q;
  18.                 return $er->createQueryBuilder('u')->where($q);
  19.             },
  20.             'choice_attr' => function($val, $key, $index) {
  21.                 return $val->getIsDeleted() ? ['disabled' => 'disabled'] : [];
  22.             },
  23.     ])
  24. }
  25.  
  26. //MagicType
  27.  
  28. public function buildForm(FormBuilderInterface $builder, array $options)
  29. {
  30.     $model = $builder->getData();
  31.     $builder
  32.         ->add('someMagicField', null, [
  33.             'label' => 'Magic Field',
  34.             'choice_label' => 'name',
  35.             'disabled' => $options['preview_only'],
  36.             'attr' => [
  37.                 'tabindex' => 8
  38.             ],
  39.             'query_builder' => function (EntityRepository $er) use ($model){
  40.                 $id = $model->getRelatedEntity()->getId();
  41.                 $q =  'u.isDeleted = 0';
  42.                 $q = $id ? $q.' OR u.id = '.$id : $q;
  43.                 return $er->createQueryBuilder('u')->where($q);
  44.             },
  45.             'choice_attr' => function($val, $key, $index) {
  46.                 return $val->getIsDeleted() ? ['disabled' => 'disabled'] : [];
  47.             },
  48.     ])
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement