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

Custom regex arguments in validations. #106

Closed
willrax opened this issue Sep 10, 2014 · 5 comments
Closed

Custom regex arguments in validations. #106

willrax opened this issue Sep 10, 2014 · 5 comments

Comments

@willrax
Copy link
Contributor

willrax commented Sep 10, 2014

Maybe something like this. Would also help eliminate the problem of trying to build the ultimate regex! :rage4:

rmq.append(UITextField, :user).validates(:email, regex: /whatever/)
@GantMan
Copy link
Member

GantMan commented Sep 10, 2014

what about rmq.append(UITextField, :gender).validates(:custom, regex: /whatever/)

That way it's not specific, in case you want to chain.

@vaughankg
Copy link
Contributor

I like the above approach but for the conversation I'll add that you can currently register a custom validator like so:

module RubyMotionQuery
  class Validation
    class << self
      IS_DAVE = Regexp.new('^Dave$')

      @@validation_methods[:is_dave] = lambda do |value, opts|
        Validation.regex_match?(value, IS_DAVE)
      end
    end
  end
end

Then

rmq.append(UITextField, :user).validates(:is_dave)

rmq.validation.valid?("Dave", :is_dave) #=> true

@GantMan
Copy link
Member

GantMan commented Sep 10, 2014

@vaughankg maybe custom just for people wanting to pass a regex, and

rmq.validation.add_validator(:is_dave) do |value, opts|
  # Whatever here
  true
end

for a convenient way to add custom validators?

@vaughankg
Copy link
Contributor

Looks good. custom is super convieniant and with add_validator you can hide away validation logic in a separate file and use it across your app.

@GantMan
Copy link
Member

GantMan commented Sep 11, 2014

closed via #107, #108, #109

@GantMan GantMan closed this as completed Sep 11, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants