使用put方法进行表单验证symfony

时间:2018-02-15 09:15:49

标签: php symfony

现在差不多3天找到关于如何在symfony中处理表单验证的答案。我有一个患者的编辑表格。所以我使用put方法来编辑功能,这就是我现在所拥有的

/**
     * @Route("/patients/{id}")
     * @param Request $request
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function putAction(Request $request)
    {

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

        $patient = $em->getRepository('AppBundle:Patient')->find(819);

        $form = $this->createForm(PatientProfileFormType::class, $patient);

        $form->submit($request->get("patient"));

        return $this->jsonResponse($form->getErrors(true), Response::HTTP_BAD_REQUEST);
    }

这是我的表单的请求参数

{
    "patient":{
        "address": {
            "city":  340,
            "country": 235,
            "state":   197,
            "street":  "fsdf",
            "street2": "fdsfs",
            "zipCode": 22222
        },
        "birthDate":   "1998-03-15",
        "contactNumbers": {
            "0":{
              "number":  124222
            }
        },
        "email":   "samplex@gmail.com",
        "firstName": "www",
        "gender":  1,
        "generalNote": "",
        "lastName":    "aww"
    }
}

firstName和lastName是我表单中的必填字段。我使用表单类型来获取数据并进行验证,这里是表单类型代码。我将文件命名为PatientProfileFormType

public function buildForm(FormBuilderInterface $builder, array $options)
    {

        $builder
            ->add(
                'firstName',
                null,
                array(
                    "constraints" => [
                        new NotBlank(["message" => "First name is required"]),
                    ],
                )
            )
            ->add(
                'lastName',
                null,
                [
                    "constraints" => [
                        new NotBlank(["message" => "Last name is required"]),
                    ],
                ]
            )
            ->add(
                'email',
                EmailType::class,
                [
                    "constraints" => [
                        new NotBlank(["message" => "Email is required"]),
                        new Email(['message' => 'Invalid email address.'])
                    ],
                ]
            )
            ->add('generalNote', TextareaType::class)
            ->add('address', AddressType::class)
            ->add("birthDate", TextareaType::class)
            ->add("gender")
            ->add(
                'contactNumbers',
                CollectionType::class,
                [
                    'entry_type' => ContactNumberType::class,
                    'allow_add' => true,
                ]
            );
    }

当我在firstName和lastName中提交带有空字段的表单时,我收到了错误消息,指出这些字段是必需的,但是当我检查数据库时。它提交空字段firstName和lastName。当我刷新页面时。我的firstName和lastName已经为空。我想知道我的表单发生了什么,以及为什么我在put方法中遇到这种问题。我的设置中是否会遗漏某些内容?

更新 这是我的患者实体代码

/**
     * @var string $firstName
     *
     * @Groups({"default", "initial","chat", "apiDefault", "core"})
     * @ORM\Column(name="first_name", type="string", length=255)
     */
    protected $firstName;

    /**
     * @var string $generalNote
     *
     * @Groups({"default", "initial", "apiDefault", "core"})
     * @ORM\Column(name="general_note", type="string", length=255, nullable=true)
     */
    protected $generalNote;

    /**
     * @var string $lastName
     *
     * @Groups({"default", "initial","chat", "apiDefault", "core"})
     * @ORM\Column(name="last_name", type="string", length=255)
     */
    protected $lastName;

我的代码没有任何特别之处。我不明白为什么它更新我的数据库中的字段

0 个答案:

没有答案