similar to: Validations with conditions (:if)

Displaying 20 results from an estimated 100000 matches similar to: "Validations with conditions (:if)"

2006 Jun 12
2
conditional validation
Hi how can I validate a field only if another field is set to a specific value? I tried to use validates_length_of :fieldname, :maximum => 100, :if => :otherfieldname == ''myvalue'' But doesn''t work Thanks Paolo
2006 Jun 25
0
Using the database schema to help validate data
I started working on this in response to Dave Thomas'' keynote as RailsConf 2006. This little patch will validate that your text fields will fit into the database. Obviously this is just a start, and many more validations could be added. Also, this code could probably be extracted into a plugin instead of patch to ActiveRecord. Index: test/validations_test.rb
2007 Mar 29
4
validates_length_of not working with :if ?
Hello, I''m trying to cut down the errors if the guy already getting the empty nickname, so he doesn''t get anything about the nickname is short. validates_presence_of :nickname This below is not working ? validates_length_of :nickname, :within => 4..40, :if => Proc.new { |user| user.nickname.length > 1 } nor this one below? validates_length_of :nickname, :within
2009 Sep 19
1
validations on a field
hi, i have a model with 2 validations on a field example: validates_presence_of :email validates_length_of :email, :within => 6..100 #r@a.wk validates_uniqueness_of :email validates_format_of :email, :with => Authentication.email_regex if i do the submit of form for a new object of the model with a blank mail i receive 4 errors. In this case i want receive
2006 Jan 20
3
Is there a way to validate a model w/o saving it?
I have some code in my controller which looks like this: if request.post? and @account.save and @user.save ... end and in my Account and User models I have many :validates, like so account.rb validates_presence_of :subdomain, :on => :create validates_uniqueness_of :subdomain, :on => :create, :message => "is already being used" validates_exclusion_of
2005 May 04
1
Conditional validations
I''ve been working with the problem that Joe Hosteny has been talking about on this list and on the tickets he''s posted (http://dev.rubyonrails.com/ticket/1196) and have come up with a tentative solution that I''d like to bring up here. What if we use a "conditional" parameter in the validates_* methods? Here''s how it would look like in practice:
2011 Apr 18
2
acts_as_commentable validations
Hi all, I recently started back up with Rails and things are going well up until now. I''ve set up acts_as_commentable in my Post model and it''s working great. Problem is users are able to create a "blank" comment. I''ve added the following validations in the comment.rb file generated by acts_as_commentable to limit the comment length: [code]
2011 Feb 28
1
(2.3.5) removing validations
Today I tried to remove (actually overwrite) a validation from a model, from within a plugin. This was the original validation: validates_length_of :login, :maximum => 30 This was the only code I could come up with to replace it: def self.included(base) base.class_eval do @validate_callbacks.delete_if { |callback| begin # Sorry, only way to remove
2005 Nov 26
0
Model warnings along with Validations
Hi railers, Is there a simple method to extend Active Record to have similar functionality as the validate functions e.g. validate_on_find validates_acceptance_of validates_each validates_format_of validates_length_of validates_uniqueness_of etc... but only report warning on model attributes instead of errors(). I would like to save the model if there are warning and if the model is invalid
2006 Mar 16
0
validating presence of a tag
Hi, I''m using acts_as_taggable. I have a form that asks for a question and a tag for that question, something like: <% form_tag -%> <%= text_area("question", "question_text") %> <%= text_field("tag", "name") %> <%= submit_tag %> <% end_form_tag %> The question is one model. Tag is another. My problem is that I
2011 Mar 01
0
(2.3.5) removing validations
(sorry for misposting this to rubyonrails-core earlier) Today I tried to remove (actually overwrite) a validation from a model, from within a plugin. This was the original validation: validates_length_of :login, :maximum => 30 This was the only code I could come up with to replace it: def self.included(base) base.class_eval do @validate_callbacks.delete_if { |callback|
2006 Dec 16
2
Validate some fields only on object creation, not update
I''m having a problem with my User model. Originally I had alot of validation, including this: validates_presence_of :password_confirmation, :password validates_length_of :password, :within => 4..50 validates_confirmation_of :password However, this made it impossible to edit a user without setting his password, which I don''t want. So I tried to isolate the above validations
2006 Nov 04
0
Validations ignoring :allow_nil => true
I have an address model and am doing some validations. One of them seems to ignore :allow_nil => true. If I leave the phone input blank, I get it back with a "too short" error. Here''s the relevant parts of the model: 1. class Address < ActiveRecord::Base 2. 3. validates_length_of :phone, :in => 7..10, :allow_nil => true 4. 5. before_validation
2009 Mar 02
0
Implementing conditional read-only attributes of ActiveRecord objects
Hello, my goal is to implement the option of specifying a condition in attr_protected, attr_readonly and possibly other attribute methods in ActiveRecord objects. Rationale (example): class User < AR:Base # User should not be able to modify fields any more once they have been verified attr_readonly :firstname, :lastname, :gender_id, :birthdate, :if => :passport_verified? # ... end
2006 Jan 30
1
Either/Or Validation
How would I use validations to ensure I either received a blank value or a ten digit number? If I set validates_length_of :field, :in => 0..10 then that would allow any value in-between. The allow_nil => true option does not seem to work. I''ve also tried using this with some combination of validates_numericality_of but cannot get it to work. Any help would be greatly
2007 Dec 27
0
Silent Validation Failures
Hello, I''m having trouble with after_validation_on_create causing my validations to fail silently. I''ve got a restful_authentication based user model that requires a number of things from the user: email, dob, gender, etc. When a user is created, all of these are validated in various ways (presence, inclusion_of, etc.). Afterwards, I call three protected methods using
2006 Mar 17
0
validations using acts_as_taggable
Can anyone provide me an example of how they are displaying validation errors when saving a tag using Demetrius''s (not DHH''s) act_as_taggable plugin? For example, the tag model has a :validates_length_of :name, :in => 1..30, :too_long => "Tag name too long - max 30 characters", :too_short => "Tag must have a name" If a user inputs a tag longer than
2006 Jul 14
0
Setting up a Proc for conditional validation (:if option)
I want to use the :if option on one of my validation calls. Basically I want to validate for a field''s format, but only if the field is submitted. It''s an optional field. validates_format_of :EMAIL, :if => Proc.new { |email| ! email.nil? }, :message => ''is not a valid email address'', :with =>
2006 May 28
3
Validating Foreign Key
Howdy, I''m a Rails beginner working on my first significant project with RoR. I''m trying to figure out how to validate a foreign key (you know, make sure the row actually exists.) However, validates_associated doesn''t seem to be what I want to use, nor does it work... -- BEGIN -- class City < ActiveRecord::Base has_many :schools belongs_to :state
2007 Jul 24
13
Problem with validates_length_of an integer
Hello guys, I''m trying to validate the input of an integer from a form, here is a bit of code: This is the definition of the offending column in the migration: t.column :position, :integer, :default => 0, :limit => 3, :null => false And here is how I try to validate it inside de model: validates_length_of :position, :within => 1..3 And this is the exception I get when I