Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

basic validations and .valid?

#21

I would love to see some (super-basic) validations support to replace the hacks I've come up with for the moment.

What I am doing now is just making an array of fields I wish to be present, then on before(:create) doing a check like this:

def validate_essentials check = ['name', 'email', 'timezone', 'password'] check.clone.each do |c|

check.delete_if {|x| x == c } if !self[c].blank?

end if check.blank?

return true

else

raise RuntimeError, check.first

end end

That gives me basic validations and a way to pass the missing field around, but as you can see it's not very elegant. It would be nice to be able to set some "required fields" once per class, then have a .valid? method to see if they were all satisfied, hooked into the official save method.

What do you think? Outside the scope of CouchRest? Or useful? : )

thanks for your excellent library btw, it's a pleasure to work with.

Reported by hkstar · November 13th, 2008 @ 06:27 PM

State: new
Milestone: none
Assigned to: J. Chris A. J. Chris A.

Activity

  1. hkstar
    hkstar

    sorry, should have checked formatting before posting. Not that the code was particularly valuable, but still ..

    Let me give it another shot:

    @@@def validate_essentials check = ['name', 'email', 'timezone', 'password'] check.clone.each do |c|

    check.delete_if {|x| x == c } if !self[c].blank?
    
    

    end if check.blank?

    return true
    
    

    else

    raise RuntimeError, check.first
    
    

    end end@@@

    November 13th, 2008 @ 06:30 PM

  2. hkstar
    hkstar

    So it seems the code formatting just does not work at all. Cool.

    November 13th, 2008 @ 06:35 PM

  3. hkstar
    hkstar

    Here's a patch for a super-simple implementation of this. All it does is define a number of fields you want to check, eg.

    validate :name, :email

    And then refuse to save (returns false) if they're not there. Also gives you .valid? and an .invalid_fields method.

    http://friendpaste.com/GWTHxAzw

    Not sure if it's really the kind of thing you have in mind, but works for me :)

    November 17th, 2008 @ 12:00 PM

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.