我一直在寻找这个简单问题的答案,但我找不到合适的解决方案。只有两个字段,我的主要目的是显示相应字段的验证错误。
我的实体类:
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity
* @ORM\Table(name="sample")
*/
class Sample
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
private $id;
/**
* @Assert\NotBlank()
* @ORM\Column(type="string")
*/
private $first_name;
/**
* @ORM\Column(type="string")
*/
private $last_name;
/**
* @Assert\NotBlank()
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @return mixed
*/
public function getFirstName()
{
return $this->first_name;
}
/**
* @param mixed $first_name
*/
public function setFirstName($first_name)
{
$this->first_name = $first_name;
}
/**
* @return mixed
*/
public function getLastName()
{
return $this->last_name;
}
/**
* @param mixed $last_name
*/
public function setLastName($last_name)
{
$this->last_name = $last_name;
}
}
我的表格类:
class Sampleform extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('firstName')
->add('lastName');
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(['data_class' => Sample::class]);
}
public function getBlockPrefix()
{
return 'app_bundle_sampleform';
}
}
我的控制器功能:
public function indexAction(Request $request)
{
$form = $this->createForm(Sampleform::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
return new Response("Same");
}
return $this->render('default/index.html.twig', ['form' => $form->createView()]);
}
我在twig视图中迷失了,因为当我尝试下面的代码时,它显示错误,而另一个twig视图不会呈现错误消息,即使调试工具栏检测到错误。
My Twig View(3行表格渲染):
{% block body %}
{{ form_start(form) }}
{{ form_widget(form) }}
<button class="btn btn-info" >Save</button>
{{ form_end(form) }}
{% endblock %}
My Twig View(个别字段):
{% extends 'base.html.twig' %}
{% block stylesheets %}
<link rel="stylesheet" href="{{ asset('bootstrap/css/bootstrap.css') }}">
{% endblock %}
{% block body %}
{{ form_start(form) }}
{{ form_label(form.lastName) }}
{{ form_errors(form.lastName) }}
{{ form_widget(form.lastName) }}
{{ form_label(form.lastName) }}
{{ form_errors(form.lastName) }}
{{ form_widget(form.lastName) }}
<button class="btn btn-info" >Save</button>
{{ form_end(form) }}
{% endblock %}
Bootstrap版本是4 BETA 2 已配置的树枝形式主题
答案 0 :(得分:0)
现在正在运作。问题出在我的bootstrap css
上