読み込みが終了しない場合は、しばらく待つか、リロードを行なってください。
If loading does not finish, wait for a while or reload.
エンジニア向けの情報を発信するブログです。
どなたでも発信できます。
お好きに利用していただれば幸いです。
2023-10-02追記
まとめ方でもっといい方法わかったので「やり方」に追記しました。
📁 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"/> <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; 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'); // 👇 これ $notBlankErrors = $this->validator->validate( $name, [ new Assert\NotBlank(), ] ); // 👇 これ $greaterThanErrors = $this->validator->validate( $name, [ new Assert\GreaterThan(4), ] ); // 👇 これたち if ($notBlankErrors->count() > 0 || $greaterThanErrors->count() > 0) { throw new \Exception(); } } return [ ]; } }
$name = $request->get('name'); // 👇 これ $errors = $this->validator->validate( $name, [ new Assert\NotBlank(), new Assert\GreaterThan(4), ] ); if ($errors->count() > 0) { throw new \Exception(); }
<?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;// 👈 追加 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'); $notBlankErrors = $this->validator->validate( $name, [ new Assert\NotBlank(), ] ); $greaterThanErrors = $this->validator->validate( $name, [ new Assert\GreaterThan(4), ] ); // 👇 これでまとめられる $errors = new ConstraintViolationList([...$notBlankErrors, ...$greaterThanErrors]); if ($errors->count() > 0) { throw new \Exception(); } } return [ ]; } }
<?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;// 👈 追加 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'); $main_errors = new ConstraintViolationList();// 👈 ConstraintViolationListをnewする(下の$notBlankErrorsでもいいけどとりあえず) $notBlankErrors = $this->validator->validate( $name, [ new Assert\NotBlank(), ] ); // 👇 これでもまとめられる(追加できる) foreach ($notBlankErrors as $notBlankError) { $main_errors->add($notBlankError); } $greaterThanErrors = $this->validator->validate( $name, [ new Assert\GreaterThan(4), ] ); // 👇 上記に同じ foreach ($greaterThanErrors as $greaterThanError) { $main_errors->add($greaterThanError); } if ($errors->count() > 0) { throw new \Exception(); } } return [ ]; } }
<?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; 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'); $notBlankErrors = $this->validator->validate( $name, [ new Assert\NotBlank(), new Assert\Type("alnum"),// 👈 こいつを追加 ] ); $greaterThanErrors = $this->validator->validate( $name, [ new Assert\GreaterThan(4), ] ); $errors = new ConstraintViolationList([...$notBlankErrors, ...$greaterThanErrors]); // if ($nameErrors->count() > 0) { // throw new \Exception(); // } } return [ 'errors' => $errors ?? [], ]; } }
20231004追記
なんの値をバリデートしたか判定する方法をこちらに記載しました。