email collection (from book), submit buttons collection - symfony

I have a problem with collection from symfony documentation. Here is my code:
controller:
$emails = array("first#qwe.com", "second#qwe.com", "third#qwe.com");
$myDForm = $this->createFormBuilder($emails)
->add('emails', 'collection', array('type' => 'email', 'options' => array('required' => false, 'attr' => array('class' => 'email-box')),))
->GetForm();
$myDForm->handleRequest($request);
twig:
{{ form_start(myDForm) }}
{{ form_end(myDForm) }}
As you can see the code looks easy, there are no errors just an empty page...
Could somebody point me where is the problem? I'm a beginner but it's almost exact as code in doc.
I'd like to make several submit buttons on a form this way but I stuck with this.
Thank you.

First:
Change
GetForm
to
getForm
$emails = array("first#qwe.com", "second#qwe.com", "third#qwe.com");
$myDForm = $this->createFormBuilder($emails)
->add(
'emails',
'collection',
array(
'type' => 'email',
'options' => array('required' => false, 'attr' => array('class' => 'email-box')),)
)->getForm();
$myDForm->handleRequest($request);
I don't know what is your point to use emails with collection.
Second:
You are just opening your form. You are not giving any field to print.
{{ form_start(myDForm) }}
{{ form_row(form.emails) }}
{{ form_end(myDForm) }}
http://symfony.com/doc/current/reference/forms/twig_reference.html

Related

form_errors does not display anything

I'm having a hard time displaying errors from forms validation. Nothing is displayed when I fill the form incorrectly.
Here is my controller :
public function Register(Request $request){
$user = new User();
// Accounts must be enabled manually
$authenticator = new GoogleAuthenticator();
$user->setEnabled(false);
$user->setAdmin(false);
$user->setSecret($authenticator->createSecret());
$form = $this->createFormBuilder($user)
->add('email', TextType::class)
->add('email', RepeatedType::class)
->add('password', PasswordType::class)
->add('password', RepeatedType::class)
->add('phone', TelType::class)
->add('secret', HiddenType::class)
->add('save', SubmitType::class, array('label' => 'Register'))
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// $form->getData() holds the submitted values
// but, the original `$task` variable has also been updated
$user = $form->getData();
// ... perform some action, such as saving the task to the database
// for example, if Task is a Doctrine entity, save it!
// $entityManager = $this->getDoctrine()->getManager();
// $entityManager->persist($task);
// $entityManager->flush();
return $this->redirectToRoute('register_success');
}
return $this->render('register.html.twig', array(
'form' => $form->createView()
));
}
And my template
{{ form_start(form) }}
{{ form_errors(form) }}
<div>
{{ form_errors(form.email) }}
{{ form_label(form.email) }} :
{{ form_widget(form.email.first, { 'attr' : {'placeholder' : 'Email address'}})}}
{{ form_widget(form.email.second, { 'attr' : {'placeholder' : 'Email address (again)'}})}}
</div>
<div>
{{ form_errors(form.password) }}
{{ form_label(form.password) }} :
{{ form_widget(form.password.first, { 'attr' : {'placeholder' : 'Password'}})}}
{{ form_widget(form.password.second, { 'attr' : {'placeholder' : 'Password (again)'}})}}
</div>
<div>
{{ form_errors(form.phone) }}
{{ form_label(form.phone) }} :
{{ form_widget(form.phone, { 'attr' : {'placeholder' : 'International format (+33)'}}) }}
</div>
<div>
{{ form_widget(form.save) }}
</div>
{{ form_end(form) }}
The forms reload with data pre-filled but no errors are displayed (empty line in source code only)
Waiting for your hints
Thanks
One need to setup validators (and therefore error messages) for them to be displayed.
Thanks for your help.
First of all, you do not need to "repeat" your already defined as RepeatedType fields, email and password.
Change to:
$form = $this->createFormBuilder($user)
->add('email', RepeatedType::class, array(
'type' => TextType::class,
'invalid_message' => 'The email fields must match.',
'options' => array('attr' => array('class' => 'email-field')),
'required' => true,
'first_options' => array('label' => 'Email'),
'second_options' => array('label' => 'Repeat Email'),
))
->add('password', RepeatedType::class, array(
'type' => PasswordType::class,
'invalid_message' => 'The password fields must match.',
'options' => array('attr' => array('class' => 'password-field')),
'required' => true,
'first_options' => array('label' => 'Password'),
'second_options' => array('label' => 'Repeat Password'),
));
To display the errors, use:
{{ form_errors(form.email.first)}}
{{ form_errors(form.email.second)}}
{{ form_errors(form.password.first)}}
{{ form_errors(form.password.second)}}
References
Symfony 4 RepeatedType field documentation

