-
Notifications
You must be signed in to change notification settings - Fork 24
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
Comments
I'm not sure if it's supported. What does your form type look like?
|
<?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,
]);
}
} |
Thanks, but where does ***@***.*** come from?
|
I have the different console command to add contacts. This one is used to edit the contacts persisted in 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
// ...
} |
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.
|
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:
What do you think? |
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. |
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. |
I have the following code, it's used to edit the contact entity:
The input form in console looks as follows:
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?
The text was updated successfully, but these errors were encountered: