Lumen開發:添加手機驗證,中文驗證與Validator驗證的“半個”生命周期


版權聲明:本文為博主原創文章,未經博主允許不得轉載。

 

添加手機驗證方法可直接看這里:https://www.cnblogs.com/cxscode/p/9609828.html

 

今天來講一下,Lumen的Validator函數

use Validator;

...

Class .. {

public function ..(){

    Validator::make($input, $rules, $message, $attributes)->validate();

}
use Validator是可以直接引用的,雖然不能直接找到該命名空間的對應的位置。也可以直接在控制器use和使用Validator::make()。
至於類名和函數名就隨意啦,$input為傳入驗證的數組,$rule為驗證規則,$message為返回的規則,$attributes為驗證字段的對應中文注釋。廢話少說,先模擬一個標准的數據,
$input = [
      'typeid' => 1,
      'title'  =>  '測試標題',
      'content' => '測試內容'
];

$rules = [
        'typeid' => 'required|numeric',
        'title' => 'required',
        'content' => 'required'
];

$message = [
       "required" => ":attribute 不能為空",
       "numeric" => ":attribute 格式不正確"
];

$attributes = [
        'typeid' => '分類id',
        'title' => '標題',
        'content' => '內容'
];

執行后,可能報錯Illuminate\Validation\ValidationException: The given data failed to pass validation ...... vendor\illuminate\validation\Validator.php on line305

這是Lumen的異常處理機制,vendor\illuminate\validation\Validator.php

  /**
     * Run the validator's rules against its data.(運行驗證的規則對數據。)
     *
     * @return void
     *
     * @throws \Illuminate\Validation\ValidationException
     */
    public function validate()
    {
        if ($this->fails()) {
            throw new ValidationException($this);
        }
    }

  看看vendor\illuminate\validation\ValidationException.php

class ValidationException extends Exception
{
  ...
  /**
     * Create a new exception instance.(創建一個新的異常實例。)
     *
     * @param  \Illuminate\Contracts\Validation\Validator  $validator
     * @param  \Symfony\Component\HttpFoundation\Response  $response
     * @return void
     */
    public function __construct($validator, $response = null)
    {
        parent::__construct('The given data failed to pass validation.');

        $this->response = $response;
        $this->validator = $validator;
}

 

直接這樣拋出異常肯定不ok,接下看看解決方法,

方法一,異常攔截

進入app\Exceptions\Hanlder.php

public function render($request, Exception $e)
{
  //自帶數據驗證錯誤返回
  if ($e instanceof \Illuminate\Validation\ValidationException) {
    return $this->handleValidationException($request, $e);
  }

  return parent::render($request, $e);
}

 

//獲取自帶數據驗證錯誤信息
protected function handleValidationException($request, $e)
{
  $errors = @$e->validator->errors()->toArray();
  $message = null;
  if (count($errors)) {
    $firstKey = array_keys($errors)[0];
    $message = @$e->validator->errors()->get($firstKey)[0];
    if (strlen($message) == 0) {
      $message = "An error has occurred when trying to register";
    }
  }

  if ($message == null) {
    $message = "An unknown error has occured";
  }

  return $message;
}

  

結果會返回第一個驗證不過的對應信息。

不過直接在異常這里做處理不太合理,方法二

//Validator::make($input, $rules , $message, $attributes)->validate();
$validator = Validator::make($input, $rules, $message, $attributes);
$failed = $validator->failed();
$messages = $validator->messages();

if(count($messages) != 0){
      dataReturn(1,current(current($messages))[0]);
}

調用failed和messages方法來處理會優雅許多!

接下來說一下大家關心的手機驗證,

Validator::extend('mobile', function($attribute, $value, $parameters) {
            return preg_match("/^1[34578]{1}\d{9}$/",$value);
        });
這段代碼應該很熟悉吧,不過到底放在哪里合適呢?我是這么做的

bootstrap/app.phpj加上
$app->register(App\Providers\Validate\ValidateServiceProvider::class);
 
        

然后創建對應服務類app/Providers/Validate/ValidateServiceProvider.php

 
        
<?php

namespace App\Providers\Validate;
use Validator;
use Illuminate\Support\ServiceProvider;

class ValidateServiceProvider extends ServiceProvider
{

    /**
     * 啟動應用服務
     *
     * @return void
     */
    public function boot()
    {

        Validator::extend('mobile', function($attribute, $value, $parameters) {
            return preg_match("/^1[34578]{1}\d{9}$/",$value);
        });
     
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        
    }
}

加入手機驗證

$input = [
      'typeid' => 1,
      'title'  =>  '測試標題',
      'content' => '測試內容',
      'phone' => '1881'
];

$rules = [
        'typeid' => 'required|numeric',
        'title' => 'required',
        'content' => 'required',
        'phone' =>  'mobile'
];

$message = [
       "required" => ":attribute 不能為空",
       "numeric" => ":attribute 格式不正確",
       "mobile" =>  ":attribute 手機格式不正確"
];

$attributes = [
        'typeid' => '分類id',
        'title' => '標題',
        'content' => '內容',
        'phone' => '聯系方式'
];

就這樣簡單的實現了手機驗證!

如果不想每次都傳$message的話,可以看看這里\vendor\laravel\lumen-framework\resources\lang\en\validation.php

 

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Validation Language Lines
    |--------------------------------------------------------------------------
    |
    | The following language lines contain the default error messages used by
    | the validator class. Some of these rules have multiple versions such
    | as the size rules. Feel free to tweak each of these messages here.
    |
    */

    'accepted'             => 'The :attribute must be accepted.',
    'active_url'           => 'The :attribute is not a valid URL.',
    'after'                => 'The :attribute must be a date after :date.',
    'after_or_equal'       => 'The :attribute must be a date after or equal to :date.',
    'alpha'                => 'The :attribute may only contain letters.',
    'alpha_dash'           => 'The :attribute may only contain letters, numbers, and dashes.',
    'alpha_num'            => 'The :attribute may only contain letters and numbers.',
    'array'                => 'The :attribute must be an array.',
    'before'               => 'The :attribute must be a date before :date.',
    'before_or_equal'      => 'The :attribute must be a date before or equal to :date.',
    'between'              => [
        'numeric' => 'The :attribute must be between :min and :max.',
        'file'    => 'The :attribute must be between :min and :max kilobytes.',
        'string'  => 'The :attribute must be between :min and :max characters.',
        'array'   => 'The :attribute must have between :min and :max items.',
    ],
    'boolean'              => 'The :attribute field must be true or false.',
    'confirmed'            => 'The :attribute confirmation does not match.',
    'date'                 => 'The :attribute is not a valid date.',
    'date_format'          => 'The :attribute does not match the format :format.',
    'different'            => 'The :attribute and :other must be different.',
    'digits'               => 'The :attribute must be :digits digits.',
    'digits_between'       => 'The :attribute must be between :min and :max digits.',
    'dimensions'           => 'The :attribute has invalid image dimensions.',
    'distinct'             => 'The :attribute field has a duplicate value.',
    'email'                => 'The :attribute must be a valid email address.',
    'exists'               => 'The selected :attribute is invalid.',
    'file'                 => 'The :attribute must be a file.',
    'filled'               => 'The :attribute field is required.',
    'image'                => 'The :attribute must be an image.',
    'in'                   => 'The selected :attribute is invalid.',
    'in_array'             => 'The :attribute field does not exist in :other.',
    'integer'              => 'The :attribute must be an integer.',
    'ip'                   => 'The :attribute must be a valid IP address.',
    'json'                 => 'The :attribute must be a valid JSON string.',
    'max'                  => [
        'numeric' => 'The :attribute may not be greater than :max.',
        'file'    => 'The :attribute may not be greater than :max kilobytes.',
        'string'  => 'The :attribute may not be greater than :max characters.',
        'array'   => 'The :attribute may not have more than :max items.',
    ],
    'mimes'                => 'The :attribute must be a file of type: :values.',
    'mimetypes'            => 'The :attribute must be a file of type: :values.',
    'min'                  => [
        'numeric' => 'The :attribute must be at least :min.',
        'file'    => 'The :attribute must be at least :min kilobytes.',
        'string'  => 'The :attribute must be at least :min characters.',
        'array'   => 'The :attribute must have at least :min items.',
    ],
    'not_in'               => 'The selected :attribute is invalid.',
    'numeric'              => 'The :attribute must be a number.',
    'present'              => 'The :attribute field must be present.',
    'regex'                => 'The :attribute format is invalid.',
    'required'             => 'The :attribute field is required.',
    'required_if'          => 'The :attribute field is required when :other is :value.',
    'required_unless'      => 'The :attribute field is required unless :other is in :values.',
    'required_with'        => 'The :attribute field is required when :values is present.',
    'required_with_all'    => 'The :attribute field is required when :values is present.',
    'required_without'     => 'The :attribute field is required when :values is not present.',
    'required_without_all' => 'The :attribute field is required when none of :values are present.',
    'same'                 => 'The :attribute and :other must match.',
    'size'                 => [
        'numeric' => 'The :attribute must be :size.',
        'file'    => 'The :attribute must be :size kilobytes.',
        'string'  => 'The :attribute must be :size characters.',
        'array'   => 'The :attribute must contain :size items.',
    ],
    'string'               => 'The :attribute must be a string.',
    'timezone'             => 'The :attribute must be a valid zone.',
    'unique'               => 'The :attribute has already been taken.',
    'uploaded'             => 'The :attribute failed to upload.',
    'url'                  => 'The :attribute format is invalid.',

    /*
    |--------------------------------------------------------------------------
    | Custom Validation Language Lines
    |--------------------------------------------------------------------------
    |
    | Here you may specify custom validation messages for attributes using the
    | convention "attribute.rule" to name the lines. This makes it quick to
    | specify a specific custom language line for a given attribute rule.
    |
    */

    'custom' => [
        'attribute-name' => [
            'rule-name' => 'custom-message',
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | Custom Validation Attributes
    |--------------------------------------------------------------------------
    |
    | The following language lines are used to swap attribute place-holders
    | with something more reader friendly such as E-Mail Address instead
    | of "email". This simply helps us make messages a little cleaner.
    |
    */

    'attributes' => [],

];

 

默認都是英文,直接改成下面的中文配置

<?php

return [

/*
|--------------------------------------------------------------------------
| Validation Language Lines
|--------------------------------------------------------------------------
|
| The following language lines contain the default error messages used by
| the validator class. Some of these rules have multiple versions such
| as the size rules. Feel free to tweak each of these messages here.
|
*/

'accepted'             => ':attribute必須接受',
'active_url'           => ':attribute必須是一個合法的 URL',
'after'                => ':attribute 必須是 :date 之后的一個日期',
'after_or_equal'       => ':attribute 必須是 :date 之后或相同的一個日期',
'alpha'                => ':attribute只能包含字母',
'alpha_dash'           => ':attribute只能包含字母、數字、中划線或下划線',
'alpha_num'            => ':attribute只能包含字母和數字',
'array'                => ':attribute必須是一個數組',
'before'               => ':attribute 必須是 :date 之前的一個日期',
'before_or_equal'      => ':attribute 必須是 :date 之前或相同的一個日期',
'between'              => [
    'numeric' => ':attribute 必須在 :min 到 :max 之間',
    'file'    => ':attribute 必須在 :min 到 :max KB 之間',
    'string'  => ':attribute 必須在 :min 到 :max 個字符之間',
    'array'   => ':attribute 必須在 :min 到 :max 項之間',
],
'boolean'              => ':attribute 字符必須是 true 或 false',
'confirmed'            => ':attribute 二次確認不匹配',
'date'                 => ':attribute 必須是一個合法的日期',
'date_format'          => ':attribute 與給定的格式 :format 不符合',
'different'            => ':attribute 必須不同於 :other',
'digits'               => ':attribute必須是 :digits 位.',
'digits_between'       => ':attribute 必須在 :min 和 :max 位之間',
'dimensions'           => ':attribute具有無效的圖片尺寸',
'distinct'             => ':attribute字段具有重復值',
'email'                => ':attribute必須是一個合法的電子郵件地址',
'exists'               => '選定的 :attribute 是無效的.',
'file'                 => ':attribute必須是一個文件',
'filled'               => ':attribute的字段是必填的',
'image'                => ':attribute必須是 jpeg, png, bmp 或者 gif 格式的圖片',
'in'                   => '選定的 :attribute 是無效的',
'in_array'             => ':attribute 字段不存在於 :other',
'integer'              => ':attribute 必須是個整數',
'ip'                   => ':attribute必須是一個合法的 IP 地址。',
'json'                 => ':attribute必須是一個合法的 JSON 字符串',
'max'                  => [
    'numeric' => ':attribute 的最大長度為 :max 位',
    'file'    => ':attribute 的最大為 :max',
    'string'  => ':attribute 的最大長度為 :max 字符',
    'array'   => ':attribute 的最大個數為 :max 個.',
],
'mimes'                => ':attribute 的文件類型必須是 :values',
'min'                  => [
    'numeric' => ':attribute 的最小長度為 :min 位',
    'file'    => ':attribute 大小至少為 :min KB',
    'string'  => ':attribute 的最小長度為 :min 字符',
    'array'   => ':attribute 至少有 :min 項',
],
'not_in'               => '選定的 :attribute 是無效的',
'numeric'              => ':attribute 必須是數字',
'present'              => ':attribute 字段必須存在',
'regex'                => ':attribute 格式是無效的',
'required'             => ':attribute 字段是必須的',
'required_if'          => ':attribute 字段是必須的當 :other 是 :value',
'required_unless'      => ':attribute 字段是必須的,除非 :other 是在 :values 中',
'required_with'        => ':attribute 字段是必須的當 :values 是存在的',
'required_with_all'    => ':attribute 字段是必須的當 :values 是存在的',
'required_without'     => ':attribute 字段是必須的當 :values 是不存在的',
'required_without_all' => ':attribute 字段是必須的當 沒有一個 :values 是存在的',
'same'                 => ':attribute和:other必須匹配',
'size'                 => [
    'numeric' => ':attribute 必須是 :size 位',
    'file'    => ':attribute 必須是 :size KB',
    'string'  => ':attribute 必須是 :size 個字符',
    'array'   => ':attribute 必須包括 :size 項',
],
'string'               => ':attribute 必須是一個字符串',
'timezone'             => ':attribute 必須是個有效的時區.',
'unique'               => ':attribute 已存在',
'url'                  => ':attribute 無效的格式',
'mobile'                  => ':attribute 手機號碼無效',

/*
|--------------------------------------------------------------------------
| Custom Validation Language Lines
|--------------------------------------------------------------------------
|
| Here you may specify custom validation messages for attributes using the
| convention "attribute.rule" to name the lines. This makes it quick to
| specify a specific custom language line for a given attribute rule.
|
*/

'custom' => [
    'attribute-name' => [
        'rule-name' => 'custom-message',
    ],
],

/*
|--------------------------------------------------------------------------
| Custom Validation Attributes
|--------------------------------------------------------------------------
|
| The following language lines are used to swap attribute place-holders
| with something more reader friendly such as E-Mail Address instead
| of "email". This simply helps us make messages a little cleaner.
|
*/

'attributes' => [],

];

然后在沒有傳$message時,就會默認讀這里了!

 

接下來講講Lumen驗證的周期,當你調用下面的語句時,

 
        
Validator::make($input, $rules, $message, $attributes);
 
        

會直接調用到對應的Factory.php,這可以說是一種規范,這里是vendor\illuminate\validation\Factory.php,看看對應的make函數

    /**
     * 創建一個新的驗證實例。
     */
    public function make(array $data, array $rules, array $messages = [], array $customAttributes = [])
    {
        // The presence verifier is responsible for checking the unique and exists data
        // for the validator. It is behind an interface so that multiple versions of
        // it may be written besides database. We'll inject it into the validator.
        $validator = $this->resolve(
            $data, $rules, $messages, $customAttributes
        );

        if (! is_null($this->verifier)) {
            $validator->setPresenceVerifier($this->verifier);
        }

        // Next we'll set the IoC container instance of the validator, which is used to
        // resolve out class based validator extensions. If it is not set then these
        // types of extensions will not be possible on these validation instances.
        if (! is_null($this->container)) {
            $validator->setContainer($this->container);
        }

        $this->addExtensions($validator);

        return $validator;
    }

  然后來到resolve函數,

    /**
     * 解決一個新的驗證實例。
     */
    protected function resolve(array $data, array $rules, array $messages, array $customAttributes)
    {
        if (is_null($this->resolver)) {
            return new Validator($this->translator, $data, $rules, $messages, $customAttributes);
        }

        return call_user_func($this->resolver, $this->translator, $data, $rules, $messages, $customAttributes);
    }
 
        

  

namespace Illuminate\Validation;

..

class Validator implements ValidatorContract
{

...


    /**
     * Create a new Validator instance.(創建一個新的驗證實例。)
     */
    public function __construct(Translator $translator, array $data, array $rules,
                                array $messages = [], array $customAttributes = [])
    {
        $this->initialRules = $rules;
        $this->translator = $translator;
        $this->customMessages = $messages;
        $this->data = $this->parseData($data);
        $this->customAttributes = $customAttributes;

        $this->setRules($rules);
    }


}

make執行到這里完。

 

接下來,先來看看剛才用的第一個驗證函數validate(),

    /**
     * Run the validator's rules against its data.(運行驗證的規則對數據。)
     */
    public function validate()
    {
        if ($this->fails()) {
            throw new ValidationException($this);
        }
    }
/**
     * Determine if the data fails the validation rules.(確定數據驗證失敗的規則。確定數據驗證失敗的規則。)
   * @return bool */ public function fails() { return ! $this->passes();//稍后分析
}

顯而易見,一旦驗證不通過則拋出異常,不太適合一般的開發。

 

再看看messages()

    /**
     * Get the message container for the validator.(得到驗證消息的容器。)
     */
    public function messages()
    {
        if (! $this->messages) {
            $this->passes();//稍后分析
        }

        return $this->messages;
    }

這里函數是返回所有驗證不通過的信息

 

再看看failed()

  /**
     * Get the failed validation rules.(獲取失敗的驗證規則。)
     *
     * @return array
     */
    public function failed()
    {
        return $this->failedRules;
    }

 這邊好像驗證失敗也是空,暈~

 

接下來看passes()

  /**
     * Determine if the data passes the validation rules.(確定數據驗證規則)
     *
     * @return bool
     */
    public function passes()
    {
        $this->messages = new MessageBag;

        // We'll spin through each rule, validating the attributes attached to that
        // rule. Any error messages will be added to the containers with each of
        // the other error messages, returning true if we don't have messages.
        foreach ($this->rules as $attribute => $rules) {
            $attribute = str_replace('\.', '->', $attribute);

            foreach ($rules as $rule) {
                $this->validateAttribute($attribute, $rule);

                if ($this->shouldStopValidating($attribute)) {
                    break;
                }
            }
        }

        // Here we will spin through all of the "after" hooks on this validator and
        // fire them off. This gives the callbacks a chance to perform all kinds
        // of other validation that needs to get wrapped up in this operation.
        foreach ($this->after as $after) {
            call_user_func($after);
        }

        return $this->messages->isEmpty();
    }

 接下來干貨來了validateAttribute($attribute, $rule)函數,處理都在這里調的

  /**
     * Validate a given attribute against a rule.(根據規則驗證給定屬性。)
     *
     * @param  string  $attribute
     * @param  string  $rule
     * @return void
     */
    protected function validateAttribute($attribute, $rule)
    {
        $this->currentRule = $rule;

        list($rule, $parameters) = ValidationRuleParser::parse($rule);

        if ($rule == '') {
            return;
        }

        // First we will get the correct keys for the given attribute in case the field is nested in
        // an array. Then we determine if the given rule accepts other field names as parameters.
        // If so, we will replace any asterisks found in the parameters with the correct keys.
        if (($keys = $this->getExplicitKeys($attribute)) &&
            $this->dependsOnOtherFields($rule)) {
            $parameters = $this->replaceAsterisksInParameters($parameters, $keys);
        }

        $value = $this->getValue($attribute);

        // If the attribute is a file, we will verify that the file upload was actually successful
        // and if it wasn't we will add a failure for the attribute. Files may not successfully
        // upload if they are too large based on PHP's settings so we will bail in this case.
        if ($value instanceof UploadedFile && ! $value->isValid() &&
            $this->hasRule($attribute, array_merge($this->fileRules, $this->implicitRules))
        ) {
            return $this->addFailure($attribute, 'uploaded', []);
        }

        // If we have made it this far we will make sure the attribute is validatable and if it is
        // we will call the validation method with the attribute. If a method returns false the
        // attribute is invalid and we will add a failure message for this failing attribute.
        $validatable = $this->isValidatable($rule, $attribute, $value);

        $method = "validate{$rule}";

        if ($validatable && ! $this->$method($attribute, $value, $parameters, $this)) {
            $this->addFailure($attribute, $rule, $parameters);
        }
    }

  主要是這兩段

     $method = "validate{$rule}";

        if ($validatable && ! $this->$method($attribute, $value, $parameters, $this)) {
            $this->addFailure($attribute, $rule, $parameters);
        }

  根據規則拼接驗證函數名,再調用,函數都寫在了最開始引用的Concerns\ValidatesAttributes

   use Concerns\FormatsMessages,
        Concerns\ValidatesAttributes;

  vendor\illuminate\validation\Concerns\ValidatesAttributes.php

namespace Illuminate\Validation\Concerns;

...

trait ValidatesAttributes
{

...

    /**
     * Validate that an attribute is numeric.(驗證屬性是數字的。)
     *
     * @param  string  $attribute
     * @param  mixed   $value
     * @return bool
     */
    protected function validateNumeric($attribute, $value)
    {
        return is_numeric($value);
    }
            return false;
        } elseif ($value instanceof File) {
            return (string) $value->getPath() != '';
        }

        return true;
    }

...

}

 這個就是剛才'required|numeric'的numeric對應執行的方法,不信可以在下面加上

  protected function validateMobile($attribute, $value, $parameters)
    {
        return preg_match("/^1[34578]{1}\d{9}$/",$value);
    }

  把剛才的ValidateServiceProvider.php的啟動注入注釋掉

public function boot()
    {

        //Validator::extend('mobile', function($attribute, $value, $parameters) {
            //return preg_match("/^1[34578]{1}\d{9}$/",$value);
        //});

     
    }

  執行還是會驗證,不過一般都是在外面注入,寫在里面只是測試下。

 

網上資料好少,純手打,請不要轉載,謝謝啦!!!

 Lumen技術交流群:310493206

版權聲明:本文為博主原創文章,未經博主允許不得轉載。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM