提交表单后的值为null

时间:2017-11-17 16:22:38

标签: forms symfony

当我提交表单时出现此错误:

  

执行' INSERT INTO技能(名称)VALUES(?)'时发生异常。与params [null]:

我在modal ..page x.html.twig内容名称(SkillType)和级别(选择选项)中使用modal i加载页面x.html.twig。

我有这个表格类型:

xxx

和SkillType是:

<?php

        namespace AppBundle\Form\Type;

        use AppBundle\Entity\CandidateSkill;
        use Symfony\Component\Form\AbstractType;
        use Symfony\Component\Form\FormBuilderInterface;
        use Symfony\Component\OptionsResolver\OptionsResolver;
        use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
        use Symfony\Component\Form\Extension\Core\Type\SubmitType;
        use AppBundle\Form\Type\SkillType;
        use Symfony\Bridge\Doctrine\Form\Type\EntityType;


        /**
         * Class CandidateSkillType.
         */
        class CandidateSkillType extends AbstractType
        {
            /**
             * {@inheritdoc}
             */
            public function buildForm(FormBuilderInterface $builder, array $options)
            {
                $builder->add('level', ChoiceType::class, array('choices' => array('Beginner' => 'Beginner', 'Confirmed' => 'Confirmed', 'Senior' => 'Senior', 'Expert' => 'Expert')))
                ->add('skill', SkillType::class)->add('Create', SubmitType::class, array('attr' => array('class'=>'skill btn btn-fill btn-rose')));
            }

            /**
             * {@inheritdoc}
             */
            public function configureOptions(OptionsResolver $resolver)
            {
                $resolver->setDefaults(array(
                    'data_class' => CandidateSkill::class,
                    'csrf_protection' => 'false',
                    'allow_extra_fields' => 'false',
                ));
            }

            /**
             * {@inheritdoc}
             */
            public function getBlockPrefix()
            {
                return 'appbundle_candidateskill';
            }
        }

代码树枝:

<?php

    namespace AppBundle\Form\Type;

    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;
    use Symfony\Component\OptionsResolver\OptionsResolver;
    use Symfony\Component\Form\Extension\Core\Type\TextType;

    /**
     * SkillType.
     */
    class SkillType extends AbstractType
    {
        /**
         * {@inheritdoc}
         */
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder->add('name',null, array('attr' => array('class' => 'form-control','data-id'=>"test")));
        }

        /**
         * {@inheritdoc}
         */
        public function configureOptions(OptionsResolver $resolver)
        {
            $resolver->setDefaults(array(
                'data_class' => 'AppBundle\Entity\Skill',
                'csrf_protection' => 'false',
            ));
        }

        /**
         * {@inheritdoc}
         */
        public function getBlockPrefix()
        {
            return 'appbundle_skill';
        }
    }

代码控制器

<style>

    #ui-id-1 {
      width:150px;
      height:50px;
      top:169px;
      left:250px;
      z-index: 9999999999999999999999999999999 !important;
    }
    .ui-menu .ui-menu-item a {
      font-size: 12px;
    }
    .ui-menu {
      z-index: 9999999999999999999999999999999 !important;
    }
    .ui-font {
      z-index: 9999999999999999999999999999999 !important;
    }



    </style>
            <div class="row">

                <div class="col-md-12">
                    <div class="card">
                        <div class="card-header card-header-icon" data-background-color="rose">
                            <i class="material-icons">title</i>
                        </div>
                        <div class="card-content">
                            <h4 class="card-title">Add Skill</h4>
                            {{ form_start(form, { 'attr' : { 'class': 'theFormskill' } }) }}

                            {{ form_end(form) }}
                        </div>
                    </div>

                </div>
                </div>


     <script>
    $( function() {

        var availableTags= [];

        {% for skill in all%}

          availableTags.push('{{skill.skill.name}}');

        {% endfor%}
        console.log(availableTags);
        $( "#appbundle_candidateskill_skill_name" ).autocomplete({
          source: availableTags
        });
      } );



    $( ".skill" ).click(function() {
        $(".theFormskill").ajaxForm({url: "{{path('recurit_canddiate_skill_new')}}", type: 'post'});
    });

     </script>

create form和getData的函数句柄:

/**
     * Creates new CandidateSkill Object.
     *
     * @Template()
     * @Route("/add-candidateskill", name="recurit_canddiate_skill_new")
     *
     * @param Request $request
     *
     * @return array|\Symfony\Component\HttpFoundation\RedirectResponse
     */
    public function newAction(Request $request)
    {
        $candiateSkill = $this->getHandler()->post($request->request->all(), $request->isMethod(Request::METHOD_POST));

        if ($candiateSkill instanceof CandidateSkill) {
            return $this->redirectToRoute('fos_user_profile_show');
        }

        return ['form' => $candiateSkill->createView(),'all'=>$this->getHandler()->all()];
    }

当我为$ form转储时,&gt; getData()..数据为空

功能保存:

 /**
         * {@inheritdoc}
         */
        public function handle($object, array $parameters, string $method, bool $submited = false, array $options = [])
        {
            $options = array_replace_recursive([
                'method' => $method,
                'csrf_protection' => false,
            ], $options);
            $form = $this->formFactory->create(get_class($this->formType), $object, $options);
            if (!$submited) {
                return $form;
            }
            $form->submit($parameters, 'PATCH' !== $method);

            return $form->getData();
        }

当我在CandidateSkillHandler中的函数post()中...错误已经在

  

$这 - &GT; repository-&GT;保存($ candidateSkill);

1 个答案:

答案 0 :(得分:0)

我们在devit2017中发现这个表格没有用,因为请求没有得到处理。

这些行

$request = $this->requestStack->getCurrentRequest();
$form->handleRequest($request); 

而不是

$form->submit($parameters, 'PATCH' !== $method);

解决这个问题。

相关问题