# Custom Error validation

**URL:** <https://www.ruby-forum.com/t/custom-error-validation/166139>\
**Category:** Rails\
**Created:** [April 22, 2009, 7:17am UTC](https://www.ruby-forum.com/t/custom-error-validation/166139 "2009-04-22T07:17:49Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [April 22, 2009, 7:17am UTC](https://www.ruby-forum.com/t/custom-error-validation/166139/1 "2009-04-22T07:17:49Z")

</div>

I have a couple of concerns here. Should I only use model methods to  
return objects and then save those objects in the controller? or is it  
okay to save within a model method like below? The other thing is I  
have created a javascript variable to flag if a time gets selected  
from a custom javascript pull down menu and I would like to be able to  
check that a time is selected and then display the error using rjs.  
Below is what I have so far, it does not totally work, but I wanted to  
get some input before I went any further.

def create  
case params[:event][:kind]  
when “appointment”  
event = Appointment.create\_and\_save(blah)  
when “personal”  
event = Personal.create\_and\_save(blah)  
end  
end

class Appointment \< Event  
def self.create\_and\_save(blah)  
if time\_selected == ‘false’  
appointment = Appointment.new  
appointment.errors.add(‘start\_int’, “You must select a time.”)  
else  
event\_info = { blah }  
appointment = Appointment.new(event\_info)  
end  
end  
end

#now back in the controller i would like to use rjs to render this  
error  
#something like:  
respond\_to do |format|  
if event.save!  
…  
else  
format.js do  
render :update do |page|  
page.replace\_html ‘error’, @user.return\_validation\_error  
(‘start\_int’) if @user.errors[‘start\_int’]  
#page \<\< “$(‘email\_help’).className = ‘signup\_error’;” if  
@user.errors[‘email’]  
end  
end  
end  
end
