Reputation: 164
I'm using the FOSuserbundle and I'm trying to pre populate a field in my database on registration. I'm trying to auto add values like "http://localhost:1337/digitalartlab/web/profile/{username}/checkin". Username would be the only variable of this string. How can I do this?
User.php
/**
* Set checkinurl
*
* @param string $checkinurl
*
* @return User
*/
public function setCheckinurl($checkinurl)
{
$this->checkinurl = $checkinurl;
return $this;
}
/**
* Get checkinurl
*
* @return string
*/
public function getCheckinurl()
{
return $this->checkinurl;
}
registrationtype.php (Some extra fields I added to the registration form).
<?php
// src/AppBundle/Form/RegistrationType.php
namespace DigitalArtLabBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class RegistrationType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('firstname');
$builder->add('lastname');
$builder->add('address');
$builder->add('zipcode');
$builder->add('Expertises');
$builder->add('Interesses');
$builder->add('saldo', 'integer', array(
'label' => 'Saldo',
'data' => '0'
));
}
public function getParent()
{
return 'fos_user_registration';
}
public function getName()
{
return 'app_user_registration';
}
}
registrationcontroller: (the disabled codes are my own attempts to populate the field)
public function registerAction()
{
$form = $this->container->get('fos_user.registration.form');
$formHandler = $this->container->get('fos_user.registration.form.handler');
$confirmationEnabled = $this->container->getParameter('fos_user.registration.confirmation.enabled');
$process = $formHandler->process($confirmationEnabled);
if ($process) {
$user = $form->getData();
$authUser = false;
if ($confirmationEnabled) {
$this->container->get('session')->set('fos_user_send_confirmation_email/email', $user->getEmail());
$route = 'fos_user_registration_check_email';
} else {
$authUser = true;
/*$userdata = $this->get('fos_user.user_manager')->updateUser($user->getUsername());
$namespace = 'localhost:1337/digitalartlab/web/profile';
$userdata->setCheckinurl('http://'.$namespace.'/'.$user->getUsername().'/checkin');*/
$route = 'fos_user_registration_confirmed';
}
$this->setFlash('fos_user_success', 'registration.flash.user_created');
$url = $this->container->get('router')->generate($route);
$response = new RedirectResponse($url);
if ($authUser) {
$this->authenticateUser($user, $response);
}
return $response;
}
return $this->container->get('templating')->renderResponse('FOSUserBundle:Registration:register.html.'.$this->getEngine(), array(
'form' => $form->createView(),
));
}
I've read the documentation but it's all just really confusing to me. I hope someone can help me make it clear how this works. Thanks.
Upvotes: 0
Views: 388
Reputation: 4704
You can create a registration listener to perform this task. See the docs here.
Be sure to add a service for the listener in services.yml
:
services:
your_bundle.registration_listener:
class: YourBundle\EventListener\RegistrationListener
tags:
- { name: kernel.event_subscriber }
Listener:
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\FormEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* RegistrationListener
*
*/
class RegistrationListener implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return array(
FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess',
);
}
public function onRegistrationSuccess(FormEvent $event)
{
$user = $event->getForm()->getData();
$userName = $user->getUserName();
$checkInUrl = 'http://localhost:1337/digitalartlab/web/profile/' . $username;
$user->setCheckinurl($checkInUrl);
}
}
Upvotes: 1