The actual form for me to fill won’t show; Symfony using Mongodb, JSON Response

I’ve create an API to create an article and i built the form in ArticleType.php. I’m testing my API using postman. It works, I can Create an article and all that but the form won’t show. I have to type my article info as raw for it to take them. And Im using Json Response

My ArticleAPIController:

class ArticleAPIController extends AbstractController
{

    private $documentManager;
    public function __construct(DocumentManager $documentManager)
    {
        $this->documentManager = $documentManager;
    }

        
    /**
     * @Route("api/article/create", name="CreateArticle",  methods={"GET", "POST"})
     * @param DocumentManager $dm
     * @param Request $request
     * @return RedirectResponse|Response
     * @throws MongoDBException
     */
    public function addArticle(Request $request)
    {
        $response = [];

        $form = $this->
        createForm(ArticleType::class, new Article() ,array('csrf_protection' => false));  
        

        $request = json_decode($request->getContent(), true);
        
        $form->submit($request);

        if($form->isSubmitted() && $form->isValid())
        {
        
           $article = $form->getData();
           $this->documentManager->persist($article);
           $this->documentManager->flush();
           
           $status = 200;
           $response = ["status" => $status, "success" => true, "data" => $article->getId()];
           // return $this->redirectToRoute('article_list'); 
       }

       else{
           $status = 422;
           $response = ["status" => $status, "success" => false, "errors" => FormErrorsSerializer::serializeFormErrors($form)];
       }

       return new JsonResponse($response, $status);
   }

ArticleType.php

class ArticleType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('title', TypeTextType::class);
        $builder->add('content', TextareaType::class);
        $builder->add('conclusion', TextareaType::class);
        $builder->add('references', TextareaType::class);
        $builder->add('category', DocumentType::class, ['class' => ArticleCategory::class,
            'choice_label' => 'title']);
        $builder->add('user', DocumentType::class, ['class' => User::class,
            'choice_label' => 'firstName']);
        $builder->add('relatedArticles', DocumentType::class, ['class' => Article::class,
            'choice_label' => 'title']);
        $builder->add('tags', DocumentType::class, ['class' => Tags::class,
            'choice_label' => 'name',]);
        $builder->add('published');
        $builder->add('save', SubmitType::class,array('label' => 'Create',
            'attr' => array('class' => 'btn btn-primary mt-5')));
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(['data_class' => Article::class]);
    }
}

look at the results of postman, no form shows:
enter image description here
THANK YOU

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *