PUGX / PUGXAutoCompleterBundle

Add an autocomplete field to your Symfony forms
GNU Lesser General Public License v3.0
93 stars 38 forks source link

Error with newAction #67

Closed OpenActu closed 3 years ago

OpenActu commented 3 years ago

Hi, I have trouble with the current code. It's very easy to explain.

Here is my controller

public function new(Request $request, EntityManagerInterface $entityManager): Response
    {
        $announce = new Announce();
        $form = $this->createForm(AnnounceType::class, $announce, [
          'entity_manager' => $entityManager
        ]);
        $form->handleRequest($request);
...

Here is my form

...
    $form
    ->add('customer', AutocompleteType::class, [
              'label_format' => 'announce.customer',
              'class' => Customer::class,
              'attr' => [
                'class' => 'autocomplete_customer',
              ],
              'required' => false,
            ])
...

When I load the page, I have the error Return value of PUGX\AutocompleterBundle\Form\Transformer\ObjectToIdTransformer::transform() must be of the type string, null returned

To solve this problem, I only change the return definition into vendor from method PUGX\AutocompleterBundle\Form\Transformer\ObjectToIdTransformer::transform($object): string to to PUGX\AutocompleterBundle\Form\Transformer\ObjectToIdTransformer::transform($object): ?string

After that, not any trouble found.

Can you change it in source, or is it any reason to have transform strictly with string output ?

garak commented 3 years ago

The reason is that having an object with a null id doesn't make sense.

OpenActu commented 3 years ago

The reason is that having an object with a null id doesn't make sense.

Thanks for the response but I don't understand it. When I create a new entity, it's not logic that I first persist my entity and then call the form. Is a process exists to avoid this behavior other than use persist before the createForm ?

Remark : Maybe my remark introduces stability risks and in this case, i could understand why the change of null output is dangerous.

garak commented 3 years ago

Thanks for the response but I don't understand it. When I create a new entity, it's not logic that I first persist my entity and then call the form. Is a process exists to avoid this behavior other than use persist before the createForm ?

Of course, but the entity you're using for autocomplete is not the new entity: it's an existing entity you're trying to relate with the new one (in your example, you're creating a new Announce and relating it to an existing Customer)

OpenActu commented 3 years ago

Hi, I solve the issue by changing the __construct() definition for new entity. In fact, the create of new entity inject an announce with null id (see below).

Thanks for your help !

origin
# old definition produced by make:entity
class Announce
{
  public function __construct() {
    $this->customer   = new Customer();
  }
}
correction
# new definition
class Announce
{
  public function __construct() {
    $this->customer   = null;
  }
}