# Seems Complex validation --- pls help

**URL:** <https://www.ruby-forum.com/t/seems-complex-validation-pls-help/170496>\
**Category:** Rails\
**Created:** [June 18, 2009, 6:11am UTC](https://www.ruby-forum.com/t/seems-complex-validation-pls-help/170496 "2009-06-18T06:11:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [June 18, 2009, 6:11am UTC](https://www.ruby-forum.com/t/seems-complex-validation-pls-help/170496/1 "2009-06-18T06:11:45Z")

</div>

Hi…  
i have a auto complete field for my authority\_id field.authority\_id  
field is not mandatory.if the user not fills it will take the default  
authority id as value.  
but i want to validate if the user enters invalid authority name into  
the auto complete text box…  
i can do this in my controller…  
but how can i implementd as modal validation.  
pls help on this  
below is my controller side implementation  
#if autority text box is empty?  
if params[:user\_info][:login].empty?  
@user = UserInfo.find\_by\_login(‘admin’)  
@company.authority\_id = @user.id  
else  
authority = UserInfo.find\_by\_login(params[:user\_info][:login])  
if authority.nil?  
flash[:error] = “\*Please Enter valid Authority name”  
else  
@company.authority\_id = authority.id  
end  
end

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [June 18, 2009, 11:39am UTC](https://www.ruby-forum.com/t/seems-complex-validation-pls-help/170496/2 "2009-06-18T11:39:42Z")

</div>

Newb N. wrote:

> Hi…  
> i have a auto complete field for my authority\_id field.authority\_id  
> field is not mandatory.if the user not fills it will take the default  
> authority id as value.  
> but i want to validate if the user enters invalid authority name into  
> the auto complete text box…  
> i can do this in my controller…  
> but how can i implementd as modal validation.  
> pls help on this  
> below is my controller side implementation  
> #if autority text box is empty?  
> if params[:user\_info][:login].empty?  
> @user = UserInfo.find\_by\_login(‘admin’)  
> @company.authority\_id = @user.id  
> else  
> authority = UserInfo.find\_by\_login(params[:user\_info][:login])  
> if authority.nil?  
> flash[:error] = “\*Please Enter valid Authority name”  
> else  
> @company.authority\_id = authority.id  
> end  
> end

Hi newb!

There’s a validation called validates\_associated which checks if an  
assosciated object is also valid.

So in company, you could add

validates\_associated :authority :unless =\> lambda { |comp|  
comp.authority\_id.nil? }

This will check if the associated authority is a valid one provided the  
company’s authority\_id is not nil

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [June 19, 2009, 5:32am UTC](https://www.ruby-forum.com/t/seems-complex-validation-pls-help/170496/3 "2009-06-19T05:32:35Z")

</div>

hi…  
some one pls help me…  
thanks
