2017-11-17 7 views
1

私はこのエラーを持っている:私はページをロードするモーダル使用null値の後に送信されたフォーム

An exception occurred while executing 'INSERT INTO skills (name) VALUES (?)' with params [null]:

をx.html.twigにおけるモーダル..page x.html.twigコンテンツ名(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'; 
      } 
     } 

およびSkillTypeである:

<?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()]; 
    } 
012私はUMPとき

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

:フォームとのgetData作成のための

関数ハンドル:私はます$ form->のgetData()..dataのダンプ時に

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

保存

機能nullですin function Post()in CandidateSkillHandler ..エラーがあります

$this->repository->save($candidateSkill);

+0

助けてください – devit2017

+0

コントローラを表示してください – Snegirekk

+0

@Snegirekk私の質問を更新してコントローラを追加しました。 – devit2017

答えて

0

devit2017で要求が処理されなかったためにこのフォームが機能しなかったことが判明しました。

これらの行

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

代わりの

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

この問題を解決します。

関連する問題