読み込みが終了しない場合は、しばらく待つか、リロードを行なってください。
If loading does not finish, wait for a while or reload.
エンジニア向けの情報を発信するブログです。
どなたでも発信できます。
お好きに利用していただれば幸いです。
📁 root/ec-cube/app/template/default/Hoge/index.twig {% extends 'default_frame.twig' %} {% block main %} <div class="ec-layoutRole__main"> <form method="POST", action="{{url('hoge')}}"> <input type="text" name="name"/> {% for error in errors %} {% if error.propertyPath is same as ('name') %}{# 👈 errorsの中にpropertyPathが'name'の奴の場合表示する処理 #} <div>{{error.message}}</div> {% endif %} {% endfor %} <input type="submit" value="送信"/> </form> </div> {% endblock %}
📁 root/ec-cube/app/Customize/Controller/HogeController.php <?php namespace Customize\Controller; use Eccube\Controller\AbstractController; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Validator\Validator\ValidatorInterface; use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\Validator\ConstraintViolationList; use Symfony\Component\Validator\Context\ExecutionContextInterface; class HogeController extends AbstractController { private $validator; public function __construct( ValidatorInterface $validator ) { $this->validator = $validator; } /** * @Route("/hoge", name="hoge", methods={"GET", "POST"}) * @Template("Hoge/index.twig") */ public function index(Request $request) { if ($request->getMethod() === 'POST') { $name = $request->get('name'); $errors = $this->validator->validate( $name, [ new Assert\Callback(function ($name, ExecutionContextInterface $context) { if ($name === null || $name === '') { $context->buildViolation('空やんねこれ') ->atPath('name')// 👈 なんの値を検証したかセットできる ->addViolation(); } }), ] ); } return [ 'errors' => $errors ?? [], ]; } }
<?php namespace Customize\Controller; use Eccube\Controller\AbstractController; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Validator\Validator\ValidatorInterface; use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\Validator\ConstraintViolationList; use Symfony\Component\Validator\Context\ExecutionContextInterface; class HogeController extends AbstractController { private $validator; public function __construct( ValidatorInterface $validator ) { $this->validator = $validator; } /** * @Route("/hoge", name="hoge", methods={"GET", "POST"}) * @Template("Hoge/index.twig") */ public function index(Request $request) { if ($request->getMethod() === 'POST') { $name = $request->get('name'); // 👇 こんな感じでpropertyPathをセットできる $errors = $this->validator->startContext()->atPath('name')->validate( $name, [ new Assert\NotBlank(), ] )->getViolations();// 👈 これ書かないと別のインスタンスが取得されるので書いとく } return [ 'errors' => $errors ?? [], ]; } }
->getViolations();
Symfony\Component\Validator\ConstraintViolationList
Symfony\Component\Validator\Validator\RecursiveContextualValidator
{% for error in errors.violations %}{# 👈 こう書けば以下の処理が正常に作動する #} {% if error.propertyPath is same as ('name') %} <div>{{error.message}}</div> {% endif %} {% endfor %}
<?php namespace Customize\Controller; use Eccube\Controller\AbstractController; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Validator\Validator\ValidatorInterface; use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\Validator\ConstraintViolationList; use Symfony\Component\Validator\Context\ExecutionContextInterface; class HogeController extends AbstractController { private $validator; public function __construct( ValidatorInterface $validator ) { $this->validator = $validator; } /** * @Route("/hoge", name="hoge", methods={"GET", "POST"}) * @Template("Hoge/index.twig") */ public function index(Request $request) { if ($request->getMethod() === 'POST') { $name = $request->get('name'); // 👇 'name'をセット $errors = $this->validator->startContext()->atPath('name')->validate( $name, [ new Assert\Callback(function ($name, ExecutionContextInterface $context) { if ($name === null || $name === '') { $context->buildViolation('空やんねこれ') ->atPath('name_osu')// 👈 こっちにもセットしてみる ->addViolation(); } }), ] )->getViolations(); } return [ 'errors' => $errors ?? [], ]; } }