0
私はこの問題の解決策を探していました。私はStackoverflowに関する多くの質問を見つけ、ほとんどすべてを試みましたが、私の問題で私を助けていません。Symfonyフォームの検証と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 %}
buy.html.twig
/**
* @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;
}
}
フォームが正しくレンダリングされます。しかし、私がそれを提出すると、検証メッセージがページに表示されることを期待していますが、表示されません。
$errors = $form->getErrors(true, false);
dump($errors);
これは、妥当性検査のエラーが翻訳されたことを示しています。しかし、私はここに何が欠けているので、それが表示に表示されないようにしています
ありがとう後
「フォームの割り当てがある必要があります。我々はまったく同じ問題を抱えていた –