0

Symfony 2, Following Code:

namespace frontend\mainBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\Collection;

class ContactForm extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('subject', 'text');
        $builder->add('email', 'email');
        $builder->add('message', 'textarea');
    }

    public function getName()
    {
        return 'contact';
    }
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $collectionConstraint = new Collection(array(
            'name' => new Length(array("min" => 5)),
            'email' => new Email(
                array('message' => 'Invalid email address')
            ),
            'message' => new Length(array("min" => 5))
        ));

        $resolver->setDefaults(array(
            'constraints' => $collectionConstraint
        ));
    }
}

DefaultController.php

    public function contactAction(Request $request)
{


  $form = $this->createForm(new ContactForm());  
if ($request->getMethod() == 'POST') {
    $form->bindRequest($request);

    if ($form->isValid()) {
        // perform some action, such as saving the task to the database
    break;
        //return $this->redirect($this->generateUrl('task_success'));
    }
}
return $this->render('frontendmainBundle:Default:contact.html.php', array(
    'form' => $form->createView()
));
}

contact.html.php

<form action="contact" method="post" <?php echo $view['form']->enctype($form) ?> >
    <?php echo $view['form']->widget($form) ?>

    <input type="submit" />
</form>

When I submit the form, with subject, email, and message ('test', '[email protected]', 'message text'), following error is coming up.

This field is missing. Subject This field was not expected. (I guess it is the email box)

I do have actually an error message in my code ('Invalid email address') This is not working too. Why?

7
  • How do you render your form in your template?
    – tamir
    Dec 24, 2012 at 15:20
  • return $this->render('frontendmainBundle:Default:contact.html.php', array( 'form' => $form->createView() )); Why?
    – craphunter
    Dec 24, 2012 at 22:54
  • I mean how does your contact.html.php look like? can you post some code?
    – tamir
    Dec 25, 2012 at 13:17
  • Ah, okay, I updated my code. Hope it helps
    – craphunter
    Dec 25, 2012 at 14:15
  • 1
    in setDefaultOptions you have a constraint for name, is it meant to be for subject?
    – tamir
    Dec 25, 2012 at 14:36

1 Answer 1

1

In setDefaultOptions you have a constraint for name instead of subject

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.