FosUserBundle:验证用户名或电子邮件

时间:2012-09-28 12:10:43

标签: php symfony fosuserbundle

我通过电子邮件选项激活FosUserBundle登录 现在我想验证ProfileForm上的两个字段之一(用户名或电子邮件) 所以我创建了一个新的ProfileForm,一个新的Profile类和一个新的Profile FormHandler:

ProfileFormType

public function buildForm(FormBuilder $builder, array $options)
{
    $child = $builder->create('user', 'form', array('data_class' => $this->class));
    $this->buildUserForm($child, $options);
    $builder
        ->add($child)
        ->add('new', 'repeated', array('type' => 'password'))
        ->add('send', 'checkbox', array(
            'label'     => 'Email',
            'required'  => false,
                    ))
                    ;
}

public function getDefaultOptions(array $options)
{
    return array(
        'data_class' => 'Tga\Bundle\UserManagementBundle\Form\Model\Profile',
        'intention'  => 'profile',
    );
}    

public function getName()
{
    return 'fos_user_profile';
}

/**
 * Builds the embedded form representing the user.
 *
 * @param FormBuilder $builder
 * @param array       $options
 */
protected function buildUserForm(FormBuilder $builder, array $options)
{
    $builder
        ->add('username', 'text', array('required' => false))
        ->add('email', 'email', array('required' => false))
        ->add('enabled', 'checkbox', array('required' => false))
    ;
}

资料

/*
 * This file is part of the FOSUserBundle package.
 *
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Tga\Bundle\UserManagementBundle\Form\Model;

use FOS\UserBundle\Model\UserInterface;
use Symfony\Component\Validator\Constraints as Assert; 

class Profile
{
    /**
     * @var string
     */
    public $new;

    /**
     * @var boolean
     */
    public $send = true;    

    /**
     * User whose password is changed
     *
     * @var UserInterface
     */
    public $user;

    public function __construct(UserInterface $user)
    {
        $this->user = $user;
    }

    /**
     * @Assert\True(message = "Error for one of the two fields")
     */
    public function isEmailOrUsername()
    {
        return ($this->user->getUsername() == '' && $this->user->getEmail() == '') ? false : true;
    }        
}

ProfileFormHandler

/*
 * This file is part of the FOSUserBundle package.
 *
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Tga\Bundle\UserManagementBundle\Form\Handler;

use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;

use FOS\UserBundle\Model\UserInterface;
use FOS\UserBundle\Model\UserManagerInterface;
use FOS\UserBundle\Form\Model\CheckPassword;
use Tga\Bundle\UserManagementBundle\Form\Model\Profile;
use FOS\UserBundle\Mailer\MailerInterface;

use FOS\UserBundle\Form\Handler\ProfileFormHandler as BaseHandler;

class ProfileFormHandler extends BaseHandler
{
    protected $request;
    protected $userManager;
    protected $form;
    protected $mailer;    

    public function __construct(Form $form, Request $request, UserManagerInterface $userManager, MailerInterface $mailer)
    {
        $this->form = $form;
        $this->request = $request;
        $this->userManager = $userManager;
        $this->mailer = $mailer;
    }

    public function getNewPassword()
    {
        return $this->form->getData()->new;
    }    

    public function getSend()
    {
        return $this->form->getData()->send;
    }        

    public function process(UserInterface $user)
    {

        $this->form->setData(new Profile($user));
        if ('POST' === $this->request->getMethod()) {
            $this->form->bindRequest($this->request);
            if ($this->form->isValid()) {
                $this->onSuccess($user);

                return true;
            }

            // Reloads the user to reset its username. This is needed when the
            // username or password have been changed to avoid issues with the
            // security layer.
            $this->userManager->reloadUser($user);
        }

        return false;
    }

    protected function onSuccess(UserInterface $user)
    {
        $user->setPlainPassword($this->getNewPassword());
        //ici envoi des identifiants par mail
        if($this->getSend())
        {
            $this->mailer->sendIdPwdMessage($user, $this->getNewPassword());
        }
        $this->userManager->updateUser($user);
    }    
}

我在句柄处理功能上有错误,除非我填写用户名和电子邮件:

Fatal error: Allowed memory size" when $this->form->bindRequest($this->request)

0 个答案:

没有答案