search

Home  >  Q&A  >  body text

How to write a field in laravel that can pass the verification through one rule when there are multiple verification rules?

How to write a field in laravel that can pass the verification through one rule when there are multiple verification rules?

'alipay'=>array('sometimes','required','email','regex:/^1(3[0-9]|4[57]|5[0-35-9] |7[0135678]|8[0-9])\d{8}$/'),

For example, this verification rule
As long as either the email account or mobile phone account passes, the verification can pass

给我你的怀抱给我你的怀抱2750 days ago681

reply all(2)I'll reply

  • 大家讲道理

    大家讲道理2017-05-16 16:50:29

    I have encountered this kind of demand before, and I really didn’t find a native support solution in the documentation. This is what I did in the end, you can refer to it

    $rule = preg_match('/^\d+$/', $request->input($this->loginUsername())) ? 'tel' : 'email';
    $this->validate($request, [
        $this->loginUsername() => "required|{$rule}",
        'password'             => 'required',
    ]);

    reply
    0
  • 怪我咯

    怪我咯2017-05-16 16:50:29

    Custom validation rules

    //AppServiceProvider
    class AppServiceProvider extends ServiceProvider
    {
        public function boot()
        {
            Validator::extend('alipay', function($attribute, $value, $parameters, $validator) {
                return preg_match('/^1(3[0-9]|4[57]|5[0-35-9]|7[0135678]|8[0-9])\d{8}$/',$value) || filter_var(FILTER_VALIDATE_EMAIL,$value);
            });
        }
    
        public function register()
        {
        }
    }
    
    // Controller 使用
    'alipay' => ['required','alipay']
    

    reply
    0
  • Cancelreply