Validating form input

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”=>“[email protected]”}, “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

On 8/7/08, Rajat G. [email protected] 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs