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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:54:16+00:00 2026-06-15T23:54:16+00:00

I have modified directly a FOSUserForm (with no overriding) to try and assign a

  • 0

I have modified directly a FOSUserForm (with no overriding) to try and assign a default value while building the form, I have added the

array('data'=>'default value') 

such as:

namespace FOS\UserBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class RegistrationFormType extends AbstractType
{
private $class;

/**
 * @param string $class The User class name
 */
public function __construct($class)
{
    $this->class = $class;
}

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('username', null, array('data' => 'Default value','label' => 'form.username', 'translation_domain' => 'FOSUserBundle'))
        ->add('email', 'email', array('data' => 'Default value','label' => 'form.email', 'translation_domain' => 'FOSUserBundle'))
        ->add('plainPassword', 'repeated', array(
            'type' => 'password',
            'options' => array('translation_domain' => 'FOSUserBundle'),
            'first_options' => array('label' => 'form.password'),
            'second_options' => array('label' => 'form.password_confirmation'),
            'invalid_message' => 'fos_user.password.mismatch',
        ))
    ;
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => $this->class,
        'intention'  => 'registration',
    ));
}

public function getName()
{
    return 'fos_user_registration';
}

It doesn’t work and I keep having a form validation message “Please enter a username”
what’s wrong?

  • 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-15T23:54:17+00:00Added an answer on June 15, 2026 at 11:54 pm

    Have you tried setting the username as a hidden field instead of “null”? Or remove the username from the form completely and set it manually before the entity is persisted in the database. I don’t recommend the second option unless you have performed a check to make sure the username is unique before persisting it to the database.

    Edit:

    Out of curiousity, why are you modifying a vendor class directly?

    Edit 2:

    The default validation for the username field should probably not be removed (and if you do, make sure it is still a unique field in the database (http://symfony.com/doc/2.0/reference/constraints/UniqueEntity.html).

    If you want to set a default username value, instead of setting the data option in the form, create a custom RegistrationFormHandler that overrides the default createUser method:

    <?php
    
    namespace Acme\UserBundle\Form\Handler;
    
    use FOS\UserBundle\Form\Handler\RegistrationFormHandler as BaseHandler;
    
    class RegistrationFormHandler extends BaseHandler
    {
        /**
         * @return UserInterface
         */
        protected function createUser()
        {
            $user = $this->userManager->createUser();
            $user->setUsername('customusername');
    
            return $user;
        }
    }
    

    Instructions on overriding this handler can be found here (towards the bottom):
    https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/overriding_forms.md

    It’s pretty simple, should only take a minute to implement. Additionally, creating the custom formhandler will also prevent any changes you’ve made in the vendor classes from being lost when you update your vendors via composer.

    Edit 3:

    DependencyInjection\EtienneUserExtension.php:

    <?php
    
    namespace Etienne\UserBundle\DependencyInjection;
    
    use Symfony\Component\DependencyInjection\ContainerBuilder;
    
    //if using xml
    use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
    
    //if using yaml
    use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
    
    use Symfony\Component\HttpKernel\DependencyInjection\Extension;
    use Symfony\Component\Config\FileLocator;
    
    class EtienneUserExtension extends Extension
    {
        public function load(array $configs, ContainerBuilder $container)
        {
            //if using XML
            $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
            $loader->load('services.xml');
    
            //or if using yaml services
            $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
            $loader->load('services.yml');
        }
    
        public function getAlias()
        {
            return 'etienne_user';
        }
    }
    

    services.xml

    <?xml version="1.0" ?>
    
    <container xmlns="http://symfony.com/schema/dic/services"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
    
    
    <services>
        <service id="acme_user.form.handler.registration" class="Acme\UserBundle\Form\RegHandler" scope="request" public="false">
            <argument type="service" id="fos_user.registration.form" />
            <argument type="service" id="request" />
            <argument type="service" id="fos_user.user_manager" />
            <argument type="service" id="fos_user.mailer" />
            <argument type="service" id="fos_user.util.token_generator" />
        </service>
    </services>
    
    </container>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Recently I have modified my code to While taking input form STDIN, I moved
I'm building a custom Ubuntu kernel and have modified one of the source files.
I have a .tpl file which contain form data which contain both add and
I have modified a slider script to mimic ebay's slider on home page. I
I have modified the Nerd Dinner application to allow editing of child records by
I have modified my MOSS 2007 configuration to query a given target AD successfully.
I have modified a working Windows service that had always been starting beforehand. After
I have modified a website with a redirection to a single page: RewriteCond %{REQUEST_FILENAME}
I have modified unobtrusive validation script to replace error messages with qtips. To do
I have cloned a remote SVN repository with git-svn. I have modified a pom.xml

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.