dongxu4023 2014-06-13 21:40
浏览 62
已采纳

使用未映射的Web表单的模型(表单类型)

I want to handle all validations with @Assert so I'm using Models for my web forms (Form Type) which are not mapped to database. The question I have is, is it an acceptable practise in Symfony world?

I know that one disadvantage of this way is not being able to automatically generate setters and getters. I read up on it but didn't get a clear picture so that's why I'm asking.

A rough example:

LoginType.php

namespace User\RecordBundle\Resources\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class LoginType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->setAction($options['action'])
            ->setMethod('POST')
            ->add('username', 'text', array('label' => 'Username'))
            ->add('button', 'submit', array('label' => 'Login'));
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array('data_class' => 'User\RecordBundle\Entity\UserEntity'));
    }

    /**
     * Returns the name of this type.
     *
     * @return string The name of this type
     */
    public function getName()
    {
        return 'login';
    }
}

LoginModel.php

namespace User\RecordBundle\Resources\Form\Model;

use Symfony\Component\Validator\Constraints as Assert;

/**
 * Class LoginModel
 * Mapped to Login form
 *
 * @package User\RecordBundle\Resources\Form\Model
 */
class LoginModel
{
    /**
     * @Assert\NotBlank(message = "The Username field should not be blank.")
     *
     * @var string $username
     */
    protected $username;

    /**
     * @return string $username
     */
    public function getUsername()
    {
        return $this->username;
    }

    /**
     * @param string $username
     * @return $this
     */
    public function setUsername($username)
    {
        $this->username = $username;

        return $this;
    }
} 
  • 写回答

1条回答 默认 最新

  • doudou32012 2014-06-14 10:46
    关注

    This case: your FormType is not related to any Entity, must be rare in a well planned application. So rarely Model with FormType solution can be used, I don't have any objections to it. Remark: Specifically for User handling I recommend you to use friends of symfony created: FOS\UserBundle\FOSUserBundle().

    You said that you're new in Symfony, so I summarized here the general practice of making a Form, which is related to an Entity and user will be available to fill some part of it.

    class code:

    class Entity
    {
    /**
      * @Assert\NotBlank(message = "The data is empty.")
      */
    private $data;
    }
    

    form type code:

    use Doctrine\Common\Persistence\ObjectManager;
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;
    use Symfony\Component\OptionsResolver\OptionsResolverInterface;
    use Doctrine\ORM\EntityRepository;
    
    class EntityType extends AbstractType
    {
    /**
     * @var \Doctrine\Common\Persistence\ObjectManager
     */
    protected $om;
    
    protected $admin;
    
    protected $edit;
    
    public function __construct($om, $admin = false, $new = false)
    {
        $this->om = $om;
        $this->admin = $admin;
        $this->new = $;
    }
    
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        // You can show specific parts of the Entity for the admin to fill and reduced group of fields for the common user.
        // You can set specific behaviour in case of creation and editing.
        // You can add a collection type field, and select specific entities with ObjectManager. To make your form more flexible.
        $builder
            ->add('data', 'text', array(
                'label' => 'Data text',
                'label_attr' => array(
                    'class' => 'reqfield'
                ),
                'attr' => array(
                    'class' => 'css-class-of-the-field'
                '...' => $etc,
            ))
            // You don't need to add submit button
    }
    
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Application\EntityBundle\Entity\Entity'
        ));
    }
    
    public function getName()
    {
        return 'application_entity_bundle_entity';
    }
    }
    // Actions like: new and edit, can use your formType above.
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输