[rails] newby question on validation

M

Matteo Corti

Hi,

I am developing a small application with rails. I'll try to summarize my
problem.

I have an HTML table displaying the content of a database table
I want to filter the rows of my table by display just a subset of them (as
an example let's say that every record has a numberic field called number
and that I want to display only the records with a number field lower than a
user supplied value).

For this reason I have a web form with a text field asking for the number.

My problem comes with the validation of the user input since the values of
this form (as the number in the example) are not related to any table in the
database and as I have seen all the validation methods are part of
ActiveRecord:Base.

Should I check every value manually or is there a way to validate a form
input when the form data is not related to a database table?

Many thanks,

Matteo
 
G

gregarican

You should be able to access this text field's value by referring to
@param. For example, here's a text input field that would appear in an
HTML form:

<INPUT TYPE="TEXT" SIZE="5" MAXLENGTH="5"
NAME="thisControl[thisNumber]">

You could access the value of this text field using the following in
your accompanying controller.rb file:

retrievedValue = @params['thisControl']['thisNumber']
render_text "The text field's value as entered is #{retrievedValue}..."

You could validate the value using conditional statements of the common
variety:

render_text "The value is too large" if retrievedValue.to_i > 100

Does this make sense?
 
J

Joe Van Dyk

Hi,
=20
I am developing a small application with rails. I'll try to summarize my
problem.

You'll probably have better luck getting answers from the Rails mailing lis=
t...
 
E

ES

Le 3/6/2005 said:
You should be able to access this text field's value by referring to
@param. For example, here's a text input field that would appear in an
HTML form:

<INPUT TYPE=3D"TEXT" SIZE=3D"5" MAXLENGTH=3D"5"
NAME=3D"thisControl[thisNumber]">

You could access the value of this text field using the following in
your accompanying controller.rb file:

retrievedValue =3D @params['thisControl']['thisNumber']
render_text "The text field's value as entered is #{retrievedValue}..."

You could validate the value using conditional statements of the common
variety:

render_text "The value is too large" if retrievedValue.to_i > 100

Does this make sense?

Yes I was trying something like that:

if @params['a']['b'] !=3D nil and @params['a']['b'].to_i < 100
# do something

but I have problems when the field is left empty (i.e. empty string). The
param is not null and it seems to pass even the second test since an empty
string is converted to 0. Any suggestion on how I could check if there is a
valid integer?

Does this work?

p =3D @params['a']['b']
if p and not p.empty? and p.to_i < 100
Many thanks for the help.

Matteo

E
 
M

Matteo Corti

You should be able to access this text field's value by referring to
@param. For example, here's a text input field that would appear in an
HTML form:

<INPUT TYPE="TEXT" SIZE="5" MAXLENGTH="5"
NAME="thisControl[thisNumber]">

You could access the value of this text field using the following in
your accompanying controller.rb file:

retrievedValue = @params['thisControl']['thisNumber']
render_text "The text field's value as entered is #{retrievedValue}..."

You could validate the value using conditional statements of the common
variety:

render_text "The value is too large" if retrievedValue.to_i > 100

Does this make sense?

Yes I was trying something like that:

if @params['a']['b'] != nil and @params['a']['b'].to_i < 100
# do something

but I have problems when the field is left empty (i.e. empty string). The
param is not null and it seems to pass even the second test since an empty
string is converted to 0. Any suggestion on how I could check if there is a
valid integer?

Many thanks for the help.

Matteo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,478
Latest member
ReginaldVi

Latest Threads

Top