Form default values is not shown - forms

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?

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',
),
))

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'),
));

Related

Display errors message form

i am trying to show errors messages at the top on page in my twig view but {{ Form_errors(form)}} don't display password errors! but shows other errors..
Form builder
->add('password', RepeatedType::class, array(
'type' => PasswordType::class,
'options' => array('attr' => array('class' => 'password-field')),
'required' => true,
'first_options' => array('label' => 'Password'),
'second_options' => array('label' => 'Repeat Password').
view :
{% extends 'base.html.twig' %}
{% block body %}
{{ form_start(form) }}
{{ form_widget(form) }}`enter code here`
{{dump(form_errors(form))}}
<input type="submit" value="Create" />
{{ form_end(form) }}
try
{{dump(form_errors(form.password))}}
to display errors specific to the password field

add html class value with existing class in symfony2 form?

I have defined some filed in my FormType and each form element has class value like below
$builder->add('name', 'text', array(
'label' => 'Name',
'attr' => array('class' => 'form-control'),
));
in twig file wrote like this
{{ form_widget(form.name) }}
This code generate output like this
<input type='text' name='name' class='form-control' />
now I want to add extra class in this element like error class which generate following output
<input type='text' name='name' class='form-control error' />
For generating above output I wrote like code in Twig file-
{{ form_widget(form.name,{attr:('class'=>'error')}) }}
This code generate following output--
<input type='text' name='name' class='error' />
So I am confused, what should to get my expected result. Add extra class value to my form element in html.
My Expected output is
<input type='text' name='name' class='form-control error' />
You can add multiple classes inside the class attribute.
Like this:
$builder->add('name', 'text', array(
'label' => 'Name',
'attr' => array('class' => 'form-control error'),
));
Going by your comment on #Moshikaro's answer, you're probably going to have to use flow control to build up the class string, either in Twig or the Controller:
Twig
{% set errorClass = '' %}
{% if errorTest %}
{% set extraClass = ' error' %}
{% endif %}
{{ form_widget(form.name,{attr:('class'=>'form-control'~errorClass)}) }}
or maybe
{% set errorClass = '' %}
{% if errorTest %}
{% extraClass = ' error' %}
{% endif %}
{{ form_widget(form.name,{attr:('class'=>form.config.attributes.class~errorClass)}) }}
Controller
$errorClass = "";
if ($test)
{
$errorClass += " error";
}
$cssClasses = "form-control" + $errorClass;
$builder->add('name', 'text', array(
'label' => 'Name',
'attr' => array('class' => $cssClasses),
));
You get the idea, anyway. I've not been able to test any of that.

Add classes and attributes to all multiple selects in Symfony2?

I want to customize all multiple selects of my application like that :
<select class="selectpicker show-tick" data-size="auto">
...
</select>
How should I do that?
EDIT for ncrocfer
This is my build form method:
This is not complete, lack some stuff...
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title')
->add('misc')
->add('url')
->add('attachment', 'file')
->add('time_estimated')
->add('started_at')
->add('finished_at')
->add('default')
->add('deadline')
->add('priority', 'entity', array(
'class' => 'LanCrmBundle:TaskPriority',
'property' => 'title',
'multiple' => false
))
->add('project', 'entity', array(
'class' => 'LanCrmBundle:Project',
'property' => 'title',
'multiple' => false
))
->add('category', 'entity', array(
'class' => 'LanCrmBundle:TaskCategory',
'property' => 'title',
'multiple' => false
))
->add('user', 'entity', array(
'class' => 'LanSecurityBundle:User',
'property' => 'username',
'multiple' => false
))
->add('products', 'entity', array(
'class' => 'LanCrmBundle:Product',
'property' => 'title',
'multiple' => true
))
;
}
Use the attr attribute in your form buider :
<?php
namespace Foo\BarBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class FooType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('foo', 'choice', array(
'choices' => array(0 => 'Option 1', 1 => 'Option 2'),
'attr' => array('class' => 'selectpicker show-tick', 'data-size' => 'auto'),
))
;
}
}
Edit
If you have a lot of forms like you said in your comment, you can use the form customization.
Create a new template file :
{# src/Foo/BarBundle/Resources/views/Form/fields.html.twig #}
{% block choice_widget_collapsed %}
{% spaceless %}
<select {{ block('widget_attributes') }}{% if multiple %} multiple="multiple"{% endif %} class="selectpicker show-tick" data-size="auto">
{% if empty_value is not none %}
<option value="">{{ empty_value|trans({}, translation_domain) }}</option>
{% endif %}
{% if preferred_choices|length > 0 %}
{% set options = preferred_choices %}
{{ block('choice_widget_options') }}
{% if choices|length > 0 and separator is not none %}
<option disabled="disabled">{{ separator }}</option>
{% endif %}
{% endif %}
{% set options = choices %}
{{ block('choice_widget_options') }}
</select>
{% endspaceless %}
{% endblock choice_widget_collapsed %}
Then import your template inside your application configuration :
# app/config/config.yml
twig:
form:
resources:
- 'FooBarBundle:Form:fields.html.twig'
The change will be effective on all your select.
Use attr attribute in FormBuilder:
'attr' => array('class'=>'selectpicker show-tick', 'data-size'=>'auto')

I can not send a variable to a twig template

I dont know why not can show a variable, I read the manual and I searched in many websites, but I have no idea what I'm doing wrong...
I have a classType (there is not from a entity), the class form have 3 combobox and a textbox, when I submit the form, another template (show2.html.twig) render the new form where I want show the textbox value... I speend many time and a have 0 result ;)
When I see the Symfony profiler, I can see the values of the form in the "Request POST Parameters" section, but I'cant catch it and show it in the twig template...
that is the code wath dont work for my
$this->get('request')->request->get('campo', 'can not show it ¬¬')
in the template dont show the "campo" value, just "can not show it ¬¬"
this is my classType
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('Pais', 'entity', array(
'class' => 'UnadeniZonaBundle:Pais',
'property' => 'paisnomb',
))
->add('Provincia', 'entity', array(
'class' => 'UnadeniZonaBundle:Provincia',
'property' => 'provnomb',
))
->add('Ciudad', 'entity', array(
'class' => 'UnadeniZonaBundle:Ciudad',
'property' => 'ciudnomb',
))
->add('Campo', 'text', array(
'label' => 'campo',));
}
public function getName() {
return 'ciudad2';
}
}
My controller...
public function newAction(Request $request) {
$form = $this->createForm(new Ciudad2Type());
return $this->render('UnadeniZonaBundle:Ciudad2:new.html.twig', array(
'form' => $form->createView(),
'mensaje' => 'test'
));
}
public function showAction(Request $request) {
if ($request->isMethod('POST')) {
return $this->render('UnadeniZonaBundle:Ciudad2:show2.html.twig', array(
'mensaje' => $this->get('request')->request->get('campo', 'can not show it ¬¬')
));
}
}
this the templates
(new.html.twig)
{% extends '::base.html.twig' %}
{% block content -%}
<form action="{{ path('ciudad2_show') }}" method="post" {{ form_enctype(form) }}>
{{ form_widget(form) }}
<input type="submit" />
{{ mensaje }}
</form>
{% endblock %}
(Show2.html.twig)
{% extends '::base.html.twig' %}
{% block content -%}
{{ mensaje }}
{% endblock %}
If you try this :
$postData = $request->request->get('ciudad2');
$targetValue = $postData['Campo'];
return $this->render('UnadeniZonaBundle:Ciudad2:show2.html.twig', array(
'mensaje' => $targetValue)
));
It's a typo issue. Change campo to Campo in your controller.
if ($this->getRequest()->isMethod('POST')) {
$request = $this->getRequest->request;
$campo = $request->get('Campo', 'cannot show it.', true);
return $this->render('UnadeniZonaBundle:Ciudad2:show2.html.twig', array(
'mensaje' => $campo
));
}

