Showing posts with label symfony 2. Show all posts
Showing posts with label symfony 2. Show all posts

Sunday, June 16, 2013

Symfony 2 FOSUserBundle - overriding the registration success event

I was trying the change the registration flow of FOSUserBundle the other day. The goal is to let new users to use the site without activating their accounts while an activation email can still be sent. Every time these users log in to the site, a flash message will show up and ask them to activate their accounts.

The first thing I did was creating a EmailConfirmationListener. You can copy this file from the FOSUserBundle in the vender's folder. All I did was 1) setting user isEnabled flag to true and 2) changing the route (if needed).

Note that the EmailConfirmationListener registers the following event:
FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess'
When I registered a new user, I registered two activation emails were be sent.

The trace log shows that two onRegistrationSuccess events were fired. One is the one from the vendor, the other one is the one I just created.

The listener service is declared in email_confirmation.xml.

Doing a quick search on the project root folder for the term "email_confirmation.xml" reveals the following:
if ($config['confirmation']['enabled']) {
         $loader->load('email_confirmation.xml');
}
Now turn the confirmation setting to false in config.yml

fos_user:
    registration:
        confirmation:
            enabled: true

Then only one onRegistrationSuccess event would be fired.

Thursday, May 16, 2013

Symfony 2 Forms - validate at least one field is non empty

Say you have a form that allows you to add a post to an existing list or a new list. The form will have a dropdown box with existing lists and an input box asking the user to input the name of the new list. When the user clicks on submit, the post will be added to that list.

In earlier version of Symfony 2, you can use the CallbackValidator to do that, but it's deprecated. You will need to use event listener now.

In the following example, the dropdown box has the element name 'listId' while the input box has the element name 'listName'.

In your form's buildForm function, add the following EventListener.

public function buildForm(FormBuilderInterface $builder, array $options)
{
        $builder->addEventListener(FormEvents::POST_BIND, function (DataEvent $event) {
            $form = $event->getForm();

            $listName = $form['listName']->getData();
            $originalListId = $form['listId']->getData();

            if( ($listName == null || $listName == '') && ($listId == null || $listId == '') ) {
                $form['listId']->addError(new FormError("Please select a list or create a new list."));
            }
        });
}

Saturday, February 9, 2013

Symfony 2 SwiftMailer Spoof and Redirect Response Abnormal behavior

Specs:
  • Symfony 2.1.x
  • FOSUserBundle
  • SwiftMailer
I was trying to modify the registration flow in FOSUserBundle to do two things:
  • After a user registers, he/she will be logged in automatically (no need to confirm emails)
  • Send an email to the user upon successful registration
However, I find that after the user registers, he/she is always redirected to the login page (I want them to be in an authenticated page). If the user refreshes this page (via the refresh button in the browser), they would be logged in.

After numerous hours of debugging, I found that if I turn off the memory spooling option in SwiftMailer, everything would work fine.

In app/config/config.yaml
swiftmailer:
    transport: %mailer_transport%
    encryption: %encryption%
    auth_mode: login
    host:      %mailer_host%
    username:  %mailer_user%
    password:  %mailer_password%
    #spool:     { type: memory }
If anyone knows why this is happening, please comment below.