Symfony 2.5中的表单验证



我正在尝试验证联系人表单上的字段,表单本身运行良好,并且正在发布数据,但我一直在验证字段。根据这份文件,它似乎非常直接地进行设置验证,但由于某种原因,它对我不起作用

当我创建捆绑包时,捆绑包的配置文件夹中没有validation.yml,所以我手动创建了它,这就是我的validation.yml的样子,它现在非常基本。

ClickTeckContactBundleEntityEnquiry:
    properties:
        name:
            - NotBlank: ~
            - Length:
                min: 2
                max: 50
                minMessage: "Your first name must be at least {{ limit }} characters long"
                maxMessage: "Your first name cannot be longer than {{ limit }} characters long"

这就是DefaultController看起来像

namespace ClickTeckContactBundleController;
use SymfonyBundleFrameworkBundleControllerController;
use ClickTeckContactBundleEntityEnquiry;
use ClickTeckContactBundleFormEnquiryType;
use SymfonyComponentHttpFoundationRequest;
class DefaultController extends Controller
{
    public function indexAction(Request $request)
    {
        $enquiry = new Enquiry();
        $form = $this->createForm(new EnquiryType(), $enquiry);
            $form->handleRequest($request);
            if ($form->isValid()) {
                return $this->redirect($this->generateUrl('contact_form'));
            }
        return $this->render('ContactBundle:Default:contact.html.twig', array(
                'form' => $form->createView()
            ));
    }
}

这是我在Entity文件夹中的Enquiry.php

<?php
namespace ClickTeckContactBundleEntity;
use DoctrineORMMapping as ORM;
/**
 * Enquiry
 */
class Enquiry
{
    /**
     * @var integer
     */
    private $id;
    /**
     * @var string
     */
    private $name;
    /**
     * @var string
     */
    private $email;
    /**
     * @var string
     */
    private $subject;
    /**
     * @var string
     */
    private $body;

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }
    /**
     * Set name
     *
     * @param string $name
     * @return Enquiry
     */
    public function setName($name)
    {
        $this->name = $name;
        return $this;
    }
    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }
    /**
     * Set email
     *
     * @param string $email
     * @return Enquiry
     */
    public function setEmail($email)
    {
        $this->email = $email;
        return $this;
    }
    /**
     * Get email
     *
     * @return string 
     */
    public function getEmail()
    {
        return $this->email;
    }
    /**
     * Set subject
     *
     * @param string $subject
     * @return Enquiry
     */
    public function setSubject($subject)
    {
        $this->subject = $subject;
        return $this;
    }
    /**
     * Get subject
     *
     * @return string 
     */
    public function getSubject()
    {
        return $this->subject;
    }
    /**
     * Set body
     *
     * @param string $body
     * @return Enquiry
     */
    public function setBody($body)
    {
        $this->body = $body;
        return $this;
    }
    /**
     * Get body
     *
     * @return string 
     */
    public function getBody()
    {
        return $this->body;
    }
}

这是Entity文件夹中的EnquiryType.php

namespace ClickTeckContactBundleForm;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
class EnquiryType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name');
        $builder->add('email', 'email');
        $builder->add('subject');
        $builder->add('body', 'textarea');
    }
    public function getName()
    {
        return 'contact';
    }
}

任意Finally我的contact模板

{% extends '::base.html.twig' %}
{% block title %}Contact{% endblock %}
{% block body %}
    <div class="container">
        <div class="row">
            <header>
                <h1>Contact Us</h1>
            </header>
            {{ form_errors(form) }}
            <form action="{{ path('contact_form') }}" method="post" {{ form_enctype(form) }} role="form"
                  class="form-horizontal">
                {{ form_errors(form) }}
                <div class="form-group">
                    <div class="col-sm-2 control-label">
                        {{ form_label(form.name) }}
                    </div>
                    <div class="col-sm-10">
                        {{ form_widget(form.name, { 'attr': {'class': 'form-control'} }) }}
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-2 control-label">
                        {{ form_label(form.email) }}
                    </div>
                    <div class="col-sm-10">
                        {{ form_widget(form.email, { 'attr': {'class': 'form-control'} }) }}
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-2 control-label">
                        {{ form_label(form.subject) }}
                    </div>
                    <div class="col-sm-10">
                        {{ form_widget(form.subject, { 'attr': {'class': 'form-control'} }) }}
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-2 control-label">
                        {{ form_label(form.body) }}
                    </div>
                    <div class="col-sm-10">
                        {{ form_widget(form.body, { 'attr': {'class': 'form-control', 'rows':'10'} }) }}
                    </div>
                </div>
                {{ form_rest(form) }}
                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <input type="submit" value="Send Message" class="btn btn-primary"/>
                    </div>
                </div>
            </form>
        </div>
    </div>
{% endblock %}

当我提交带有空名称字段的表格时,我没有收到任何错误,如果有人能为我指明正确的方向,我将不胜感激。

这就是解决我的问题的原因,在app/config/config.yml中,默认情况下验证似乎被关闭,将其更改为以下内容,所有验证都有效。

framework:
    validation:      { enabled: true, enable_annotations: false }

相关内容

  • 没有找到相关文章

最新更新