title: Symfony 2 stop saving form process
body: <p>I am trying to link my local users stored in the database with some external services, so they can login anywhere with the same credentials.</p>
  
  <p>I have an EventListener waiting for some FOSUserEvents like <code>FOSUserEvents::REGISTRATION_SUCCESS</code> or <code>FOSUserEvents::CHANGE_PASSWORD_SUCCESS</code>, fired when the data are valid but not actually saved, to perform some call to the various external services and replicate the new user credentials.</p>
  
  <p>If the services return a message saying that the data are replicated, everything work fine.</p>
  
  <p>But if a service say that there is a problem, no mater what it is, I would like to interrupt the saving process of the form and adding an error message.</p>
  
  <p>The objective is to prevent Symfony to save the data, even if they are valid, if an external service say <code>no</code>, and I don't know how to perform this kind of emergency stop.</p>
  
  <p>Actually, it's only based on FOSUserBundle but if I find a working solution, I will have to execute something similar for other entities, that why I try to be as generic as possible.</p>
  
  <p>Here a some of my code</p>
  
  <pre><code>class MyListener implements EventSubscriberInterface
  {
      public static function getSubscribedEvents()
      {
          return array(
              FOSUserEvents::REGISTRATION_SUCCESS =&gt; 'createUser',
              FOSUserEvents::CHANGE_PASSWORD_SUCCESS =&gt; 'editUser'
          );
      }
      public function createUser(FormEvent $event)
      {
          $user = $event-&gt;getForm()-&gt;getData();
  
          // Check if the user exists before trying to edit
          $result = $this-&gt;_userExists($user);
          if($result['value'] == false)
          {
  
              // Create the user
              $result = $this-&gt;_createUser($user);
  
              // Check if the user is successfully replicated
              if($result['result'] != 'success')
              {
                  /*
                   * Emergency stop (user not replicated)
                   */
              }
          }else{
             /*
              * Emergency stop (user doesn't exists)
              */
          }
      }
  
      . . .
  }
  </code></pre>
  
  <p>Actually I didn't find any way to stop the saving workflow and returning to the form page to display an error message. So if somebody has an idea of how to perform this, feel free so write your idea, and thanks you all guys. Stackoverflow is really the best ;-)</p>