Yeah, I couldn't understand what Ken meant by his "state-machine" approach.

I can't think of a dry way of currently doing that.

I'm just not much concerned about this as I don't use AR and Sequel's way of doing validation is by defining a "validate" method, so I'd put my logic there, but anyway...

Em 21-06-2012 21:23, Anthony Richardson escreveu:
I can see some advantge of declaring a validation dependency. Checking
if an age is less than 16 makes no sense if the age has failed a
validation on being a number already.

maybe something like:

class User<  AR::Base
    validates_numericality_of :age

    #custom validation
    validate :user_older_16, :depends =>  :age

    def user_older_16
      errors.add(:age, "should be older 16") if age<= 16
    end
  end

Where :user_older_16 would only run if age has no errors. In the
context shown above this is a sensible dependency, but used
incorrectly the concerns expressed by others would be valid.

Another suggestion:

class User<  AR::Base

    validates_with_dependency do
      validates_numericality_of :age
      validate :user_older_16
    end

    def user_older_16
      errors.add(:age, "should be older 16") if age<= 16
    end
  end

In the above the validations in the validate_with_dependency block
would execute in order, exiting the sequence if any fail.

I think the suggestion to have dependent validaiton is a good one as
it allows for structuring simiple validations where you know a
validation cannot possibly pass if an earlier one has failed.

Cheers,

Anthony Richardson.

On Thu, Jun 21, 2012 at 8:26 PM, Pavel Gabriel<[email protected]>  wrote:
Hi!

I found that it's a popular use case for AR/AM validations:

class User<  AR::Base
   validates_numericality_of :age

   #custom validation
   validate :user_older_16

   def user_older_16
     errors.add(:age, "should be older 16") if age<= 16
   end
end

When you will try to validate as the following:

   user =  User.new(age: 'bla')
   user.valid?

you will get: ArgumentError: comparison of String with 16 failed.

Folks write a lot of code to check attribute values in their custom
validations, but it's not a DRY way because attributes already validated by
AM.

Is it a good idea to run validation methods only if errors.count == 0?

Or maybe we can add something like "validate_further :user_older_16" that
will run only if attributes are valid?

--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Core" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/rubyonrails-core/-/Ca3mpVVPWlgJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-core?hl=en.

--
You received this message because you are subscribed to the Google Groups "Ruby on 
Rails: Core" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-core?hl=en.

Reply via email to