Used in the controller

If you want to use the validator in the controller is very simple, just need to be an annotation @Validate

  • An action can define multiple @Validate using multiple validators
  • Multiple validators are verified in the order of configuration

Define a ValidatorController as follows, using both the default validator and the custom validator as well as our custom validation rules.

 <?php declare(strict_types=1);

namespace App\Http\Controller;

use Swoft\Http\Message\Request;
use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
use Swoft\Validator\Annotation\Mapping\Validate;

/**
 * Class ValidatorController
 *
 * @Controller()
 */
class ValidatorController
{
    /**
     * 验证TestValidator验证器中的所有已定义字段
     *
     * @RequestMapping()
     * @Validate(validator="TestValidator")
     * @param Request $request
     *
     * @return array
     */
    public function validateAll(Request $request): array
    {
        return $request->getParsedBody();
    }

    /**
     * 仅验证TestValidator验证器中的 type 字段
     *
     * @RequestMapping()
     * @Validate(validator="TestValidator",fields={"type"})
     * @param Request $request
     *
     * @return array
     */
    public function validateType(Request $request): array
    {
        return $request->getParsedBody();
    }

    /**
     * 仅验证TestValidator验证器中的 password 字段 password字段使用的是自定义的验证规则。
     *
     * @RequestMapping()
     * @Validate(validator="TestValidator",fields={"password"})
     * @param Request $request
     *
     * @return array
     */
    public function validatePassword(Request $request): array
    {
        return $request->getParsedBody();
    }

    /**
     * 使用userValidator自定义验证器
     *
     * @RequestMapping()
     * @Validate(validator="userValidator")
     * @param Request $request
     *
     * @return array
     */
    public function validateCustomer(Request $request): array
    {
        return $request->getParsedBody();

    }
}
 
  • $request->getParsedBody() all parsed data
  • $request->parsedBody('key', 'default') specifies KEY parsing data
  • $request->getParsedQuery() all parsed Query parameters
  • $request->parsedQuery('key', 'default') specifies KEY parsing data (>=2.0.2)

@Validate

  • Validator specifies the validator name
  • Fields specifies the fields validated in the validator so that the validator can be reused efficiently
  • Type default body , ValidateType::GET validates the GET request query parameter
  • Params parameters used by the custom validator to pass to the custom validator

Note that the request data obtained by $request->getParsedBody() the data that has been modified by the validator. The validator can support forms and request body data validation, but the body validation needs to define the corresponding data parser. The framework provides a JSON/XML type data parser by default. For details, please refer to the Http Server chapter.

/docs/2.x/en/validator/controller-validator.html
progress-bar