I have a bug in my Symphony Controller Replay

Asked

Viewed 23 times

-1

My goal is this, in my Form there is an attempt to send in one of the fields that was empty, something like this. V

Example -> https://i.stack.Imgur.com/Hiof9.gif

exepction - https://imgur.com/a/3j5JF2Y

Controler V

<?php
// src/Controller/DefaultController.php
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;

class DefaultController extends AbstractController
{
    public function index(Request $request)
            {
                $array[] = [
                    "text" => "Band"];
                $array[] = [
                    "text" => "Tour",];
                $array[] = [
                    "text" => "Contact",];
                $array[] = [
                    "text" => "Merchandaise",];

                $form = $this->createFormBuilder()
                            ->add('name', TextType::class,array(
                                'required'=>false,
                        'attr'=> ['class' => 'w3-input w3-border']))
                            ->add('email', EmailType::class,array(
                                'required'=>false,
                        'attr'=> ['class' => 'w3-input w3-border']))
                            ->add('message', TextareaType::class,array(
                                'required'=>false,
                        'attr'=> ['class' => 'w3-input w3-border']))
                            ->add('send', SubmitType::class,array(
                        'attr'=> ['class' => 'w3-input w3-border w3-button w3-black w3-section w3-right']))
                            ->getForm();

                    return $this->render('body.html.twig' , [
                    'arrays' => $array,
                    'form' => $form->createView(), 
                ]);
           }

    public function indexN2(Request $request)
        {
            $form = $this->createFormBuilder()
            ->add('name', TextType::class,array(
        'attr'=> ['class' => 'w3-input w3-border']))
            ->add('email', EmailType::class,array(
        'attr'=> ['class' => 'w3-input w3-border']))
            ->add('message', TextareaType::class,array(
        'attr'=> ['class' => 'w3-input w3-border']))
            ->add('send', SubmitType::class,array(
        'attr'=> ['class' => 'w3-input w3-border w3-button w3-black w3-section w3-right']))
            ->getForm();

            $form->handleRequest($request);

                if ($form->isSubmitted() && $form->isValid()) 
                {  

                $name = $form["name"]->getData();
                $email = $form["email"]->getData();
                $message = $form["message"]->getData();
                $error = array();

                if(!$name)
                    $error[] = 'name';

                if (count($error > 0))
                {
                return new JsonResponse(array(
                        'status' => 0,
                        'data' => $error,
                    ));
                }
                    $response = 'array';
                }    
                return new JsonResponse(array(
                        'name' => $name,
                        'email' => $email,
                        'message' => $message,
                    ));        
        }
}
``````````````


Ajax V
``````````````

  $("form").submit(function(e) {

    e.preventDefault(); // avoid to execute the actual submit of the form.

    console.log('pppppp->'+$(this).serialize())
    $.ajax({
           type: "POST",
           url: 'index2',
           data: $(this).serialize(), // serializes the form's elements.
           success: function(data)
           {
                console.log(data);
           }
         });
});


`````````````

1 answer

0


Your validation is not working because the function count also counts empty elements of an array.

$array = array("" => "");
echo count($array);
// Saída: 1

You can improve your validation in two ways.

Using the function array_filter to "filter" the array where this takes two parameters, the array and a callback.

When callback is omitted all array entries equal to FALSE will be removed.

if (count(array_filter($error) > 0)) {
    return new JsonResponse(array(
        'status' => 0,
        'data' => $error,
    ));
}

Or use the function Empty to validate an array, which in my opinion I think is more advisable.

if (!empty($error)) {
    return new JsonResponse(array(
        'status' => 0,
        'data' => $error,
    ));
}

Notice that using the empty its validation becomes less and more explicit.

  • Thanks for the help it worked :D

  • You’re welcome, I’m happy to help you! xD

Browser other questions tagged

You are not signed in. Login or sign up in order to post.