Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8170237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:05:36+00:00 2026-06-06T21:05:36+00:00

My form as field users (entity type). How can i add validation in order

  • 0

My form as field users (entity type). How can i add validation in order to specificity that at least one user should be selected? Actually i’m adding an event listener but i don’t know if this is a legit solution or not:

public function buildForm(\Symfony\Component\Form\FormBuilder $builder,
    array $options)
{
    $builder
        ->add('title', 'text', array(
            'label' => 'Titolo'
        ))
        ->add('content',  'textarea', array(
            'label' => 'Contenuto'
        ))
        ->add('sender_text',  'text', array(
            'label' => 'Mittente testuale',
        ))
        ->add('users', 'entity', array(
            'label'    => 'Destinatari',
            'class'    => 'DL\FidelityBundle\Entity\User',
            'property' => 'select_label',
            'multiple' => true
        ));
    ;

    // Valida il numero di utenti selezionati
    $builder->addEventListener(\Symfony\Component\Form\FormEvents::POST_BIND,
        function($event) {
            $form = $event->getForm();
            $data = $event->getData();

            if(!$data->users->isEmpty()) return;

            $msg = 'Occorre specificare almeno un utente destinatario';
            $form->get('users')->addError(new FormError($msg));
    });
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T21:05:36+00:00Added an answer on June 6, 2026 at 9:05 pm

    Have a look at the validation component: http://symfony.com/doc/current/book/validation.html

    You can write a callback constraint within the object you want to validate:

    use Symfony\Component\Validator\ExecutionContext;
    
    public function isUsersValid(ExecutionContext $context)
    {
        if ($this->users->isEmpty()) {
            $propertyPath = $context->getPropertyPath() . '.users';
            $context->setPropertyPath($propertyPath);
            $context->addViolation('Occorre specificare almeno un utente destinatario', array(), null);
        }
    }
    

    See the callback constraint page how to add this constraint to your entity (this depens if you are using annotations or yaml/xml).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to build a form where users can add a text field
I'd like to generate a select form field, where a user can select one
I have a form where users can add input fields with jQuery. <input type="text"
I have a dynamic form that users can add/delete sets of input fields. I
I build a form with entity type like this: $form = $this->createFormBuilder() ->add('users', 'entity',
We can easily custom the powerful Symfony2 form component with an entity Field Type
I have form where user submits field. Field can have letters, numbers, and punctuation.
I am trying to add a custom field to the user form in the
I have a User entity that has a Current Location field (city and country).
I have a simple db.Model , that has one of the fields db.ListProperty(users.User) For

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.