fperet
fperet

Reputation: 456

Form default values is not shown

The problem as it is. I'm trying to set defalut form values, and there's no way i can achieve this.

Page layout

{% extends bundle ~ "Bundle::reg.html.twig" %}
{% form_theme regForm   'AcmeBundle:Form:order_type_fields.html.twig' %}
{% block content %}
<form action="{{ path('reg') }}" {{ form_enctype(regForm) }} method="POST">
    <div>
     {{ form_widget( regForm.firstname )}}
    </div>
    {{ form_rest( regForm )}}
    <input type="submit>
</form>
{% endblock %}

fields layout

{% block text_widget %}
{% spaceless %}
    <label>{{label}}</label>
    <input type="text" {{ block('widget_attributes') }} value="{{ value }}">
{% endspaceless %}
{% endblock text_widget %}

Controller

$user      = new User();
$regForm   = $this->get('form.factory')->create(new RegForm(), $user);

return $this->render("FrontendBundle:Order:type.html.twig", array(
       'regForm'   => $regForm->createView(),
);

Form

class RegForm extends AbstractType
{
    public function buildForm( FormBuilder $builder, array $options ){
      $builder->add( 'firstname',     'text',  array( 'label' => ' ', 'data' => 'Enter your name here' ) );
    }       

    public function getName()
    {
        return 'reg';
    }
}

I've already tried to set default field data in many ways:

in my controller by defining firstname field (

$user->setFirstname('Enter your name here');

).

In form builder by using setData method (

$builder->add( 'firstname',     'text',  array( 'label' => ' ' ) )->setData(array('firstname' => 'Enter your name here'));

Or just

$builder->setData(array('firstname' => 'Enter your name here'));

All other form stuff work's just fine. For example, i can set label 'FIRSTNAME' without any problems. So, what am i missing?

UPD: After first answer i decided to ask a direct question: Why doesn't work default value for this, while label works just fine:

$builder->add('firstname', 'text', array('label' => 'name', 'data' => 'Andrew'));

?

Maybe some mistakes in twig templates?

Upvotes: 3

Views: 2890

Answers (2)

amsross
amsross

Reputation: 453

Have you tried this in your Form Type:

$builder
    ->add('firstname','hidden', array(
        'attr' => array(
            'value' => 'Enter your name here',
        ),
    ))

It seems to me, though, that you want to use a placeholder (HTML5) instead:

$builder
    ->add('firstname','hidden', array(
        'attr' => array(
            'placeholder' => 'Enter your name here',
        ),
    ))

Upvotes: 3

solarc
solarc

Reputation: 5738

In your controller, set the data on the $user object before creating the form

$user      = new User();
$user->setFirstname('Default name');
$regForm   = $this->get('form.factory')->create(new RegForm(), $user);

BTW you might want to use the placeholder attribute on the input:

<input placeholder="Enter your name here">

You can do this by adding the 'attr' option on the form builder:

$builder->add('firstname', 'text', array(
    'attr' => array('placeholder' => 'Enter your name here'),
));

Upvotes: 1

Related Questions