我正在尝试验证联系表单上的字段,它本身的表单工作正常并且正在发布数据,但是我一直在验证字段。根据这个文档,设置验证似乎很简单,但由于某种原因,它对我不起作用。
当我创建捆绑包时,捆绑包的配置文件夹中没有validation.yml
,所以我手动创建了它,这就是我的validation.yml
样子,它现在非常基本。
ClickTeck\ContactBundle\Entity\Enquiry:
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 ClickTeck\ContactBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use ClickTeck\ContactBundle\Entity\Enquiry;
use ClickTeck\ContactBundle\Form\EnquiryType;
use Symfony\Component\HttpFoundation\Request;
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()
));
}
}
这是我的Enquiry.php
内部Entity
文件夹
<?php
namespace ClickTeck\ContactBundle\Entity;
use Doctrine\ORM\Mapping 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;
}
}
这是EnquiryType.php
内部Entity
文件夹
namespace ClickTeck\ContactBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
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';
}
}
任何 最后我的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 %}
当我提交带有空名称字段的表单时,我没有收到任何错误,如果有人能指出我正确的方向,我将不胜感激。