Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [Symfony2][SF][SF2] Pole w formularzu, które stworzy obiekt
Forum PHP.pl > Forum > PHP > Frameworki
Fluke
Witam,

Mam pytanie odnośnie pojedynczego pola w formularzu, które po uzupełnieniu stworzy mi obiekt danego typu.
Mam encję Person z polami: [id, firstname, lastname, email, address]
Formularz:
  1. $builder->add(
  2. 'email',
  3. 'email',
  4. [
  5. 'property_path' => 'person.email',
  6. 'data_class' => Person::class,
  7. ]
  8. );

I w ten sposób chciałbym, żeby mi stworzyło encję typu Person z uzupełnionym polem email. Niestety dostaję: Expected argument of type "object or array", "NULL" given.

Jest sposób na to aby automatycznie w Symfony2 stworzył mi taką encję ? Zaznaczę, że próbowałem jeszcze z "empty_data" który zwracał obiekt Person ale też nie działa. Też chciałbym nie używać DataTransformer.

Pozdrawiam.
destroyerr
Nie wiem o co chodzi. Dodajesz pole email i chcesz żeby było obiektem klasy Person? Jeżeli chcesz żeby formularz tworzył Ci obiekt to musisz ustawić odpowiednią opcję.
Poza tym podajesz treść błędu, nie wiemy gdzie się pojawia, nie wiemy co wywołujesz, że pojawia się ten błąd.
Fluke
@destroyerr
Właśnie tak. Tak żebym ewentualnie nie musiał tworzyć formularza typu PersonType z 1 polem email który ma $resolver(['data_class] => Person::class);
kpt_lucek
Poczytaj o transformerach,
  1.  
  2. class MojFormType extends AbstractForm {
  3.  
  4. /**
  5.   * @param FormBuilderInterface $builder
  6.   * @param array $options
  7.   */
  8. public function buildForm(FormBuilderInterface $builder, array $options)
  9. {
  10. $builder
  11. ->add('school', 'EntityInt', array(
  12. 'label' => "Label",
  13. 'class' => 'ABCBundle\Entity\SuperEntity',
  14. 'query' => '',
  15. 'attr' => array(
  16. 'class' => 'super_class',
  17. )
  18. ));
  19. }
  20. }

  1. // MojBundle\Form\FormTypes\EntityIntType
  2. // typ EntityInt - zdefiniowany przeze mnie
  3. public function buildForm(FormBuilderInterface $builder, array $options)
  4. {
  5. $transformer = new Transformer($this->em);
  6. $transformer->setClass($options['class']);
  7. $transformer->setProperty('id');
  8. $builder->add(
  9. $builder->create('id', 'integer', array(
  10. 'error_bubbling' => false,
  11. 'label' => 'labelka'
  12. 'attr' => array(
  13. 'class' => "superklasa",
  14. )
  15. ))
  16. ->addModelTransformer($transformer)
  17. );
  18. }

Transofmera niestety podać nie mogę. Jeżeli Ci to pomoże, to...
- Interface DataTransformerInterface
- setClass przyjmuje klasę jako parametr - Twoją encję która będzie transformowana obustronnie {property}->obiekt, obiekt->{property}
- setProperty pole, dokladniej nazwa kolumny - znajdziesz w encji (Doctrine ORM Entity)

W praktyce to transformery bardzo ułatwiają życie i nie widzę potrzeby wyszukiwania innego sposobu smile.gif


Pozdrawiam
Fluke
Do tego zrobiłem sobie klasę:

  1. class CustomType extends AbstractType {
  2. /**
  3.   * @var string
  4.   */
  5. protected $name;
  6.  
  7. /**
  8.   * @var callable
  9.   */
  10. protected $callback;
  11.  
  12. /**
  13.   * @var array
  14.   */
  15. protected $defaults;
  16.  
  17. /**
  18.   * @param string $name
  19.   * @param callable $callback
  20.   * @param array $defaults
  21.   */
  22. function __construct($name, $callback, array $defaults = []) {
  23. $this->name = $name;
  24. $this->callback = $callback;
  25. $this->defaults = $defaults;
  26. }
  27.  
  28. /**
  29.   * @param FormBuilderInterface $builder
  30.   * @param array $options
  31.   */
  32. public function buildForm(FormBuilderInterface $builder, array $options) {
  33. call_user_func_array($this->callback, [$builder, $options]);
  34. }
  35.  
  36.  
  37. /**
  38.   * @param OptionsResolverInterface $resolver
  39.   */
  40. public function setDefaultOptions(OptionsResolverInterface $resolver) {
  41. $resolver->setDefaults($this->defaults);
  42. }
  43.  
  44. /**
  45.   * @return string The name of this type
  46.   */
  47. public function getName() {
  48. return $this->name;
  49. }
  50. }


I mogę teraz używać w ten sposób:
  1. new CustomType('my_custom_form', function ($builder, $options) {
  2. $builder->add('firstname', 'text);
  3. $builder->add('lastname', 'text');
  4. }, ['data_class' => User::class]);
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.