Twig shows extra fields - php

I have a simple form which looks like this
.......
$builder
->add('name', 'text')
->add('email', 'text'
->add('save', 'submit')
->getForm();
What I want is to render only the name field
{{ form_start(form, {'attr': {'role': 'form', 'novalidate' : 'novalidate'} }) }}
{{ form_label(form.name) }}
{{ form_widget(form.name, { 'attr': {'class': 'form-control'} }) }}
{{ form_errors(name.name, { 'attr': {'class': 'form-control'} }) }}
{{ form_end(form)
As result I get the page rendered with both name and email fields. What I'm doing wrong and how to prevent rendering of email field?

In according with the doc, if you don't want to render unrendered fields, you can use:
{{ form_end(form, {'render_rest': false}) }}
Hope this help

If your field is not required, you can do :
{{ form_label(form.email, null, {'label_attr': {'class':'hidden'}}) }}
{{ form_widget(form.email, { 'attr': {'class': 'form-control hidden'} }) }}
{{ form_errors(name.email, { 'attr': {'class': 'form-control hidden'} }) }}
Hope this helps.

Related

Get default datetime form on Symfony 2 twig

i have some probleme, i would like to have a default value in my datetime twig, i dont want to have the defaut value from BuildForm cause i use it for other twigs.
My add twig
<div class="form-group">
{{ form_label(form.Url, "Le URL", {'label_attr': {'class': 'col-sm-4 textTab control-label'}}) }}
{{ form_errors(form.Url) }}
<div class="col-sm-6">
{{ form_widget(form.Url, {'attr': {'class': 'form-control'}}) }}
</div>
</div>
<div class="form-group">
{{ form_label(form.dateDeLaDemande, "Date de la demande du crawl", {'label_attr': {'class': 'col-sm-4 textTab control-label'}}) }}
{{ form_errors(form.dateDeLaDemande) }}
<div class="col-sm-6" style="margin-top: 8px;">
{{ form_widget(form.dateDeLaDemande, {'attr': {'class': 'col-sm-6'}}) }}
</div>
</div>
<div class="form-group">
{{ form_label(form.DateDuCrawl, "Date du crawl", {'label_attr': {'class': 'col-sm-4 textTab control-label'}}) }}
{{ form_errors(form.DateDuCrawl) }}
<div class="col-sm-6" style="margin-top: 8px;">
{{ form_widget(form.DateDuCrawl, {'attr': {'class': 'col-sm-6' }}) }}
</div>
</div>
And my buildFom
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('site','text')
->add('Url','url')
->add('dateDeLaDemande','date')
->add('DateDuCrawl','date')
->add('DateNextCrawl','date')
->add('faitVP', 'checkbox', array('required' => false))
->add('integrerMavec','checkbox', array('required' => false))
// ->add('historique','textarea')
->add('historiques', 'collection', array(
'type' => new CategoryType(),
'allow_add' => true,
'allow_delete' => true
))
->add('save','submit')
;
}
Can some one help me plz ? thanks and sorry about my english :)
Try something like this
{{ form_widget(form.form.dateDeLaDemande, {value : currentDate}) }}
To declare a variable in twig :
{% set currentDate = "now"|date("m/d/Y") %}
or directely :
{{ form_widget(form.form.dateDeLaDemande, {value : "now"|date("m/d/Y")}) }}

Issue with resizing fields of form under symfony 3

I have a problem with resizing the fields I've generated with symfony, my fields take the whole width of the page. Here is my code:
Controller :
$user= new User();
$form=$this->CreateFormBuilder($user)
->add("firstName",TextType::class)
->add("lastName",TextType::class)
->add("login",TextType::class)
->add("password",PasswordType::class)
->add("tel",TextType::class)
->add("email",TextType::class)
->add("address",TextType::class)
->add("send",SubmitType::class)
-> getForm();
$form->handleRequest($req);
if($form->isValid())
{
$em=$this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
}
return $this->render('eplateformeBundle:Default:register.html.twig',['form'=>$form->createView()]);
The view :
<div class="well">
{{ form_start(form ) }}
{{ form_errors(form) }}
{{ form_row(form.firstName) }}
{{ form_row(form.lastName) }}
{{ form_row(form.login) }}
{{ form_row(form.password) }}
{{ form_row(form.tel) }}
{{ form_row(form.email) }}
{{ form_row(form.address) }}
{{ form_widget(form.send, {'attr': {'class': 'btn btn-primary'}}) }}
{{ form_rest(form) }}
{{ form_end(form) }}
</div>
You can transfer own class to inputs fields like this:
<div class="well">
{{ form_start(form ) }}
{{ form_errors(form) }}
{{ form_row(form.firstName,{'attr':{'class':'your own class''}}) }}
{{ form_row(form.lastName,{'attr':{'class':'your own class''}}) }}
{{ form_row(form.login,{'attr':{'class':'your own class''}}) }}
{{ form_row(form.password,{'attr':{'class':'your own class''}}) }}
{{ form_row(form.tel,{'attr':{'class':'your own class''}}) }}
{{ form_row(form.email,{'attr':{'class':'your own class''}}) }}
{{ form_row(form.address,{'attr':{'class':'your own class''}}) }}
{{ form_widget(form.send, {'attr': {'class': 'btn btn-primary'}}) }}
{{ form_rest(form) }}
{{ form_end(form) }}
</div>

symfony change attribut 'for' form_label

I search in google but i don't find how to change( surcharging) the 'for' attribut in a form_label.
{{ form_widget(form.star,{'id':'rating-input-1-5','attr':{'class':'rating-input'}}) }}
{{ form_label(form.star,null,{'label_attr':{'class':'rating-star','for':'rating-input-1-5'}}) }}
{{ form_widget(form.star,{'id':'rating-input-1-4','attr':{'class':'rating-input'}}) }}
{{ form_label(form.star,null,{'label_attr':{'class':'rating-star','for':'rating-input-1-4'}}) }}
{{ form_widget(form.star,{'id':'rating-input-1-3','attr':{'class':'rating-input'}}) }}
{{ form_label(form.star,null,{'label_attr':{'class':'rating-star','for':'rating-input-1-3'}}) }}
{{ form_widget(form.star,{'id':'rating-input-1-2','attr':{'class':'rating-input'}}) }}
{{ form_label(form.star,null,{'label_attr':{'class':'rating-star','for':'rating-input-1-2'}}) }}
{{ form_widget(form.star,{'id':'rating-input-1-1','attr':{'class':'rating-input'}}) }}
{{ form_label(form.star,null,{'label_attr':{'class':'rating-star','for':'rating-input-1-1'}}) }}
I want that the form_label point on the id of my form_widget.
Is it possible ?
Thanks for your answer
Try to change form_label call to
{{ form_label(form.star,null,{'id':'rating-input-1-5', 'label_attr':{'class':'rating-star'}}) }}

How to customize FOSUserBundle forms

I have below problem with FOSUserBundle customization.
I was overwritten FOSUser register form, then I changed register_content from
{{ form_widget(form) }}
to:
<div class="col-md-6">
{{ form_label(form.firstname) }}
{{ form_errors(form.firstname) }}
{{ form_widget(form.firstname) }}
{{ form_label(form.lastname) }}
{{ form_errors(form.lastname) }}
{{ form_widget(form.lastname) }}
</div>
<div class="col-md-6">
{{ form_label(form.email) }}
{{ form_errors(form.email) }}
{{ form_widget(form.email) }}
{{ form_label(form.plainPassword) }}
{{ form_errors(form.plainPassword) }}
{{ form_widget(form.plainPassword) }}
{{ form_label(form.plainPassword.second) }}
{{ form_errors(form.plainPassword.second) }}
{{ form_widget(form.plainPassword.second) }}
</div>
But, when I was went to the browser, I got it:
bad form render
The form renders incorrectly. How I can repair this?
try with this
{{ form_label(form.plainPassword.first) }}
{{ form_errors(form.plainPassword.first) }}
{{ form_widget(form.plainPassword.first) }}
{{ form_label(form.plainPassword.second) }}
{{ form_errors(form.plainPassword.second) }}
{{ form_widget(form.plainPassword.second) }}

Symfony change style of 'repeated' field

I have a form with a repeated field:
$builder->add('password', 'repeated', array( 'type' => 'password' ));
I want this repeated field to render differently from the other fields - how do I do that? I'm new to Symfony and twig, so if you have suggestions with code, please add some information as to where to put the code.
My form.html.twig looks like this:
{{ form_widget(form) }}
Thanks in advance.
This is how i display my repeated field using twitter bootstrap, of course you can change those classes to the one you are using
<form action="{{ path('passwordReset') }}" method="post" role="form">
{{ form_errors(form) }}
<div class="login-screen">
<h4>Reset Your Password</h4>
<div class="login-form">
<div class="form-group">
{{ form_widget(form.password.first, { 'attr': {'class': 'form-control', 'placeholder': 'Enter your password', 'value':''} }) }}
{% if(form_errors(form.password.first)) %}
<div class="alert alert-danger">{{ form_errors(form.password.first) }}</div>
{% endif %}
<label class="login-field-icon fui-lock" for="login-password"></label>
</div>
<div class="form-group">
{{ form_widget(form.password.second, { 'attr': {'class': 'form-control', 'placeholder': 'Confirm your password', 'value':''} }) }}
{% if(form_errors(form.password.second)) %}
<div class="alert alert-danger">{{ form_errors(form.password.second) }}</div>
{% endif %}
<label class="login-field-icon fui-lock" for="login-name"></label>
</div>
<button class="btn btn-primary btn-lg btn-block" type="submit">Submit</button>
<a class="login-link" href="{{ path('login') }}">Sign in</a>
</div>
</div>
{{ form_rest(form) }}
</form>
What you need are the following two
{{ form_widget(form.password.first, { 'attr': {'class': 'form-control', 'placeholder': 'Enter your password', 'value':''} }) }}
{{ form_widget(form.password.second, { 'attr': {'class': 'form-control', 'placeholder': 'Confirm your password', 'value':''} }) }}
Just assign them the class you want to assign them to make them look different.
Helo
'first_options' => array('label' => 'form.password','attr' => array('class' => 'mystyle'))
something like that in the formType, it add a class to your input element and let you customize.

Categories