开发者

server validation doesn't work

I have contact form, subject should have minimum 5 chars but when I write in this field one char and submit form, I see no errors, why, what is wrong?

WebsiteController.php:

namespace Acme\WebsiteBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

use Acme\WebsiteBundle\Contact\ContactForm;
use Acme\WebsiteBundle\Contact\ContactRequest;


class WebsiteController extends Controller
{ 
    public function contactAction(Request $request)
    { 
        $contactRequest = new ContactRequest();
        $form = $this->createForm(new ContactForm(), $contactRequest);

        if ($request->getMethod() == 'POST') 
        {
            $form->bindRequest($request);
            if ($form->isValid()) 
            {
                $contactRequest->send();

                $this->get('session')->setFlash('notice', 'Email is ok.');
                $this->redirect($this->generateUrl('AcmeWebsiteBundle_homepage'));
            }
            else
            {
                $this->get('session')->setFlash('notice', 'Email is wrong.');
            }
        }

        return $this->render('AcmeWebsiteBundle:Website:contact.html.php', array('form' => $form->createView()));        
    }
}

ContactForm.php:

namespace Acme\WebsiteBundle\开发者_StackOverflow社区Contact;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;


class ContactForm extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder->add('sender', 'email', array('label' => 'Email', 'trim' => true, 'required' => true, 'max_length' => 50));
        $builder->add('subject', 'text', array('label' => 'Temat', 'trim' => true, 'required' => true, 'max_length' => 100));
        $builder->add('message', 'textarea', array('label' => 'Wiadomość', 'trim' => true, 'required' => true, 'max_length' => 1000));
    }

    public function getDefaultOptions(array $options)
    {
        return array(
            'data_class' => 'Acme\WebsiteBundle\Contact\ContactRequest',
        );
    }

    public function getName()
    {
        return 'contact';
    }
}

ContactRequest.php:

namespace Acme\WebsiteBundle\Contact;

class ContactRequest
{
    /**
     * @validation:MinLength(5)
     * @validation:MaxLength(100)
     * @validation:NotBlank
     */
    protected $subject;

    /**
     * @validation:MinLength(5)
     * @validation:MaxLength(1000)
     * @validation:NotBlank
     */
    protected $message;

    /**
     * @validation:MaxLength(50)
     * @validation:Email
     * @validation:NotBlank
     */
    protected $sender;



    public function setSubject($subject)
    {
        $this->subject = $subject;
    }

    public function getSubject()
    {
        return $this->subject;
    }

    public function setMessage($message)
    {
        $this->message = $message;
    }

    public function getMessage()
    {
        return $this->message;
    }

    public function setSender($sender)
    {
        $this->sender = $sender;
    }

    public function getSender()
    {
        return $this->sender;
    }



    public function send()
    {
         //.......
    }
}


I think you're using doctrine native validation(?), whereas in symfony2 you need to use @Assert for that task. http://symfony.com/doc/current/book/validation.html

Basically add Assert namespace and then replace @validation: with @Assert\

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