简体   繁体   中英

Rails validation - inclusion

I am trying to add a validation to prevent the user from entering a url that does not include the word 'mywebsite' in it.

validates :url, inclusion: { in: %w(mywebsite), message: "Must be a mywebsite.com subdomain" }

The problem with the above code is that it will only accept 'mywebsite' as a valid argument, while I'd like it to accept any argument which includes the word mywebsite in it. Example: mywebsite.com/something

How should I approach this?

Thanks for the guidance. Here is a working solution to my question:

validates_format_of :url, :with => /mywbesite(.*)/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM