symfony-console-form icon indicating copy to clipboard operation
symfony-console-form copied to clipboard

How to insert an empty value while editing entity?

Open secit-pl opened this issue 4 years ago • 8 comments

I have the following code, it's used to edit the contact entity:

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $io = new SymfonyStyle($input, $output);

        $contacts = $this->doctrine->getRepository(Contact::class)->findAll();
        if (!$contacts) {
            $io->note('No contacts found.');

            return Command::SUCCESS;
        }

        $contact = $io->choice('Contact', $contacts);

        $this->getHelper('form')->interactUsingForm(
            ContactType::class,
            $input,
            $output,
            [],
            $contact
        );

        $this->doctrine->getManager()->flush();

        $io->newLine(2);
        $io->success('Contact changed');

        return Command::SUCCESS;
    }

The input form in console looks as follows:

image

The question is, what should I do to clear the email field value? Hitting "enter" leave the data unchanged. Entering empty space don't change anything. I'd like to set the email to empty string or to null, now it can be done?

secit-pl avatar Apr 09 '21 18:04 secit-pl

I'm not sure if it's supported. What does your form type look like?

matthiasnoback avatar Apr 09 '21 18:04 matthiasnoback

<?php

namespace App\Form;

use App\Entity\Contact;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ContactType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name')
            ->add('email')
            ->add('phoneNumber')
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Contact::class,
        ]);
    }
}

secit-pl avatar Apr 09 '21 18:04 secit-pl

Thanks, but where does @.*** come from?

matthiasnoback avatar Apr 09 '21 18:04 matthiasnoback

I have the different console command to add contacts. This one is used to edit the contacts persisted in the database. So in short, the form default value comes from the entity loaded from the database.

The easier example, explaining the same problem will be:

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $contact = new Contact();
        $contact->setName('name')
            ->setEmail('[email protected]')
            ->setPhoneNumber('+48123456789');

        $this->getHelper('form')->interactUsingForm(
            ContactType::class,
            $input,
            $output,
            [],
            $contact
        );

        $contact->getEmail(); // <- I'd like to have here after form processing the empty string or null

        // ...
    }

secit-pl avatar Apr 09 '21 18:04 secit-pl

I'm afraid that won't work with this library... Are the default values meant as a suggestion? Because then they could be presented in some other way, e.g. as part of the label or help text.

matthiasnoback avatar Apr 09 '21 19:04 matthiasnoback

The default values are the values which the edited object has already filled in. I do not want to change them if "Enter" was hit without typing anything.

Idea: maybe the bundle can implement the feature to define the special string which user should insert to clear the file value?

$this->getHelper('form')->interactUsingForm(
            ContactType::class,
            $input,
            $output,
            [],
            $contact,
            '~' // the special clear character
        );

The example question:

Email [[email protected]] (Hit "Enter" to use the default value or type "~" to use an empty value): 

What do you think?

secit-pl avatar Apr 09 '21 19:04 secit-pl

I don't think we can take special characters to mean "empty". Instead, I'd suggest asking another question: you left the field empty, would you like to use the default value? yes/no That behavior may have to be configurable though, because some people may like pressing enter to use default values.

matthiasnoback avatar Apr 14 '21 06:04 matthiasnoback

The same goes for this issue by the way: I have only a very small amount of time to spend on this library, so it would be very helpful if you can do some work in a PR.

matthiasnoback avatar Apr 14 '21 07:04 matthiasnoback