我一直在寻找这个问题的解决方案。我在Stackoverflow上发现了很多问题并尝试了几乎所有问题,但它并没有帮助我解决我的问题。
我在控制器和树枝视图中有一个表单,它将呈现表单。我还有一个验证实体。
/**
* @Route("/buy", name="buy")
*/
public function buyAction(Request $request)
{
$priceArray = $this->container->getParameter('price');
$options = [];
foreach ($priceArray as $billingCycle => $array) {
$options[$array['description'].' $'.$array['price']] = $billingCycle;
}
$orderData = new OrderData();
// $orderData->setNickName('Enter Server NickName');
$form = $this->createFormBuilder($orderData)
->add(
'billingCycle',
ChoiceType::class,
array(
'choices' => $options,
'choices_as_values' => true,
'expanded' => true,
'label' => 'Select Billing Cycle',
)
)
->add('nickName', TextType::class, ['label' => 'Nick name for your server'])
->add('ipAddress', TextType::class, ['label' => 'IP Address of your server'])
->add(
'save',
SubmitType::class,
array(
'label' => 'Proceed to Payment',
'attr' => [
'formnovalidate' => 'formnovalidate',
],
)
)
->getForm();
$options = [
'priceArray' => $priceArray,
'form' => $form->createView(),
];
$form->handleRequest($request);
if ($form->isSubmitted()) {
if ($form->isValid()) {
die('form is valid');
} else {
$errors = $form->getErrors(true, false);
dump($errors);
exit;
}
}
return $this->render('default/buy.html.twig', $options);
}
的appbundle /实体/ OrderData.php
<?php
namespace AppBundle\Entity;
use Symfony\Component\Validator\Constraints as Assert;
class OrderData
{
/**
* @Assert\NotBlank(message="missing_billingcycle");
*/
protected $billingCycle;
/**
* @Assert\NotBlank(message="missing_nickname")
*/
protected $nickName;
/**
* @Assert\NotBlank(message="missing_ipaddress");
* @Assert\Ip(message="invalid_ipaddress");
*/
protected $ipAddress;
/**
* @return mixed
*/
public function getBillingCycle()
{
return $this->billingCycle;
}
/**
* @param mixed $billingCycle
*/
public function setBillingCycle($billingCycle)
{
$this->billingCycle = $billingCycle;
}
/**
* @return mixed
*/
public function getIpAddress()
{
return $this->ipAddress;
}
/**
* @param mixed $ipAddress
*/
public function setIpAddress($ipAddress)
{
$this->ipAddress = $ipAddress;
}
/**
* @return mixed
*/
public function getNickName()
{
return $this->nickName;
}
/**
* @param mixed $nickName
*/
public function setNickName($nickName)
{
$this->nickName = $nickName;
}
}
buy.html.twig
{% extends 'base.html.twig' %}
{% block body %}
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">Purchase</h3>
</div>
<div class="panel-body">
{{ form_start(form) }}
{{ form_row(form.billingCycle) }}
{{ form_row(form.nickName) }}
{{ form_row(form.ipAddress) }}
{{ form_end(form) }}
</div>
</div>
{% endblock %}
表单呈现正确。但是,如果我提交它,我希望验证消息在页面上可见,但它不可见。
$errors = $form->getErrors(true, false);
dump($errors);
这向我展示了翻译的验证错误。但是,我不太清楚我在这里缺少什么,所以它没有在视图上显示
答案 0 :(得分:2)
$options = [
'priceArray' => $priceArray,
'form' => $form->createView(),
];
$form->handleRequest($request);
&#39;形式&#39;赋值必须在handleRequest之后
$form->handleRequest($request);
$options = [
'priceArray' => $priceArray,
'form' => $form->createView(),
];