Add http(s) to URL if it's not there?

前端 未结 7 1957
抹茶落季
抹茶落季 2020-12-08 19:27

I\'m using this regex in my model to validate an URL submitted by the user. I don\'t want to force the user to type the http part, but would like to add it myself if it\'s n

7条回答
  •  南笙
    南笙 (楼主)
    2020-12-08 19:52

    I wouldn't try to do that in the validation, since it's not really part of the validation.

    Have the validation optionally check for it; if they screw it up it'll be a validation error, which is good.

    Consider using a callback (after_create, after_validation, whatever) to prepend a protocol if there isn't one there already.

    (I voted up the other answers; I think they're both better than mine. But here's another option :)

提交回复
热议问题