1

I need to transform a user name to a user entity, and I'm using this transformation in many form,every form need some specific test to the returned user entity.

In the registration form a user can enter a referral code (user name) or leave this empty, the referral code must have the role ROLE_PARTNER.

In the dashboard a partner can append some user to his account: in this case the user name can't be empty, the user must have the role ROLE_CLIENT and the partner can't enter his own user name

this is my data transformer class

class UserToNameTransformer implements DataTransformerInterface
{
    private $om;
    private $role;
    private $acceptEmpty;
    private $logged;
    public function __construct(ObjectManager $om,$role,$acceptEmpty,$logged)
    {
        $this->om = $om;
        $this->role=$role;
        $this->acceptEmpty=$acceptEmpty;
        $this->logged=$logged;
    }
    public function transform($user)
    {
        if (null === $user) {
            return "";
        }

        return $user->getUsername();
    }
    public function reverseTransform($username)
    {
        if (!$username) {
            if ($this->acceptEmpty)
            return null;
            else 
                throw new TransformationFailedException(sprintf(
                        'user name can\'t be empty!'
                        ));

        }

        $user = $this->om
        ->getRepository('FMFmBundle:User')
        ->findOneBy(array('username' => $username))
        ;
        if (null === $user) {
            throw new TransformationFailedException(sprintf(
                    'user with user name "%s" not found!',
                    $username
                    ));
        }
        else if (!$user->hasRole($this->role)){
            throw new TransformationFailedException(sprintf(
                    'user name "%s" is not valid',
                    $username
                    ));
        }
        else if($this->logged==true){
            $activeUser=$this->get('security.context')->getToken()->getUser();
            if($user===$activeUser)
                throw new TransformationFailedException(sprintf(
                        'you can\'t enter you user name'
                        ));
        }
        else
            return $user;
    }
}

Form type
this form work fine because I'm not using the custom field

class ActivatePartnerType extends AbstractType
{

    private $entityManager;

    public function __construct(EntityManager $entityManager)
    {
        $this->entityManager = $entityManager;
    }
    public function buildForm(FormBuilderInterface $builder, array $options)
    {

        $builder
            ->add('user','text', array(
                'invalid_message' => 'That is not a valid username',
                'label' => 'Partner Username :',
            ))
            ->add('next','submit')
        ;
        $builder->get('user')
            ->addModelTransformer(new UserToNameTransformer($this->entityManager,'ROLE_PARTNER',false,true));
    }
    public function getName()
    {
        return 'fm_fmbundle_activate_partner';
    }
}

The custom field type

class UserSelectorType extends AbstractType
    {
        private $entityManager;

        public function __construct(EntityManager $entityManager)
        {
            $this->entityManager = $entityManager;
        }

        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $transformer = new UserToNameTransformer($this->entityManager,**{others parametres}**);
            $builder->addModelTransformer($transformer);
        }

        public function configureOptions(OptionsResolver $resolver)
        {
            $resolver->setDefaults(array(
                    'invalid_message' => 'The selected user does not exist'
            ));
        }

        public function getParent()
        {
            return 'text';
        }

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

the custom field service

FmarketUserSelector:
            class: FM\FmBundle\Form\UserSelectorType
            arguments: ["@doctrine.orm.entity_manager"]
            tags:
                - { name: form.type, alias: user_selector }

the form type

class ActivatePartnerType extends AbstractType
{

    private $entityManager;

    public function __construct(EntityManager $entityManager)
    {
        $this->entityManager = $entityManager;
    }
    public function buildForm(FormBuilderInterface $builder, array $options)
    {

        $builder
            ->add('user','user_selector')
            ->add('next','submit')
        ;
    }
    public function getName()
    {
        return 'fm_fmbundle_activate_partner';
    }
}
1
  • I'm not sure I understand your question. You ask how to pass in parameters, but then you have a place holder exactly where you would do so. Is your question instead "how do I get these values so I can pass them in as parameters?" Commented Aug 24, 2015 at 18:01

2 Answers 2

0

Look at this pages:

In your case, it would look like this:

FmarketUserSelector:
    class: FM\FmBundle\Form\UserSelectorType
    arguments: ["@doctrine.orm.entity_manager", "ROLE_PARTNER", false, true]
    tags:
        - { name: form.type, alias: user_selector }

but looks like, you're passing dynamic values to your form type, that's why this doesn't suit your needs I think..

In your case you can call your form type as a regular class call, like this:

...

$builder
    ->add('user', new UserSelectorType(ARG1, ARG2, ...))
    ->add('next','submit')
;

...

may be this answer can also help you a bit..

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the code and the link,I used your solution and it work fine. the link give me the idea of using the $options array, and i tried it,it work fine .
your solution is not using the service declaration so it's not useful in my case at that time cause now I'm using constraint validation instead of arguments.
0

Here, is how I use the $options array

class UserSelectorType extends AbstractType
{
    private $entityManager;

    public function __construct(EntityManager $entityManager)
    {
        $this->entityManager = $entityManager;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $transformer = new UserToNameTransformer($this->entityManager,$options['role'],$options['accept_empty'],$options['logged']);
        $builder->addModelTransformer($transformer);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
                'invalid_message' => 'The selected user does not exist',
                'role'=>'ROLE_PARTNER',
                'accept_empty'=>true,
                'logged'=>false
        ));
    }

    public function getParent()
    {
        return 'text';
    }

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

In the form type

$builder->add('referral','user_selector',array(
                    'role'=>'ROLE_PARTNER',
                    'accept_empty'=>false,
                    'logged'=>false
            ));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.