提交表单后值为空

问题描述 投票:0回答:1

当我提交表单时,出现以下错误:

使用参数 [null] 执行“INSERT INTO Skills (name) VALUES (?)”时发生异常:

我使用模态加载页面 x.html.twig 在模态..页面 x.html.twig 内容名称(技能类型)和级别(选择选项)。

我有这个表格类型:

<?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>

代码控制器

/**
     * 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()];
    }

用于创建表单和获取数据的函数句柄:

 /**
         * {@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();
        }

当我转储 $form->getData() ..data 为空时

功能保存:

/**
 * @param mixed $object
 * @param array $arguments
 */
public function save($object, array $arguments = ['flush' => true])
{
    $this->em->persist($object);
    if (true === $arguments['flush']) {
        $this->em->flush();
    }
}

当我在 CandidateSkillHandler 中使用 post() 函数时 .. 错误出现在

$this->存储库->保存($candidateSkill);

php forms symfony
1个回答
0
投票

我们在 devit2017 中发现此表单不起作用,因为请求未得到处理。

这些行

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

而不是

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

解决这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.