Forum: Ruby on Rails How can I validate a text_field_tag?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Fdb488647484e40f785dde7134afc6d1?d=identicon&s=25 Heinz Strunk (cojones)
on 2009-03-17 18:24
Hey people,

I was wondering whether it's possible to validate a text_field_tag which
has nothing to do with the actual object being created?

Thanks
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-03-17 19:00
(Received via mailing list)
On Mar 17, 5:24 pm, Heinz Strunk <rails-mailing-l...@andreas-s.net>
wrote:
> Hey people,
>
> I was wondering whether it's possible to validate a text_field_tag which
> has nothing to do with the actual object being created?
>
validations are by definition a model centric thing. However there's
nothing stopping you doing
if not_good(params[:some_text:])
  ...
else
  @record.save
end

Fred
C4bfcc81ac9281cb905f38e97e4d4e0b?d=identicon&s=25 Shandy Nantz (snantz)
on 2009-03-17 22:42
Heinz Strunk wrote:
> Hey people,
>
> I was wondering whether it's possible to validate a text_field_tag which
> has nothing to do with the actual object being created?
>
> Thanks

What about JavaScript? For example, I call a Vlaidation method onsubmit
with a form when somebody creates a new object. Just a thought.

-S
Fdb488647484e40f785dde7134afc6d1?d=identicon&s=25 Heinz Strunk (cojones)
on 2009-03-17 23:04
Hey,

both are actually pretty good approaches I think but I was hoping I
could use that could use error_messages_for for the text_field_tag...
that's probably not the case?
If I was using "if not_good(params..." I'd need to display it
differently. It'd be extremly handy if I could just add an error to
error_messages_for using "if not_good(..."
5ed5cafb61087f0f2c046aa3344e25f2?d=identicon&s=25 tony (Guest)
on 2009-03-17 23:04
(Received via mailing list)
you could also try...@record.is_valid?

which will validate w/o saving

On Mar 17, 11:00 am, Frederick Cheung <frederick.che...@gmail.com>
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-03-17 23:15
(Received via mailing list)
On Mar 17, 10:04 pm, Heinz Strunk <rails-mailing-l...@andreas-s.net>
wrote:
> Hey,
>
> both are actually pretty good approaches I think but I was hoping I
> could use that could use error_messages_for for the text_field_tag...
> that's probably not the case?
> If I was using "if not_good(params..." I'd need to display it
> differently. It'd be extremly handy if I could just add an error to
> error_messages_for using "if not_good(..."

You can call some_object.errors.add(...) to add errors to an
activerecord object (you probably need to make the validations run
first). Check out the errors class in the api.

Fred
Fdb488647484e40f785dde7134afc6d1?d=identicon&s=25 Heinz Strunk (cojones)
on 2009-03-17 23:15
But the text_field_tag has nothing to do with the @record so it wouldn't
help if I was checking if the @record is valid, right?

tony wrote:
> you could also try...@record.is_valid?
>
> which will validate w/o saving
>
> On Mar 17, 11:00�am, Frederick Cheung <frederick.che...@gmail.com>
5ed5cafb61087f0f2c046aa3344e25f2?d=identicon&s=25 tony (Guest)
on 2009-03-18 02:29
(Received via mailing list)
since your text field tag isn't part of the model, you could
instantiate the object with the value from the tag and then call
is_valid and check the errors variable if you want the message.  else
you'll have to write your own validation methods

On Mar 17, 3:15 pm, Heinz Strunk <rails-mailing-l...@andreas-s.net>
D2536dbfa799820f207aaa74832a26d5?d=identicon&s=25 James Cowlishaw (Guest)
on 2009-03-18 10:28
(Received via mailing list)
Heinz,
If you're looking to invalidate the object based on this non-member
field,
you could use
ActiveRecord.Errors.add_to_base()

e.g. @record.errors.add_to_base("Invalid unpopple value")

Then check validity with @record.errors.empty?
Note that @record.is_valid will still return true (unless you override
invalid? for the class).

Hope that helps,
James.

2009/3/17 Heinz Strunk <rails-mailing-list@andreas-s.net>
91a72b0ad3e43e3ef3a31816667ec694?d=identicon&s=25 bill walton (Guest)
on 2009-03-20 01:22
(Received via mailing list)
Very nice.  Very helpful.  Thank you, James.

Best regards,
Bill
This topic is locked and can not be replied to.