Symfony2 Update Specific Field in Form Collection

I have an interesting problem. I couldn't find any solution in stackoverflow and also google. I have an Entity User and User have some metas. So I created a UserMeta Entity and also UserMetaValue. In user form there is lots of tabs. And I used these metas in the tabs. Some of them in first tabs, some of them in other tabs. And all tabs have their own form. When I bind a form on active tab, active tab metas update, others changed to NULL.
StudentPersonalType.php
namespace ATL\UserBundle\Form\Type;
use ATL\CommonBundle\Utils\Shortcut;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormBuilderInterface;
class StudentPersonalType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options){
$builder->add("first_name", null, array(
"label" => "İsim",
"required" => true,
"attr" => array(
"class" => "span10"
)
))->add("last_name", null, array(
"label" => "Soyisim",
"required" => true,
"attr" => array(
"class" => "span10"
)
))->add("username", null, array(
"label" => "Öğrenci Numarası",
"required" => true,
"attr" => array(
"class" => "span10"
)
))->add("email", null, array(
"label" => "Email",
"required" => true,
"attr" => array(
"class" => "span10"
)
))->add('metas', 'collection', array(
'label' => "Metas",
'type' => new UserMetaType()
));
}
public function getName(){
return "personal";
}
public function setDefaultOptions(OptionsResolverInterface $resolver){
$resolver->setDefaults(array(
"data_class" => "ATL\UserBundle\Entity\User"
));
}
}
StudentEducationType.php
namespace ATL\UserBundle\Form\Type;
use ATL\CommonBundle\Utils\Shortcut;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormBuilderInterface;
class StudentEducationType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options){
$builder->add('metas', 'collection', array(
'label' => "Metas",
'type' => new UserMetaType(),
'by_reference' => false
));
}
public function getName(){
return "education";
}
public function setDefaultOptions(OptionsResolverInterface $resolver){
$resolver->setDefaults(array(
"data_class" => "ATL\UserBundle\Entity\User"
));
}
}
And twig
<div id="personal-info" class="tab-pane row-fluid active">
<form style="margin:20px 0 0 0;" class="ajaxForm form-horizontal form-row-seperated" action="{{ formAction }}">
{{ form_row(form.first_name) }}
{{ form_row(form.last_name) }}
{{ form_row(form.email) }}
{{ form_row(form.username) }}
{% for meta in form.metas %}
{% if meta.value.vars.label in formValues.personal %}
{{ form_widget(meta) }}
{% endif %}
{% endfor %}
{{ form_row(form._token) }}
<div class="form-actions" style="margin-bottom:0;">
<button class="btn blue" type="submit"><i class="icon-ok"></i> Kaydet</button>
</div>
</form>
</div>
<div id="education-info" class="tab-pane row-fluid">
<form style="margin:20px 0 0 0;" class="ajaxForm form-horizontal form-row-seperated" action="{{ formAction }}">
{% for meta in educationForm.metas %}
{% if meta.value.vars.label in formValues.education %}
{{ form_widget(meta) }}
{% endif %}
{% endfor %}
{{ form_row(educationForm._token) }}
<div class="form-actions" style="margin-bottom:0;">
<button class="btn blue" type="submit"><i class="icon-ok"></i> Kaydet</button>
</div>
</form>
</div>
I filter collection fields by check it in twig file with IF statement.
I ask my question again, How could I use metas in different form in same page without affecting the others?
You have done half the job of filtering out the irrelevant fields for rendering. But you also need to filter out the irrelevant fields for form binding.
When you bind the request to the form it is expecting values for every UserMeta entity because there is a field for all of them in the UserMetaType collection. You'll need to remove all the UserMetaType forms which don't correspond to one of your submitted values. It's probably best to this with a FormEvents::PRE_BIND listener.
You can see a simpler example of this at Form: Avoid setting null to non submitted field. It will be slightly more complex for you, because you'll have to iterate through the collection of UserMetaType forms and remove the ones which you don't want bound.