Bissap
Bissap

Reputation: 551

form whith createForm doesn't work and i can't get assert message

I already have to ask a question about a similar assert problem with FosUserBundle configuation.

But now I'm trying to run a simple form, but even with a esiest form => nothing is happening.

when i click on submit :

1) isValid() stay to false

2) No Assert message appears when input name is blank/empty

UserTmp.php (entity)

<?php

namespace BISSAP\UserBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * BISSAP\UserBundle\Entity\User
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="BISSAP\UserBundle\Entity\UserRepository")
 */
class Usertmp 
{

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(name="name", type="string", length=255)
     *
     * @Assert\NotBlank(message="Please enter your name.", groups={"Registration", "Profile"})
     * @Assert\Length(
     *     min=3,
     *     max=255,
     *     minMessage="The name is too short.",
     *     maxMessage="The name is too long.",
     *     groups={"Registration", "Profile"}
     * )
     */
    private $name;



    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return Usertmp
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }
}

UserType.php

<?php

namespace BISSAP\ForumBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class UserType extends AbstractType
{


     /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
            $builder
                    ->add('name', 'text', array('required' => false))
                    ->add('Envoyer', 'submit', array(
                        'attr' => array(
                        'class' => 'btn right-flt'
                        )));


    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'BISSAP\UserBundle\Entity\Usertmp'
        ));
    }

    public function configureOptions(OptionsResolver $resolver) 
    { 
        $resolver->setDefaults(array( 'data_class' => $this->class, 'intention' => 'Registration', )); 
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'bissap_forumbundle_user';
    }
}

TController.php

<?php

namespace BISSAP\ForumBundle\Controller;

use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use BISSAP\ForumBundle\Form\UserType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use BISSAP\UserBundle\Entity\Usertmp;

class TController extends Controller
{

  public function indexAction()
  {
    $entity = new Usertmp();
    $form = $this->createForm(new UserType(), $entity);

    if ($form->isValid())
    {
        return $this->redirectToRoute('bissap_forum_index');
    }

    return $this->render('BISSAPForumBundle:T:index.html.twig', array('form'=> $form->createView(), 'errors_tmp' => $this->getErrorMessages($form)));
  }

   private function getErrorMessages(\Symfony\Component\Form\Form $form) 
   {      
        $errors = array();
        foreach ($form->getErrors(true, false) as $error) {
        // My personnal need was to get translatable messages
        // $errors[] = $this->trans($error->current()->getMessage());
        $errors[] = $error->current()->getMessage();
        }

        return $errors;
    }

}
?>

index.html.twig

----> {{form( form )}}
{% for error in errors_tmp %}
    <div>error : {{ error }}</div>
{% endfor %} 

Upvotes: 0

Views: 398

Answers (2)

Bissap
Bissap

Reputation: 551

So, form didn't work cause : $form->handleRequest($request); missed in TController.php

Upvotes: 1

Victor Sanchez
Victor Sanchez

Reputation: 983

In your UserType.php, try:

$resolver->setDefaults(array( 'data_class' => $this->class, 'intention' => 'Registration', 'validation_groups' => array('registration'),)); 

You can also set the validation group in your TController.php instead of hard-coding it on your UserType:

$form = $this->createForm(new UserType(), $entity, array('validation_groups' => 'registration'));

Source: Validation Groups

Also in your UserType.php you are saying that the name is not required, but at the same time you want to assert if it is not blank:

->add('name', 'text', array('required' => false))

Try removing that option too.

Upvotes: 0

Related Questions