-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);
}
});
});
`````````````
Thanks for the help it worked :D
– david.bandarra
You’re welcome, I’m happy to help you! xD
– Victor Carnaval