How to access choice from form in Symfony - php

I have following form builder:
$builder->add('status', 'choice', array('attr' => array(
'choices' => array('0' => 'Principal', '1' => 'Teacher', '2' => 'Student'),
'required' => true, 'expanded' => 'false', 'multiple' => 'true'
)));
How to present those 3 values as radio buttons in twig template in a way like below presented(it doesn't work)?
{{ form_start(form) }}
{{ form_widget(form.status.choices[0]) }}
{{ form_end(form) }}

If you want radio buttons you need change options to 'expanded' => 'true', 'multiple' => 'false'
In twig:
{{ form_start(form) }}
{{ form_widget(form.status) }}
{{ form_end(form) }}
More information: choice Field Type

Related

Show form errors

I would like to show error messages in the top of my registration form. I created my registration form:
<div class="example-wrapper">
<h1>Register</h1>
{{ form_start(form) }}
{{ form_row(form.email) }}
{{ form_row(form.plainPassword.first) }}
{{ form_row(form.plainPassword.second) }}
{{ form_row(form.firstname) }}
{{ form_row(form.lastname) }}
{{ form_row(form.termsAccepted) }}
<button type="submit">Register!</button>
{{ form_end(form) }}
</div>
And on my UserType class, I added all necessaries input:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('email', EmailType::class)
->add('plainPassword', RepeatedType::class, array(
'type' => PasswordType::class,
'first_options' => array('label' => 'Password'),
'second_options' => array('label' => 'Repeat Password'),
'constraints' => [
new NotBlank([
'message' => 'Enter a valid password.'
]),
new Length([
'min' => 8,
'minMessage' => 'Password must be at least 8 characters.'
])
]
))
->add('firstname', TextType::class)
->add('lastname', TextType::class)
->add('termsAccepted', CheckboxType::class, array(
'mapped' => false,
'constraints' => new IsTrue(),
))
;
}}
Every time I get an error message, correctly displayed, I found it under the concerned input and not in the top of my registration form.
I added this on my form but don't help:
{{ form_errors(form) }}
Any suggestion?
Did you set error_bubbling => true?
Documentation: https://symfony.com/doc/current/reference/forms/types/text.html#error-bubbling

Symfony 2 multiple upload

using symfony 2.5 and Php 5.3.13.
i just want to add a field file type with option multiple => true.
It works ! but when the user click a second time on the button [Upload] it's deleting the first one already uploaded..
How can i do something like that : https://jsfiddle.net/gxfwvtqe/
In a symfony way, AdvertType.php :
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', 'text')
->add('content', 'textarea')
->add('category', 'choice', array(
'choices' => array(
'incident' => 'Incident',
'general' => 'General',
),
'multiple' => false,
'expanded' => true,
'required' => true,))
->add('documents','file', array(
'required' => false,
'data_class'=> null,
'multiple' => true))
->add('save', 'submit')
;
}
form.html.twig :
<div class="well">
{{ form_start(form, {'attr': {'class': ''}}) }}
{{ form_errors(form) }}
{{ form_row(form.documents) }}
{{ form_widget(form.save, {'attr': {'class': 'btn btn-primary'}}) }}
{{ form_rest(form) }}
{{ form_end(form) }}

Symfony Form filtered drop down choice

I have for with drop down entity Customer by field name, my question how to change form for filter by name. Example in my drop down many customer, and I write "a" and in drop down remained only who have first letter in name "a"
$builder
->add('customer', 'entity', array(
'class' => Customer::class,
'attr' => array('class' => 'form-control select2 all_customers'),
'property' => 'name',
'empty_value' => 'Choice Customer',
'query_builder' => function ($repository) {
/** #var CustomerRepository $repository */
return $repository->getAllQuery();
},
'required' => false
))
this my template what need to do which create text field for filter
{{ form_start(form) }}
{{ form_errors(form) }}
{{ form_label(form.customer, label|default(null), {'label_attr': {'class': 'control-label'}}) }}
{{ form_widget(form.customer, {'attr': {'class': 'select2', 'type': 'text'}}) }}
{{ form_end(form) }}

how to submit a nested form using ajax post value with different url

i have two form that is i used nested forms created in laravel.here i want to submit nested form only.How to submit a nested form and how to sent the this form post values using ajax with different URl's. That is each will be submit with separate url.I have form like
{{ Form::model($property, array('url' => array('sampleurl'), 'class' => 'form-inline', 'id' => 'mainForm')) }}
{{ Form::model($property, array('url' => array('sampleurltwo'), 'class' => 'form-inline', 'id' => 'enquiryForm')) }}
{{ Form::select('country', array('' => '--Select--','india' => 'india','US' => 'US','singapore' => 'singapore','malesia' => 'malesia'), '', array('class' => 'textarea_field', 'required' => 'required')) }}
{{ Form::text('mobile', null, array('placeholder' => 'mobile', 'class' => 'text_field', 'required' => 'required')) }}
{{ Form::Submit('Connect Now',array('class' => 'connect connectNowButton', 'id' => 'connect', 'onclick' => 'sendVal()'))}}
{{ Form::close() }}
{{ Form::close() }}
Here is the post values i want to submit.

Symfony2 custom radio_widget entity access

I have a form with 3 entity fields displaying radios input.
->add(
'membership',
'entity',
array(
'class' => 'Comiti\UserBundle\Entity\Membership',
'expanded' => true,
'multiple' => false,
'label' => false,
'empty_value' => 'Aucune adhésion',
'query_builder' => function (MembershipRepository $er) {
return $er->createQueryBuilder('membership')
->where('membership.club = :club')
->setParameter('club', $this->authentication_service->getCurrentClub())
->orderBy('membership.name', 'ASC')
;
},
)
)->add(
'federal_license',
'entity',
array(
'class' => 'Comiti\UserBundle\Entity\FederalLicense',
'expanded' => true,
'multiple' => false,
'label' => false,
'empty_value' => 'Aucune licence',
'query_builder' => function (FederalLicenseRepository $er) {
return $er->createQueryBuilder('federal_license')
->where('federal_license.club = :club')
->setParameter('club', $this->authentication_service->getCurrentClub())
->orderBy('federal_license.name', 'ASC')
;
}
)
)->add(
'insurance',
'entity',
array(
'class' => 'Comiti\UserBundle\Entity\Insurance',
'expanded' => true,
'multiple' => false,
'label' => false,
'empty_value' => 'Aucune assurance',
'query_builder' => function (InsuranceRepository $er) {
return $er->createQueryBuilder('insurance')
->where('insurance.club = :club')
->setParameter('club', $this->authentication_service->getCurrentClub())
->orderBy('insurance.name', 'ASC')
;
}
)
);
I need to define a custom template for those radios input that put in each input an attr whith "data-price".
i made this:
{%- block radio_widget -%}
<input type="radio" data-price="{{Myprivcevar}}" {{ block('widget_attributes') }}{% if value is defined %} value="{{ value }}"{% endif %}{% if checked %} checked="checked"{% endif %} />
Is there any possibility to access to {{MypriceVar }}
I am on Symfony 2.6
You can add attributes to an input without creating a custom template:
{{ form_widget(yourRow, {'attr': {'data-price':'yourValue'}}) }}
Example:
{{ form_widget(choiceFormView, {'attr': {'data-price':'2'}}) }}
result in
<input type="radio" id="form_choice_0" name="form[choice]" required="required" data-price="2" value="1" checked="checked">

Categories