You can override the ProfileFormType to add an event listener to the form.
Before the submission occurs this will modify the data and append the @something
suffix
if it hasn't been provided already by the user.
<?php
namespace Acme\UserBundle\Form\Type;
use Symfony\Component\Form\FormBuilderInterface;
use FOS\UserBundle\Form\Type\ProfileFormType as BaseType;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
class ProfileFormType extends BaseType
{
const USERNAME_SUFFIX = '@something';
public function buildForm(FormBuilderInterface $builder, array $options)
{
parent::buildForm($builder, $options);
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
$data = $event->getData();
if (isset($data['username']) && $this->usernameHasSuffix($data['username']) {
$data['username'] = $data['username'].self::USERNAME_SUFFIX;
}
$event->setData($data);
});
}
public function getName()
{
return 'acme_user_profile';
}
private function usernameHasSuffix($username)
{
return substr($username, -strlen(self::USERNAME_SUFFIX)) === self::USERNAME_SUFFIX;
}
}
Once created, just register it and tag it in the service container
# src/Acme/UserBundle/Resources/config/services.yml
services:
acme_user.profile.form.type:
class: Acme\UserBundle\Form\Type\ProfileFormType
arguments: [%fos_user.model.user.class%]
tags:
- { name: form.type, alias: acme_user_profile }
Then reconfigure the FOS UserBundle to use this form.
# app/config/config.yml
fos_user:
# ...
profile:
form:
type: acme_user_profile