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.
Heinz S. (Guest)
on 2009-03-17 19: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
Frederick C. (Guest)
on 2009-03-17 20:00
(Received via mailing list)
On Mar 17, 5:24 pm, Heinz S. <removed_email_address@domain.invalid>
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
Shandy N. (Guest)
on 2009-03-17 23:42
Heinz S. 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
Heinz S. (Guest)
on 2009-03-18 00: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(..."
tony (Guest)
on 2009-03-18 00:04
(Received via mailing list)
you could also removed_email_address@domain.invalid_valid?

which will validate w/o saving

On Mar 17, 11:00 am, Frederick C. <removed_email_address@domain.invalid>
Frederick C. (Guest)
on 2009-03-18 00:15
(Received via mailing list)
On Mar 17, 10:04 pm, Heinz S. <removed_email_address@domain.invalid>
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
Heinz S. (Guest)
on 2009-03-18 00: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 removed_email_address@domain.invalid_valid?
>
> which will validate w/o saving
>
> On Mar 17, 11:00�am, Frederick C. <removed_email_address@domain.invalid>
tony (Guest)
on 2009-03-18 03: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 S. <removed_email_address@domain.invalid>
James Cowlishaw (Guest)
on 2009-03-18 11: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 S. <removed_email_address@domain.invalid>
bill walton (Guest)
on 2009-03-20 02: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.