Ricardo Raz Ricardo Raz - 7 months ago 217
Twig Question

Symfony 3 - Form validation error fields not displaying

I've been looking for answer for this simple question but I can't find a working solution. There are only two fields and my main purpose is just to display the validation errors by the respective fields.

My Entity Class:

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;


/**
* @ORM\Entity
* @ORM\Table(name="sample")
*/
class Sample
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
private $id;

/**
* @Assert\NotBlank()
* @ORM\Column(type="string")
*/
private $first_name;

/**
* @ORM\Column(type="string")
*/
private $last_name;

/**
* @Assert\NotBlank()
* @return mixed
*/
public function getId()
{
return $this->id;
}

/**
* @return mixed
*/
public function getFirstName()
{
return $this->first_name;
}

/**
* @param mixed $first_name
*/
public function setFirstName($first_name)
{
$this->first_name = $first_name;
}

/**
* @return mixed
*/
public function getLastName()
{
return $this->last_name;
}

/**
* @param mixed $last_name
*/
public function setLastName($last_name)
{
$this->last_name = $last_name;
}


}


My Form Class:

class Sampleform extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('firstName')
->add('lastName');
}

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

public function getBlockPrefix()
{
return 'app_bundle_sampleform';
}
}


My Controller Function:

public function indexAction(Request $request)
{
$form = $this->createForm(Sampleform::class);

$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
return new Response("Same");
}

return $this->render('default/index.html.twig', ['form' => $form->createView()]);
}


I am lost in the twig view since when I try the code below it shows the errors while the other twig view doesn't render the error messages even though the debug toolbar detects the error.

My Twig View (3-lines form render):

{% block body %}
{{ form_start(form) }}
{{ form_widget(form) }}
<button class="btn btn-info" >Save</button>
{{ form_end(form) }}
{% endblock %}


My Twig View (individual fields):

{% extends 'base.html.twig' %}

{% block stylesheets %}
<link rel="stylesheet" href="{{ asset('bootstrap/css/bootstrap.css') }}">
{% endblock %}

{% block body %}
{{ form_start(form) }}

{{ form_label(form.lastName) }}
{{ form_errors(form.lastName) }}
{{ form_widget(form.lastName) }}

{{ form_label(form.lastName) }}
{{ form_errors(form.lastName) }}
{{ form_widget(form.lastName) }}

<button class="btn btn-info" >Save</button>
{{ form_end(form) }}
{% endblock %}


Bootstrap version is 4 BETA 2
Twig form themes already configured

Answer Source

It is working now. The issue is on my bootstrap css

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download