如何从api控制器中的表单中检索数据

时间:2019-07-25 12:03:49

标签: forms rest symfony postman

我正在通过symfony控制器在api中注册一名医生。为此,我为医生创建了一个表格,然后我必须将这些数据保存在数据库中。那是我无法从表单获取数据的地方,那是我对邮递员尝试的结果,但是它向我返回500错误,提示:

  

子级\“用户名\”不存在。

我正在尝试通过以下查询恢复表单数据:

  

$ user-> setUsername($ form ['username']);

控制器

  /**
 * @Route("/api/inscription/medecin")
 * @Rest\View(statusCode=Response::HTTP_CREATED)
 * @Method("POST")
 */
   public function postDocAction(Request $request){

     $medecin = new Medecin();
    $user = new User();
    $user->setSalt('');
    $form = $this->createForm('Doctix\MedecinBundle\Form\MedecinType', $medecin);

    $form->submit($request->request->all()); // Validation des données

    if ($form->isValid()){
     $encoder = $this->get('security.password_encoder');
     $encoded = $encoder->encodePassword($user, $user->getPlainPassword());
        $user->setPassword($encoded);
        $user->setUsername($form['username']);
        $user->setRoles(array('ROLE_MEDECIN'));

        $user->setNom($form['nom']);
        $user->setPrenom($form['prenom']);
        $user->setNumTel($form['numTel']);
        $user->setAdresse($form['adresse']);

        $medecin->setUser($user);
        $medecin->setSexe($form['sexe']);
        $medecin->setQuartier($form['quartier']);
        $medecin->setNumOrdre($form['numordre']);
        $medecin->setSpecialite($form['specialite']);
        $medecin->setClinique($form['clinique']);

        $em = $this->getDoctrine()->getManager();
        $em->persist($medecin);
        $em->flush();
  }
   }

表格

   class MedecinType extends AbstractType
{
/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('user',UserType::class)
        ->add('sexe',  TextType::class)
        ->add('specialite',SpecialiteType::class)
        ->add('clinique',CliniqueType::class)
        ->add('quartier',  TextType::class)
        ->add('numordre',  TextType::class)
    ;
}

/**
 * @param OptionsResolver $resolver
 */
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'Doctix\MedecinBundle\Entity\Medecin',
        'csrf_protection' => false
    ));
}
}

邮递员测试

registertest

2 个答案:

答案 0 :(得分:0)

完成$form->handleRequest($request);之后, $form->getData()保存提交的值。

但是,原始$medecin变量也已更新。因此,无需再次设置所有字段。

https://symfony.com/doc/current/forms.html#handling-form-submissions

答案 1 :(得分:0)

简短答案: 根据json,用户似乎是一个对象,因此访问username时,您可能应该这样写:

  

$ form [“ user”] [“ username”];   您应该对用户内部的所有元素应用相同的逻辑。

完整答案: 您的代码中存在多个不一致的地方。将对象传递到createForm()方法的优点是,一旦提交表单并使其有效,该对象将自动更新。这样可以避免您像现在这样一遍遍所有的二传手。

因此,如果您希望将用户对象正确地更新为医学对象,则应在创建表单之前设置用户:

<?php
 /**
 * @Route("/api/inscription/medecin")
 * @Rest\View(statusCode=Response::HTTP_CREATED)
 * @Method("POST")
 */
 public function postDocAction(Request $request){

    $medecin = new Medecin();
    $user = new User();
    $user->setSalt('');
    $user->setRoles(['ROLE_MEDECIN']);
    $medecin->setUser($user);

    $form = $this->createForm('Doctix\MedecinBundle\Form\MedecinType', $medecin);
    $form->submit($request->request->all()); // Validation des données

    if ($form->isValid()) {
     $encoder = $this->get('security.password_encoder');
     $encoded = $encoder->encodePassword($user, $user->getPlainPassword());
     $user->setPassword($encoded);

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

     $em->persist($user); // Safety, as i don't know how you configured your entities
     $em->persist($medecin);
     $em->flush();
   }
 }