Assuming you are using FOSUserBundle and that you don't use the username field I think the easier way is to "compose" your 3 fields and use them as username
To do this you need:
- Customize the login form. Hide the username field and add your 3.
- Create a
kernel.request listener that read your 3 fields and add the username fields to the request object. Once you have done this you are in the "normal" flow
Let's say I've these data:
In the DB my username would be [email protected]|MyRandomString|1 (You can compose the username as you like)
Here an example for the listener
My\PersonBundle\Listener\MyLoginFieldFormatterListener
namespace My\AwesomeBundle\Listener;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
class MyLoginFieldFormatterListener {
public function onKernelRequest(GetResponseEvent $event)
{
// This is you request object.
// http://api.symfony.com/2.5/Symfony/Component/HttpFoundation/Request.html
$request = $event->getRequest();
// Add a condition to execute code only when route login data are submitted is required
if ($request->get('_route') != 'fos_user_security_check')
return;
// You can access POST values via `request` property
$yourField1 = $request->request->get('yourField1');
// Get the other fields and compose your username
// The field name MUST be _username as long as you didn't overwrite it in the conf
$request->request->set('_username', $yourComposedUsername);
}
}
And the configuration
services.yml
services:
listener.compose_username_listener:
class: My\PersonBundle\Listener\MyLoginFieldFormatterListener
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest, priority: 255 }
Do not forget priority so this listener is the first one executed.
loadUserByUsernameas a method to get a user by an unique identifier. Do you have an unique field for you users ? - Hpatoio