Symfony2使用继承属性创建没有嵌入表单的表单类型

时间:2014-09-09 14:36:57

标签: symfony

我正在尝试创建一个具有继承属性的表单类型,我有一个实体(Candidate),它有两个属性(String dni和User user)。

我做了另一个question,但我想要一个没有嵌入形式的解决方案,就像这样(由FyodorX提供):

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
    ->add('dni', null, array_merge(array(
        'label' => 'form.dni',
        'translation_domain' => 'DroidersCandidateBundle',
    ), $this->mergeOptions))
    ->add('username', null, array_merge(array(
        'label' => 'form.username',
        'property_path' => 'user.username',
        'translation_domain' => 'SonataUserBundle'
    ), $this->mergeOptions))
    ;
}

但是有了这个我有这个错误:

  

类型为“object or array”的预期参数,给出“NULL”   500内部服务器错误 - UnexpectedTypeException

这是我的代码:

Candidate.php

class Candidate
{
/**
 * @var integer
 */
private $id;

/**
 * @var string
 */
private $dni;

/**
 * @var /Application\Sonata\UserBundle\Entity\User
 */
private $user;

/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}

/**
 * Set dni
 *
 * @param string $dni
 * @return Candidate
 */
public function setDni($dni)
{
    $this->dni = $dni;

    return $this;
}

/**
 * Get dni
 *
 * @return string 
 */
public function getDni()
{
    return $this->dni;
}

/**
 * Set user
 *
 * @param \Application\Sonata\UserBundle\Entity\User $user
 * @return Candidate
 */
public function setUser(\Application\Sonata\UserBundle\Entity\User $user = null)
{
    $this->user = $user;

    return $this;
}

/**
 * Get user
 *
 * @return \Application\Sonata\UserBundle\Entity\User 
 */
public function getUser()
{
    return $this->user;
}

}

RegistrationFormType.php

class RegistrationFormType extends AbstractType{

private $class;

/**
 * @var array
 */
protected $mergeOptions;

/**
 * @param string $class        The User class name
 * @param array  $mergeOptions Add options to elements
 */
public function __construct($class, array $mergeOptions = array())
{
    $this->class        = $class;
    $this->mergeOptions = $mergeOptions;
}

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('dni', null, array_merge(array(
            'label' => 'form.dni',
            'translation_domain' => 'DroidersCandidateBundle',
        ), $this->mergeOptions))
        ->add('username', null, array_merge(array(
            'label' => 'form.username',
            'property_path' => 'user.username',
            'translation_domain' => 'SonataUserBundle'
        ), $this->mergeOptions))
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => $this->class,
        'intention'  => 'registration',
    ));
}

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

RegistrationCandidateController.php:

class RegistrationCandidateController extends RegistrationFOSUser1Controller {

public function registerAction()
{
    $user = $this->container->get('security.context')->getToken()->getUser();

    if ($user instanceof UserInterface) {
        $this->container->get('session')->getFlashBag()->set('sonata_user_error', 'sonata_user_already_authenticated');
        $url = $this->container->get('router')->generate('sonata_user_profile_show');

        return new RedirectResponse($url);
    }

    $form = $this->container->get('droiders_candidates.registration.form');
    $formHandler = $this->container->get('droiders_candidates.registration.form.handler');
    $confirmationEnabled = $this->container->getParameter('fos_user.registration.confirmation.enabled');

    $process = $formHandler->process($confirmationEnabled);
    if ($process) {
        $candidate = $form->getData();
        $user = $candidate->getUser();

        $authUser = false;
        if ($confirmationEnabled) {
            $this->container->get('session')->set('fos_user_send_confirmation_email/email', $user->getEmail());
            $url = $this->container->get('router')->generate('fos_user_registration_check_email');
        } else {
            $authUser = true;
            $route = $this->container->get('session')->get('sonata_basket_delivery_redirect');

            if (null !== $route) {
                $this->container->get('session')->remove('sonata_basket_delivery_redirect');
                $url = $this->container->get('router')->generate($route);
            } else {
                $url = $this->container->get('session')->get('sonata_user_redirect_url');
            }
        }

        $this->setFlash('fos_user_success', 'registration.flash.user_created');

        $response = new RedirectResponse($url);

        if ($authUser) {
            $this->authenticateUser($user, $response);
        }

        return $response;
    }

    $this->container->get('session')->set('sonata_user_redirect_url', $this->container->get('request')->headers->get('referer'));

    return $this->container->get('templating')->renderResponse('DroidersCandidateBundle:Registration:register.html.'.$this->getEngine(), array(
            'form' => $form->createView(),
        ));
}

}

services.yml:

droiders_candidates.registration.form:
    factory_method: createNamed
    factory_service: form.factory
    class: Symfony\Component\Form\Form
    arguments:
        - droiders_registration_form
        - @droiders_candidates.registration.form.type
        - null
        - { validation_groups: %fos_user.registration.form.validation_groups% }

droiders_candidates.registration.form.type:
    class: Droiders\CandidateBundle\Form\Type\RegistrationFormType
    arguments:
        - %droiders.model.candidate.class%
        - %sonata.user.registration.form.options%

droiders_candidates.registration.form.handler:
    class: Droiders\CandidateBundle\Form\Handler\RegistrationFormHandler
    scope: request
    arguments:
        - @droiders_candidates.registration.form
        - @request
        - @droiders.candidate_manager
        - @fos_user.mailer
        - @fos_user.util.token_generator

0 个答案:

没有答案