douzhong2954
2018-05-01 09:48
浏览 835
已采纳

检查输入中是否没有URL地址Laravel

I have 2 inputs with social accounts, I need get login this social accounts. I need deny write links in this inputs. How I can do it?

$this->validate($request, [
        'google_plus' => 'required|not:url',
        'twitter' => 'required|not:url',
    ]);

not:url not working..

Example: I need deny link: https://twitter.com/loginUser And allow only: loginUser

图片转代码服务由CSDN问答提供 功能建议

我有两个社交帐户输入,我需要登录这个社交帐户。 我需要在此输入中拒绝写入链接。 我怎么做?

  $ this-> validate($ request,[
'google_plus'=>'required | not:url',
'  twitter'=>'required | not:url',
]); 
   
 
 

not:url not working .. < / p>

示例:我需要拒绝链接: https://twitter.com/loginUser仅允许: loginUser

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doujian0265 2018-05-01 16:15
    已采纳

    First, follow the instructions in laravel validation, custom validation to create a new rule called NotUrl like this:

    class NotUrl implements Rule
    {   
       public function passes($attribute, $value)
       {
          return !filter_var($value, FILTER_VALIDATE_URL);
       }
    
       public function message()
       {
          return 'The :attribute must not be a complete URL.';
       }
    }
    

    Then validate like this:

    use App\Rules\NotUrl;
    
        $this->validate($request, [
                'google_plus' => ['required', new NotUrl],
                'twitter' => ['required', new NotUrl],
            ]);
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongliufa6380 2018-05-01 12:36

    You can use the regex validator or create a custom validator that implements the rule you're after.

    For example, something like regex:^https://twitter.com/.+ would validate an input that starts with that domain.

    评论
    解决 无用
    打赏 举报
  • dsiv4041 2018-05-01 12:52

    The purpose of validators is to stop submission if it doesn't meet the requirements. Based on what you described, you don't want to stop submission, but just trim it.

    In this case, don't put any rule on that field. When saving it to the database, use preg_replace:

    $pattern = "/(?i)\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))/";
    
    $newValue = preg_replace($pattern, "", $request->twitter);
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题