dreamer1231 2012-06-07 10:34
浏览 37
已采纳

没有实体的验证表单与表单类

I need help about validation forms without entity but with form class. My problem is the collection constraint in my form class in the override method getDefaultOoptions. It doesn't take this collection into account when I bind the form in my controller.

This is my class form

    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);
    }
}

In my controller

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'];
  }
}   

I have also test with bindRequest and that is the same problem.

I follow the Validation without class but i don't where is the mistake. When i have done the without form class the collection constraints works well.

If you have any idea, explain to me. Thank you

  • 写回答

1条回答 默认 最新

  • dongxiezhuo8852 2012-07-26 15:02
    关注

    You should do smth like this:

    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...
        }
    

    }

    When you call isValid() Symfony tries to checks you daya with your constraint...

    Also you should get your data through form:

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

    Of course you should do it after binding form from request...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法