doucu5836 2015-01-17 06:40
浏览 353

Laravel表单文本字段验证特定URL?

I am building some user profiles and want to add social links to the profiles so users can link to their, let's say Steam, YouTube, Google+ profiles.

So i can't find any way of validating against specific url's in laravel. I want a user, if he set a link in steam text field to validate if given url is really url to steampowered.com, or if not to throw an error that url is not valid.

I have read the documentation on validation and i read that there is a URL validation, but as i have read it's only validating agains the input if it's formated as an url. so basically user can post any url and it will be validated.

Is there a filter or some additional condition to URL validation for a specific url. So the input field will be valid only if user insert like: http://steamcommunity.com in the field.

How can someone achieve that, or i must write old php regex expression?

  • 写回答

3条回答 默认 最新

  • dongqin1167 2015-01-17 09:25
    关注

    You should definetely write your own custom Validator and use a regexp to verify that:

    \Validator::extend('checkUrl', function($attribute, $value, $parameters)
    {
        $url = $parameters[0];
        //Your pattern, not pretending to be super precise, so it's up to you
        $pattern = '/^((http|https)\:\/\/)?(www\.)?'.$url.'\..+$/'
        if(  preg_match( $pattern,$value)
        {
            return true;
        }
        return false;
    });
    

    And to use it provide it as a rule to your validator

    $rules = [
       your_field => 'checkUrl:http://steamcommunity.com'
    ];
    
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建