Search code examples
symfonyoopplaceholderformbuilder

How can I use a placeholder in my Formbuilder (Symfony 4)?


I want to use a placeholder for my Password field (in the case it is not empty):

$formBuilder->add('plainPassword', PasswordType::class, array('attr' => array('class' => 'form-control'), 'label' => 'Password','placeholder' => '...',));

But I get an error message:

The option "placeholder" does not exist. Defined options are: "action", "allow_extra_fields", "always_empty", "attr", "auto_initialize", "block_name", "by_reference", "compound", "constraints", "csrf_field_name", "csrf_message", "csrf_protection", "csrf_token_id", "csrf_token_manager", "data", "data_class", "disabled", "empty_data", "error_bubbling", "error_mapping", "extra_fields_message", "help", "inherit_data", "invalid_message", "invalid_message_parameters", "label", "label_attr", "label_format", "mapped", "method", "post_max_size_message", "property_path", "required", "translation_domain", "trim", "upload_max_size_message", "validation_groups".

How can I create a placeholder?


Solution

  • You have to put your placeholder option to the attr array like this:

    $formBuilder
        ->add('plainPassword', PasswordType::class, array(
            'attr' => array(
                'class' => 'form-control',
                'placeholder' => '...',
            ),
            'label' => 'Password',
        ));