Symfony RequestValidatorBundle
Usage
<?php namespace AppBundleController; use SymfonyBundleFrameworkBundleControllerController; use SymfonyComponentValidatorConstraints as Assert; use SeferovRequestValidatorBundleValidatorRequestValidator; use SeferovRequestValidatorBundleAnnotationValidator; class AcmeController extends Controller { /** * @Validator(name="page", default="1", constraints={@AssertType(type="numeric"), @AssertRange(min=1)}) * @Validator(name="limit", default="25", constraints={@AssertType(type="numeric"), @AssertRange(min=10, max=100)}) * @Validator(name="order", default="desc", constraints={@AssertChoice(choices={"asc", "desc"}, message="error.wrong_order_choice")}) * @Validator(name="name", constraints={@AssertNotBlank()}) * @Validator(name="email", required=true, constraints={@AssertEmail()}) * * @param RequestValidator $requestValidator */ public function someAction(RequestValidator $requestValidator) { // You can get errors if there is any /** @var SymfonyComponentValidatorConstraintViolationList $errors */ $errors = $requestValidator->getErrors(); // You can get the request value with `get($path)` method $email = $requestValidator->get('email'); // ... } }
Installation
Step 1: Download the Bundle
Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:
$ composer require seferov/request-validator-bundle
This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.
Step 2: Enable the Bundle
Then, enable the bundle by adding it to the list of registered bundles
in the app/AppKernel.php
file of your project:
<?php // app/AppKernel.php // ... class AppKernel extends Kernel { public function registerBundles() { $bundles = array( // ... new SeferovRequestValidatorBundleSeferovRequestValidatorBundle(), ); // ... } // ... }
版权声明:
1、该文章(资料)来源于互联网公开信息,我方只是对该内容做点评,所分享的下载地址为原作者公开地址。2、网站不提供资料下载,如需下载请到原作者页面进行下载。
3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考学习用!
4、如文档内容存在违规,或者侵犯商业秘密、侵犯著作权等,请点击“违规举报”。