Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 对未映射的web表单(表单类型)使用模型_Php_Symfony - Fatal编程技术网

Php 对未映射的web表单(表单类型)使用模型

Php 对未映射的web表单(表单类型)使用模型,php,symfony,Php,Symfony,我想用@Assert处理所有验证,所以我使用的是未映射到数据库的web表单(表单类型)模型。我的问题是,在Symfony世界,这是一种可以接受的做法吗 我知道这种方法的一个缺点是不能自动生成setter和getter。我读过了,但没有得到一个清晰的图片,所以这就是为什么我问 一个粗略的例子: LoginType.php namespace User\RecordBundle\Resources\Form\Type; use Symfony\Component\Form\AbstractType

我想用@Assert处理所有验证,所以我使用的是未映射到数据库的web表单(表单类型)模型。我的问题是,在Symfony世界,这是一种可以接受的做法吗

我知道这种方法的一个缺点是不能自动生成setter和getter。我读过了,但没有得到一个清晰的图片,所以这就是为什么我问

一个粗略的例子:

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';
    }
}
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;
    }
} 
LoginModel.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';
    }
}
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;
    }
} 

这种情况:您的FormType与任何实体都不相关,在计划良好的应用程序中必须是罕见的。所以很少可以使用带有FormType解决方案的模型,我对此没有任何异议备注:特别是对于用户处理,我建议您使用symfony创建的好友:FOS\UserBundle\FOSUserBundle()

你说你是Symfony的新手,所以我在这里总结了制作表单的一般做法,表单与实体相关,用户可以填写表单的某些部分

类别代码:

class Entity
{
/**
  * @Assert\NotBlank(message = "The data is empty.")
  */
private $data;
}
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.
表单类型代码:

class Entity
{
/**
  * @Assert\NotBlank(message = "The data is empty.")
  */
private $data;
}
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.