How to change the size of a "select box" form

A client doesn't want to scroll in a select form so I need to increase the size of my select form.
I tried this but still doesn't work, I only see 4 inputs :
{{ form_widget(filterForm.accomodationFilter1, { attr: { class: 'fancyselect filter type' }, {'size': '10'} }) }}
Any idea how I can do that and where is the documentation ?
Thanks
Try doing it directly in the formBuilder:
->add('example', 'choice', array(
'attr' => array('class' => 'fancyselect filter type',
'style' => 'max-width:100px'),
'choices' => 'choices',
)
Or just use:
->add('example', 'choice', array(
'attr' => array('class' => 'fancyselect filter type'),
'choices' => 'choices',
'max_length' => 100,
)

Umlauts not beeing displayed properly in symfony2

Im having the following problem.
If I try to use this code
$form = $this->createFormBuilder()
->add('code', 'integer', array(
'attr' => array('class' => 'login-input')
))
->add('einlösen', 'submit', array(
'attr' => array('class' => 'login-submit')
))
->getForm();
my browser doesnt show the ö in einlösen.
If i remove the class attr it works.
I thought this might be a css issue but the problem seems to be with symfony, since there
is no matter what class I use the ö always gets mangled.
Another thing I realized is that if I do a cache clear the ö is there but as soon as i press the button once and reload the page its not showing up properly again.
Try to use the label option:
$this->createFormBuilder()
->add('submit', 'submit', array(
'label' => 'einlösen',
'attr' => array('class' => 'login-submit')
))
As an alternative you can set/overwrite the label in twig:
{{ form_widget(form.submit, { 'label': 'einlösen' }) }}

Symfony Form, CSRF token missing

Like in the topic, i have problem with CSRF token missing. This is my form:
$builder
->add('email', 'email', array(
'label' => 'Adres e-mail'
))
->add('userFirstname', 'text', array(
'label' => 'Imię',
'required' => false
))
->add('userLastname', 'text', array(
'label' => 'Nazwisko',
'required' => false
))
->add('userBusiness', 'entity', array(
'label' => 'Firma',
'required' => false,
'class' => 'Cloud\CrmBundle\Entity\RelationContact',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('u')->where("u.type = 'b'");
},
'empty_value' => true
))
->add('old_password', 'password', array(
'label' => 'Stare hasło',
'mapped' => false,
'required' => false
))
->add('new_password', 'repeated', array(
'first_options' => array(
'label' => 'Nowe hasło'),
'second_options' => array(
'label' => 'Powtórz nowe hasło'),
'mapped' => false,
'required' => false,
'type' => 'password'
));
My view:
<div class="form-horizontal">
{{ form_row(form.email) }}
{{ form_row(form.userFirstname) }}
{{ form_row(form.userLastname) }}
{{ form_row(form.userBusiness) }}
{{ form_row(form.old_password) }}
{{ form_row(form.new_password) }}
</div>
</div>
What's wrong guys? Any ideas? :( I just don't understand this strange error... What could cause that ?
Probably you've to add this _token by hand because you're trying to display form manually:
{{ form_widget(form._token) }}
Symfony2 set a hidden field with the required informations. For this you have to include the hidden fields with:
{{ form_widget(form._token) }}
if you don't want the CSRF-Protection then you can disable the dunction in your parameters file.
Disable symfony 2 csrf token protection on ajax submit
If you use form_start and form_end symfony will add the token field to the form automatically
<div class="form-horizontal">
{{ form_start(form) }}
{{ form_row(form.email) }}
{{ form_row(form.userFirstname) }}
{{ form_row(form.userLastname) }}
{{ form_row(form.userBusiness) }}
{{ form_row(form.old_password) }}
{{ form_row(form.new_password) }}
{{ form_end(form) }}
</div>

set preferred choice in entity type form

I have the following entity type form element in symfony2:
->add('member_type', 'entity', array(
'class' => 'AdminBundle:CustomerTier',
'property' => 'tier',
'multiple' => true,
'expanded' => true,
'required' => true,
)
)
This gives checkboxes. In the edit view i want to checked the already selected choices when adding.
Please help me to solve this problem.
Do you mean checking a checkbox in the view (twig)?
{{ form_widget(form.checkme, { 'attr': {'checked': 'checked'} }) }}

Resources