【问题标题】:Validation Forms without Entity with form class没有带有表单类的实体的验证表单
【发布时间】:2012-06-07 10:34:36
【问题描述】:

我需要关于没有实体但有表单类的验证表单的帮助。我的问题是覆盖方法getDefaultOoptions中我的表单类中的集合约束。当我在控制器中绑定表单时,它不会考虑这个集合。

这是我的班级表格

    namespace Projet\TicketBundle\Form;

use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Collection;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

class TicketPackAndAppType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder
            ->add('total', 'text' ,array('label' => 'Total de la commande  ',     'read_only' => true))


            ->add('address_delivery', 'entity' , array('class' => 'TacTill\CustomerBundle\Entity\Address',
                  'property' => 'address',
                  'required' => false,
                  'label' => 'Adresse de livraison  ',
                  'required' => false
                 ))
            ->add('address_invoice', 'entity' , array('class' => 'TacTill\CustomerBundle\Entity\Address',
                  'property' => 'address',
                  'required' => false,
                  'label' => 'Adresse de facturaton  ',
                  'required' => false
                 ))


            ->add('color', 'choice', array(
                'choices' => array(
                    'B' => 'Noir', 
                    'W' => 'Blanc'),
                'required' => true,
                "property_path" => false,
                'expanded' => true,
                'multiple' => false
            ))

            ->add('model', 'choice', array(
                'choices' => array(
                    'iPadDeux16Gowifi' => '16 Go Wifi', 
                    'iPadDeux16Gowifi3G' => '16 Go Wifi et 3G', 
                    'iPadNew16Gowifi' => '16GoWifi', 
                    'iPadNew32Gowifi' => '32 Go Wifi',
                    'iPadNew64Gowifi' => '64 Go Wifi',
                    'iPadNew16Gowifi4G' => '16 Go Wifi et 4G',
                    'iPadNew32Gowifi4G' => '32 Go Wifi et 4G',
                    'iPadNew64Gowifi4G' => '64 Go Wifi et 4G'
                    ),
                'required' => true,
                "property_path" => false,
                'expanded' => true,
                'multiple' => false
            ))       

            ->add('stand', 'choice', array(
                'choices' => array(
                    'standsecurity' => 'Sécurité', 
                    'standdesign' => 'Design',
                    'standmobility' => 'Mobilité'
                    ),
                'required' => true,
                "property_path" => false,
                'expanded' => true,
                'multiple' => false
            )) 

            ->add('printer', 'choice', array(
                'choices' => array(
                    'printerB' => 'Noire', 
                    'printerW' => 'Blanche'
                    ),
                'required' => true,
                "property_path" => false,
                'expanded' => true,
                'multiple' => false
            ))  

            ->add('cashDrawer', 'choice', array(
                'choices' => array(
                    'cashDrawerG' => 'Graphite', 
                    'cashDrawerP' => 'Perle'
                    ),
                'required' => true,
                "property_path" => false,
                'expanded' => true,
                'multiple' => false
            )) 

            ->add('app', 'choice', array(
                'choices' => array(
                    'appSmall' => 'S\'abonner à TacTill', 
                    'noApp' => 'Télécharger l\'application plus tard'
                    ),
                'required' => true,
                "property_path" => false,
                'expanded' => true,
                'multiple' => false
            ))                  
        ;                      

    }


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

    public function getDefaultOptions(array $options)
    {
        $collectionConstraint = new Collection(array(
                'color' => new NotBlank(array('message' => 'Champ vide')),
                'model' => new NotBlank(array('message' => 'Champ vide')),
                'stand' => new NotBlank(array('message' => 'Champ vide')),
                'printer' => new NotBlank(array('message' => 'Champ vide')),
                'cashDrawer' => new NotBlank(array('message' => 'Champ vide')),
                'app' => new NotBlank(array('message' => 'Champ vide')),
        ));

        return array('validation_constraint' => $collectionConstraint);
    }
}

在我的控制器中

public function createOrderAction()
{

    $em = $this->getDoctrine()->getEntityManager();

    $requestInArray = $this->getRequest()->request->get('ticket_packandapptype'); 

    $request = $this->getRequest();
    $form    = $this->createForm(new TicketPackAndAppType());

    if ($request->getMethod() == 'POST') {

        $form->bind($requestInArray);



        $stand = $requestInArray['stand'];
        $printer = $requestInArray['printer'];
        $cashDrawer = $requestInArray['cashDrawer'];
        $app = $requestInArray['app'];
  }
}   

我也用 bindRequest 进行了测试,这是同样的问题。

我关注Validation without class,但我不知道错误在哪里。 当我完成无表单类时,集合约束效果很好。

如果您有任何想法,请向我解释。谢谢你

【问题讨论】:

    标签: php symfony


    【解决方案1】:

    你应该这样做:

    if ($request->getMethod() == 'POST') {
    
        $form->bindRequest($request);
        if ($form->isValid()) {
            //do your actions with form values
        } else {
           //return form with error messages
           //or do smth else...
        }
    

    }

    当你调用 isValid() 时,Symfony 会尝试用你的约束来检查你...

    您还应该通过表单获取数据:

    $data = $form->get('your_field_name')->getData();
    

    当然你应该在从请求中绑定表单之后这样做......

    【讨论】:

      猜你喜欢
      • 2014-06-24
      • 2012-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多