Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between "validate" and "validates"?

I added a validation to an objects using:

validate :field, presence: true

I found they do not give error messages. I changed them to validates and got the error messages. This ticket is relevant.

I tried to add a custom validation with validates and got an error:

You need to supply at least one validation

I changed it to validate, and everything went along as expected.

My understanding is to use validates with normal validations, and validate with custom ones. Is that right? Are there any other pieces I should know about? Is there any way to have the first problem fail loudly and not just validate everything?

like image 568
Lucy Bain Avatar asked Aug 09 '13 06:08

Lucy Bain


People also ask

What do you mean validate?

Definition of validate transitive verb. 1a : to make legally valid : ratify. b : to grant official sanction to by marking validated her passport. c : to confirm the validity of (an election) also : to declare (a person) elected.

What is an example of validation?

To validate is to confirm, legalize, or prove the accuracy of something. Research showing that smoking is dangerous is an example of something that validates claims that smoking is dangerous.

Is validator a word?

A validator is a computer program used to check the validity or syntactical correctness of a fragment of code or document. The term is commonly used in the context of validating HTML, CSS and XML documents or RSS feeds though it can be used for any defined format or language.


2 Answers

validates This method is a shortcut to all default validators and any custom validator classes ending in ‘Validator’. Note that Rails default validators can be overridden inside specific classes by creating custom validator classes in their place such as PresenceValidator.

validates :title, :body, :presence => true

validate, Adds a validation method or block to the class. This is useful when overriding the validate instance method becomes too unwieldy and you’re looking for more descriptive declaration of your validations.

validate :must_be_friends

  def must_be_friends
    errors.add(:base, 'Must be friends to leave a comment') unless commenter.friend_of?(commentee)
  end
like image 164
rmagnum2002 Avatar answered Oct 06 '22 09:10

rmagnum2002


I believe the :validate declaration is used for custom validation where as :validates is used for generic validation like presence, uniqueness etc on a field

The validate method looks for a method with the parameter's name, i.e. if you do validate :field it will look for

def field 

end

on your object. Since Rails defines an attr_accessor for every database field the validate :field would call the field's reader method.

If the validation function returns true or if there is an error object, i.e. object.errors is not empty, then the object is considered valid?

Hence the reason in ligthouse issue, they complain that validate silently bypasses the validation :)

Hope this make sense

like image 33
Viren Avatar answered Oct 06 '22 08:10

Viren