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) }}
Related
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
I have a form in which I want to have checkboxes to add or remove elements from a collection: a User which have Responsability[].
I want to show some of the existing Responsability in the form but not all of them. I use an attribute called automatic to determine if I want to display them or not.
How can I edit my form to do such a thing?
UserType.php:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('username', TextType::class, [
'label' => 'Nom d\'utilisateurice'
])
->add('responsibilities', EntityType::class, [
// looks for choices from this entity
'class' => Responsibility::class,
// uses the Responsibility.label property as the visible option string
'choice_label' => 'label',
'label' => 'RĂ´les',
'multiple' => true,
'expanded' => true,
'choice_attr' => function($responsibility)
{
return [
'data-responsibility-description' => $responsibility->getDescription(),
];
},
])
->add('submit',SubmitType::class, [
'label' => 'Changer les informations',
'attr' => [
'class' => 'btn btn-outline-primary float-right'
]
]);
}
edit.html.twig:
{{ form_start(edit_form, {'attr': {'id': 'form-edit-user'}}) }}
<div class="form-group">
{{ form_label(edit_form.username) }}
{{ form_widget(edit_form.username) }}
</div>
<div class="form-group">
{{ form_label(edit_form.responsibilities) }}
{% for responsibility in edit_form.responsibilities %}
<div class="form-group">
{{ form_widget(responsibility) }}
{{ form_label(responsibility) }}
<span class="text-muted responsibility-description">
{{ responsibility.vars.attr['data-responsibility-description'] }}
</span>
</div>
{% endfor %}
</div>
{{ form_widget(edit_form) }}
{{ form_end(edit_form) }}
You can use query_builder form option as documented here.
Something like this should work:
'query_builder' => function (EntityRepository $repository) {
return $repository
->createQueryBuilder('o')
->where('o.automatic = FALSE');
}
Or like this if you prefer having a parameter:
'query_builder' => function (EntityRepository $repository) {
return $repository
->createQueryBuilder('o')
->where('o.automatic = :automatic')
->setParameter('automatic', false);
}
I am using Symfony 3 to create a web-application. I am using FOSUserBundle. I want user to register theirself, so I wrote a custom template and RegistrationFormType. My problem is, that the fiel "email" is not filled correctly in the controller. Every submit I get following error: https://ibb.co/cYkp8y
I am using the default controller from FOSUserBundle.
My template:
{{ form_start(form, {'method': 'post', 'action': path('fos_user_registration_register'), 'attr': {'class': 'form-group'}}) }}
<fieldset>
<div class="form-group">
{{ form_widget(form.forename, {'attr': {'class': 'form-control col-md-7 col-xs-12', 'placeholder': 'Vorname'}}) }}
</div>
<div class="form-group">
{{ form_widget(form.surname, {'attr': {'class': 'form-control col-md-7 col-xs-12', 'placeholder': 'Nachname'}}) }}
</div>
<div class="form-group">
{{ form_widget(form.email, {'attr': {'class': 'form-control col-md-7 col-xs-12', 'placeholder': 'Email'}}) }}
</div>
<div class="form-group">
{{ form_widget(form.club, {'attr': {'class': 'form-control col-md-7 col-xs-12'}}) }}
</div>
<div class="form-group">
{{ form_widget(form.plainPassword.first, {'attr': {'class': 'form-control col-md-7 col-xs-12', 'placeholder': 'Passwort'}}) }}
</div>
<div class="form-group">
{{ form_widget(form.plainPassword.second, {'attr': {'class': 'form-control col-md-7 col-xs-12', 'placeholder': 'Passwort wiederholen'}}) }}
</div>
<input class="btn btn-lg btn-success btn-block" type="submit" value="Anmelden">
</fieldset>
{% do form.username.setRendered %}
{{ form_end(form) }}
My RegistrationType:
class RegistrationType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('email', EmailType::class)
->add('plainPassword', LegacyFormHelper::getType('Symfony\Component\Form\Extension\Core\Type\RepeatedType'), array(
'type' => LegacyFormHelper::getType('Symfony\Component\Form\Extension\Core\Type\PasswordType'),
'options' => array('translation_domain' => 'FOSUserBundle'),
'first_options' => array('label' => 'form.password'),
'second_options' => array('label' => 'form.password_confirmation'),
'invalid_message' => 'fos_user.password.mismatch',
))
->add('forename', TextType::class, array(
'label' => 'Vorname',
))
->add('surname', TextType::class, array(
'label' => 'Nachname'
))
->add('club', EntityType::class, array(
'class' => 'AppBundle\Entity\Club',
'multiple' => false,
'expanded' => false,
'label' => 'Vereinszugehörigkeit',
'required' => false,
))
;
}
public function getParent()
{
return 'FOS\UserBundle\Form\Type\RegistrationFormType';
}
public function getBlockPrefix()
{
return 'app_user_registration';
}
}
Does anybody know, why the controller can't handle the email?!
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) }}
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