Is there a 'right' way to do _tag field validation?


#1

I’m reading the Ruby on Rails book by the pragmatic programmers, and it
has
a brief section on using the _tag input fields (text_field_tag, etc) to
collect form data for fields that don’t represent fields in a model.
They
do validation manually in the controller, which seems really clunky. Is
there any way to normalize the form validation between model input
fields
and non-model fields? It seems like a waste to have
validate_presense_of()
and then write a whole nother set of validation functions to validate
the
tag fields.

For example, say I have a log in page that just has two input fields,
neither of which correspond to an actual table or row because I call
some
other method to verify the login and password. Is there anyway that I
can
use all of the existing validation functionality to validate the form,
or is
the only way to manually check the validation in the controller? (and
subsquently, check for any error message in the view and display it)

It would be nice if there were a way to add validation rules to specific
elements without having to check each field in the controller.

I am very new to Ruby and Rails, so I may very well be missing something
obvious. I’ve asked in #rubyonrails and googled some though, without
finding
a very satisfactory explanation, so I’m hoping someone here may be able
to
help me. Thanks,
-Chris


#2

Chris Boyce wrote:

neither of which correspond to an actual table or row because I call some
other method to verify the login and password. Is there anyway that I can
use all of the existing validation functionality to validate the form, or is
the only way to manually check the validation in the controller? (and
subsquently, check for any error message in the view and display it)

Validation in the model and view should work just fine for non-DB
attributes
in AR-decscended models defined as either attr_accessors or by custom
methods,
and there are several solutions on the Web for fully non-DB-backed
models.

Post an example of what is not working.


We develop, watch us RoR, in numbers too big to ignore.


#3

At this point, there isn’t anything ‘not’ working, I’m just trying to
figure
out how to do it. What would the model look like if I just wanted to
validate the presence of a username and password field?


#4

Chris Boyce wrote:

At this point, there isn’t anything ‘not’ working, I’m just trying to figure
out how to do it. What would the model look like if I just wanted to
validate the presence of a username and password field?

Use the usual validation helpers in the model, and active record
form helpers in the view (rather than the _tag variants).


We develop, watch us RoR, in numbers too big to ignore.