8000 [Twig, Form Types] Form text field placeholder can not be null. Why? · Issue #37573 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Twig, Form Types] Form text field placeholder can not be null. Why? #37573

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

Closed
dawsza opened this issue Jul 14, 2020 · 5 comments
Closed

[Twig, Form Types] Form text field placeholder can not be null. Why? #37573

dawsza opened this issue Jul 14, 2020 · 5 comments

Comments

@dawsza
Copy link
dawsza commented Jul 14, 2020

Symfony version(s) affected: 5.1.2

Description
Form input placeholder can be empty string or false but can not be null. Why?
Another attributes like for example class can by null.

Error: Argument 1 passed to Symfony\Bridge\Twig\Extension\TranslationExtension::trans() must be of the type string, null given

How to reproduce

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', null, ['attr' => [
                'placeholder'   => null,
            ]])
        ;
    }
@xabbuh
Copy link
Member
xabbuh commented Jul 14, 2020

Can you explain why you would want to set the placeholder attribute to null instead of not setting it at all?

@dawsza
Copy link
Author
dawsza commented Jul 14, 2020

Because placeholder comes from database and sometimes can be null value.
Of course I can casting null to string, but why it's works with false (boolean) or works with null for another attributess (class for example)?

@apfelbox
Copy link
Contributor

@xabbuh I had the same issue some time ago.

If you have an optional placeholder, this one is way easier:

$form
    // 10 Form Fields
    ->add("someField", null, [
        // ...
        "attr" => [
            "placeholder" => $data->getPlaceholder(),
        ],
    ])
    // 4 more form fields

instead of

$someFieldAttributes = [
    // ...
];

if (null !== $data->getPlaceholder())
{
    $someFieldAttributes["attr"]["placeholder"] = $data->getPlaceholder();
}

$form
    // 10 Form Fields
    ->add("someField", null, $someFieldAttributes)
    // 4 more form fields
  1. now the options for this one field are somewhere else of all the other, also
  2. when making $data->getPlaceholder() nullable later on will lead to some dozen line changes in the form class.

@xabbuh
Copy link
Member
xabbuh commented Oct 16, 2020

Please have a look at #38595. Does it work for you?

@fabpot fabpot closed this as completed Oct 21, 2020
fabpot added a commit that referenced this issue Oct 21, 2020
…(xabbuh)

This PR was merged into the 3.4 branch.

Discussion
----------

[TwigBridge] do not translate null placeholders or titles

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #37573
| License       | MIT
| Doc PR        |

Commits
-------

2ee24a0 do not translate null placeholders or titles
@apfelbox
Copy link
Contributor

Looks great, thanks @xabbuh ! 🎉

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

No branches or pull requests

5 participants
0