# Validating form input

**URL:** <https://www.ruby-forum.com/t/validating-form-input/145340>\
**Category:** Ruby\
**Created:** [August 8, 2008, 6:50am UTC](https://www.ruby-forum.com/t/validating-form-input/145340 "2008-08-08T06:50:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rajatgarg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a183cd/32.png) [@rajatgarg](https://www.ruby-forum.com/u/rajatgarg)\
**Post date:** [August 8, 2008, 6:50am UTC](https://www.ruby-forum.com/t/validating-form-input/145340/1 "2008-08-08T06:50:00Z")

</div>

Hi Everybody,

I have a simple form whose input I am trying to validate in the  
controller.  
However, this if clause is not getting called at all… Is this a  
correct  
way to do it?

Please help.

_In Controller (action is getting called and code in else gets  
executed)-\>_

begin  
if (params[:fltAvailable].blank? || params[:user].blank?  
params[:user][“lastname”].blank? ||  
params[:user][:email].blank? ||  
params[:user][:firstname].blank? ||  
params[:fltAvailable][:from\_airport].blank? ||  
params[:fltAvailable][:from\_time].blank? ||  
params[:fltAvailable][:num\_seats].blank? ||  
params[:fltAvailable][:flight\_type].blank? || \*  
params[:fltAvailable][:mfr\_name].blank?\*)  
raise “error due to …”  
else  
…  
…  
…  
end

_From log file (mfr\_name is empty):_

Processing LocalFlightsController#save\_flight (for 24.16.111.232 at  
2008-08-07 22:01:11) [POST]  
Session ID: 59114620aa763d537c7a80d0877163e5  
Parameters: {“return\_uri”=\>“/local\_flights/post\_your\_flight”,  
“user”=\>{“lastName”=\>“Garg”, “firstName”=\>“Rajat”,  
“phone”=\>“206-499-9495”,  
“email”=\>“removed\_email\_address@domain.invalid”}, “commit”=\>“List your Upcoming  
Flight”, “action”=\>“save\_flight”,  
“fltAvailable”=\>{“from\_airport”=\>“HQM”,  
“num\_seats”=\>“2”, “estimated\_flying\_time”=\>“1:00”, “estimated\_cost”=\>“85  
per  
hou”, “model\_name”=\>“172”, “return\_time”=\>“Midnight”,  
“description”=\>“here  
is the flight stuff”, _“mfr\_name”=\>“”_, “from\_time”=\>“11:00 AM”,  
“return\_date”=\>“mm/dd/yy”, “from\_date”=\>“2008-08-21”, “to\_airport”=\>“”,  
“aircraft\_id”=\>“3318J”, “flight\_type”=\>“Local Tour”},  
“controller”=\>“local\_flights”, “fltPictures”=\>{“uploaded\_data”=\>“”}}

–  
Rajat G.

Ph: 206-499-9495  
Add: 16140 SE EastGate Way APT E-204  
Bellevue, WA 98008  
Web: [http://www.pilotoutlook.com](http://www.pilotoutlook.com)

---

<div class="post-metadata">

**Author:** ![rajatgarg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a183cd/32.png) [@rajatgarg](https://www.ruby-forum.com/u/rajatgarg)\
**Post date:** [August 8, 2008, 9:12pm UTC](https://www.ruby-forum.com/t/validating-form-input/145340/2 "2008-08-08T21:12:50Z")

</div>

On 8/7/08, Rajat G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi Everybody,
> 
> I have a simple form whose input I am trying to validate in the controller.  
> However, this if clause is not getting called at all… Is this a correct  
> way to do it?

I don’t know exactly what’s wrong with your statement, but you are  
missing  
a || on the end of the first line - so the parser treats the newline  
like a semicolon,  
meaning your first two tests are executed but ignored - they have no  
bearing on the result.

In terms of maintainablity/readibility, you may want to reorganize a  
little.  
The following code did raise the expected error for me when I set  
‘params’ to your log data:  
required = { :user=\>[:lastname, :email],  
:fltAvailable=\> [:from\_airport, :from\_time,:mfr\_name] }  
required.each{|key,subset|  
raise “error” if (params[key].blank? || subset.find{|s|  
params[key][s].blank?'})  
}

-Adam
