Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to insert an empty value while editing entity? #48

Open
secit-pl opened this issue Apr 9, 2021 · 8 comments
Open

How to insert an empty value while editing entity? #48

secit-pl opened this issue Apr 9, 2021 · 8 comments

Comments

@secit-pl
Copy link

secit-pl commented Apr 9, 2021

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?

@matthiasnoback
Copy link
Owner

matthiasnoback commented Apr 9, 2021 via email

@secit-pl
Copy link
Author

secit-pl commented Apr 9, 2021

<?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,
        ]);
    }
}

@matthiasnoback
Copy link
Owner

matthiasnoback commented Apr 9, 2021 via email

@secit-pl
Copy link
Author

secit-pl commented Apr 9, 2021

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

        // ...
    }

@matthiasnoback
Copy link
Owner

matthiasnoback commented Apr 9, 2021 via email

@secit-pl
Copy link
Author

secit-pl commented Apr 9, 2021

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?

@matthiasnoback
Copy link
Owner

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
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants