# Custom validation

**URL:** <https://www.ruby-forum.com/t/custom-validation/185785>\
**Category:** Rails\
**Created:** [March 26, 2010, 1:36am UTC](https://www.ruby-forum.com/t/custom-validation/185785 "2010-03-26T01:36:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![migrate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc79bd/32.png) [@migrate](https://www.ruby-forum.com/u/migrate)\
**Post date:** [March 26, 2010, 1:36am UTC](https://www.ruby-forum.com/t/custom-validation/185785/1 "2010-03-26T01:36:01Z")

</div>

Hi all,

I need to call validation methods from outside the models.  
This is a workflow application so the models don’t have validation  
methods declared in it.

Example:  
class Step \< ActiveRecord::Base  
end

def Wrk \< ActiveRecord::Base  
has\_many :steps  
end

I’ve tried using for instance Step.validates\_numericality\_of(…) or  
Wrk.validates\_format\_of(…) but it does not work.

Can anyone help me?  
Best regards,  
Hugo

---

<div class="post-metadata">

**Author:** ![migrate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc79bd/32.png) [@migrate](https://www.ruby-forum.com/u/migrate)\
**Post date:** [March 26, 2010, 3:27am UTC](https://www.ruby-forum.com/t/custom-validation/185785/2 "2010-03-26T03:27:10Z")

</div>

I believe you are not thinking in the proper use of validations.

What you want to do is:

s = Step.new  
…

s.valid?  
s.errors.on(_attribute\_to\_be\_validated_) #returns nil for a valid  
value. or  
the error message in case the validation was not met

On Fri, Mar 26, 2010 at 2:36 AM, Hugo M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def Wrk \< ActiveRecord::Base  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

–  
Mahmoud Said  
Software Engineer - eSpace

> **[Thoughts](http://blog.modsaid.com)**

---

<div class="post-metadata">

**Author:** ![migrate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc79bd/32.png) [@migrate](https://www.ruby-forum.com/u/migrate)\
**Post date:** [March 26, 2010, 8:43am UTC](https://www.ruby-forum.com/t/custom-validation/185785/3 "2010-03-26T08:43:21Z")

</div>

Hi Mahmoud,

Thanks for the info but the problem here is that my model does not have  
any validations declared because they depend on the workflow that the  
user sets.

For instance, the user can set that in the Step 1 of the Workflow he  
wants to validate that a given attribute is greater than 300.  
To do this I need to call directly the validates\_numericality\_of class  
method:

Step.validates\_numericality\_of(:attribute, :greater\_than =\> 300)

Best regards,  
Hugo

Mahmoud Said wrote:

> I believe you are not thinking in the proper use of validations.
> 
> What you want to do is:
> 
> s = Step.new  
> …
> 
> s.valid?  
> s.errors.on(_attribute\_to\_be\_validated_) #returns nil for a valid  
> value. or  
> the error message in case the validation was not met
> 
> On Fri, Mar 26, 2010 at 2:36 AM, Hugo M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > def Wrk \< ActiveRecord::Base  
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> –  
> Mahmoud Said  
> Software Engineer - eSpace  
> [blog.modsaid.com](http://blog.modsaid.com)

---

<div class="post-metadata">

**Author:** ![migrate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc79bd/32.png) [@migrate](https://www.ruby-forum.com/u/migrate)\
**Post date:** [March 26, 2010, 9:59pm UTC](https://www.ruby-forum.com/t/custom-validation/185785/4 "2010-03-26T21:59:16Z")

</div>

The validations method you’re trying to call is not a class method,  
it’s a validation helper so you can’t call object.validation\_method  
(in this case Step.validates\_numericality\_of(…)).

The best way would be to declare customs validations if you want to  
call them outside your class and then handle exception (begin/rescue  
etc.), you should also use object.errors.add(attribute, ‘custom error  
message’).

Cheers,  
Emré.

---

<div class="post-metadata">

**Author:** ![migrate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc79bd/32.png) [@migrate](https://www.ruby-forum.com/u/migrate)\
**Post date:** [March 27, 2010, 8:27am UTC](https://www.ruby-forum.com/t/custom-validation/185785/5 "2010-03-27T08:27:15Z")

</div>

exactly like Emre told u. just keep in mind when u have to clear the  
errors  
if that depends on the state

On Fri, Mar 26, 2010 at 8:09 PM, Emre [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> EmrÃ©.
> 
> > To do this I need to call directly the validates\_numericality\_of class
> > 
> > > value. or  
> > > Software Engineer - eSpace  
> > > removed\_email\_address@domain.invalid[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> > > .  
> > > For more options, visit this group at  
> > > [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

–  
Mahmoud Said  
Software Engineer - eSpace

> **[Thoughts](http://blog.modsaid.com)**
