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 8603605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:21:48+00:00 2026-06-12T02:21:48+00:00

With the text field type in Symfony there is a trim option . I’m

  • 0

With the text field type in Symfony there is a trim option. I’m pretty sure that the trim() operation is carried by the Form\Extension\Core\EventListener\TrimListener class. It’s a listener for the PRE_BIND event and calls:

$event->setData(trim($event->getData()));

I’d like to provide my own "normalize_spaces" option:

$builder->add('last_name', 'text', array(
    'label'           => 'Last name',
    'normlize_spaces' => true
));

How can i provide this option with my NormalizeSpacesListener?

class NormalizeSpacesListener implements EventSubscriberInterface
{
    public function preBind(FormEvent $event)
    {
        $data = $event->getData();

        if (is_string($data)) {
            $event->setData(preg_replace('/[ ]{2,}/', ' ', $data));
        }
    }

    public static function getSubscribedEvents()
    {
        return array(FormEvents::PRE_BIND => 'preBind');
    }
}
  • 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-12T02:21:49+00:00Added an answer on June 12, 2026 at 2:21 am

    I think you would probably override Symfony core FormType Class, especially the buildForm method:
    https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Extension/Core/Type/FormType.php

    and add your test for that option just like they do it for the trim option. Something like:

    Class MyFormType Extends FormType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
             parent::buildForm($builder, $options);
    
             if ($options['normalize_spaces']) {
                 $builder->addEventSubscriber(new NormalizeSpacesListener());
             }
         }
     }
    

    To override that class and have the system use it instead of the default core one, you can use the service container and change the class for the service form.type.form

    See how its declared here: https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Resources/config/form.xml

    And read this to understand how to actually tell the service container to use your own class instead of the default one:

    How to Override any Part of a Bundle

    Note: That’s how I would try to do it but I have not tested what I just wrote

    Another option would be to attach your listener to each form you build, and not make it a default option. I think that would work as well.

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

Sidebar

Related Questions

Using PHP; is there a way to check what type of form field was
I have a 1) text field type in my Sql server that I would
I understand that text_field creates an input field of type text , while email_field
How can I format a users text field input as they type and insert
I have some text stored in the body field of a custom content type
I'm writing a custom text field type for my product SKUs. If I have
Situation: I have user model. attribute meta_data in db represents text type field. In
I have a type ahead text field, and when the user hits Enter I
I am printing out the type field and text field from an AST tree
I have orders details as serialized in the TEXT Type of mysql field. Recently

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.