Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use validates_url inside a AR validate method #78

Open
JulienItard opened this issue Mar 13, 2019 · 2 comments
Open

Use validates_url inside a AR validate method #78

JulienItard opened this issue Mar 13, 2019 · 2 comments

Comments

@JulienItard
Copy link

Hello !

I'd like to call validates_url inside a method. Is it possible to do it ?

With this test code, it raises undefined method.

validate :my_validation

def my_validation 
  validates_url(:url) && other validation && other validation
end

Thanks a lot

@kritik
Copy link
Member

kritik commented Mar 13, 2019

Yes, if you will initialize validator yourself. But you don't want to do that in 99%. What is your case?

@JulienItard
Copy link
Author

JulienItard commented Mar 13, 2019

Thanks for your quick anwser.

I have field, external link, that can be an URL, a mailto:... or a tel:....

I'd like to create a custom method and use your validates_url method when it's an URL.
Something like :

def my_validation
  validates_url(:url) || url.start_with?("mailto:") || url.start_with?("tel:")
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants