zendframework 2 inputfilter customize default error message

前端 未结 3 518
醉梦人生
醉梦人生 2020-12-09 20:34

I am trying to customize the default error message \"Value is required and can\'t be empty\" in zf2

I am using following code to add customise default e

3条回答
  •  渐次进展
    2020-12-09 21:00

    The StringLength validator does not check for the input to be empty or not. It checks against lengths. Following message templates exist for StringLength validator:

    const INVALID   = 'stringLengthInvalid';
    const TOO_SHORT = 'stringLengthTooShort';
    const TOO_LONG  = 'stringLengthTooLong';
    
    /**
     * @var array
     */
    protected $messageTemplates = array(
        self::INVALID   => "Invalid type given. String expected",
        self::TOO_SHORT => "The input is less than %min% characters long",
        self::TOO_LONG  => "The input is more than %max% characters long",
    );
    

    See the example of @Developer for a direct approach. Though i suggest going with the CamelCased naming of the Validators, so 'name' => 'NotEmpty' instead of 'name' => 'not_empty'

    You can check which messageTemplates exist if you see the code for each of the validator classes. You will find them under ./vendor/zendframework/zendframework/library/Zend/Validator/*

提交回复
热议问题