Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
validating input values(integer)
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  7 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Soichi Ishida  
View profile  
 More options May 4 2012, 3:16 am
From: Soichi Ishida <li...@ruby-forum.com>
Date: Fri, 04 May 2012 09:16:32 +0200
Local: Fri, May 4 2012 3:16 am
Subject: validating input values(integer)
Rails 3.1.3

I have a table 'Contribution' having a column, 'price' which must be
integers.

  validate :price => true, :numericality => { :only_intger => true }

then, in creating a new Contribution DB, I need to check the user input
values. Of course, if the values are not valid, it needs to stay in the
same page showing error messages .

in 'contributions_controller.rb',

    respond_to do |format|
      if @contribution.save
        format.html { render action: "new", notice: 'Contribution was
successfully created.' }
        format.json { render json: @contribution, status: :created,
location: @contribution }
      else
        format.html { render action: "index" }
        format.json { render json: @contribution.errors, status:
:unprocessable_entity }
      end
    end

hoping that the 'save' method FAILS upon inputing string values, for
example.

But interestingly, if I input, say, 'this' in the input form, it directs
to the next page and the '0' (an integer, though) value is inserted in
the DB.  I do not have any default value to 'price'.

Could anyone point out any mistakes that I am making here?

Thanks in advance.

soichi

--
Posted via http://www.ruby-forum.com/.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Michael Pavling  
View profile  
 More options May 4 2012, 3:24 am
From: Michael Pavling <pavl...@gmail.com>
Date: Fri, 4 May 2012 08:24:43 +0100
Local: Fri, May 4 2012 3:24 am
Subject: Re: [Rails] validating input values(integer)
On 4 May 2012 08:16, Soichi Ishida <li...@ruby-forum.com> wrote:

> But interestingly, if I input, say, 'this' in the input form, it directs
> to the next page and the '0' (an integer, though) value is inserted in
> the DB.  I do not have any default value to 'price'.

If you pop open a Rails console and type:

  'this'.to_i

What result do you get?

Yep - zero is an integer. Rails casts the values from params to map to
the DB fields, so if you want validate the values the user typed
rather than the values Rails has translated them to, you may need to
write a custom validation and check the "before_typecast" value.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Soichi Ishida  
View profile  
 More options May 4 2012, 4:41 am
From: Soichi Ishida <li...@ruby-forum.com>
Date: Fri, 04 May 2012 10:41:21 +0200
Local: Fri, May 4 2012 4:41 am
Subject: Re: validating input values(integer)
Thanks for your reply.

> If you pop open a Rails console and type:

>   'this'.to_i

> What result do you get?

1.9.3-p0 :008 > 'this'.to_i
 => 0
1.9.3-p0 :009 >

It's zero...it does not seem good at all ;)

soichi

--
Posted via http://www.ruby-forum.com/.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Michael Pavling  
View profile  
 More options May 4 2012, 4:53 am
From: Michael Pavling <pavl...@gmail.com>
Date: Fri, 4 May 2012 09:53:14 +0100
Local: Fri, May 4 2012 4:53 am
Subject: Re: [Rails] Re: validating input values(integer)
On 4 May 2012 09:41, Soichi Ishida <li...@ruby-forum.com> wrote:

> 1.9.3-p0 :008 > 'this'.to_i
>  => 0
> 1.9.3-p0 :009 >

> It's zero...it does not seem good at all ;)

That's a different thread entirely ;-)

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "Can I get some advise on my gem ?" by Juhyeong
Juhyeong  
View profile  
 More options May 4 2012, 4:16 am
From: Juhyeong <zzur...@gmail.com>
Date: Fri, 4 May 2012 01:16:47 -0700
Local: Fri, May 4 2012 4:16 am
Subject: Can I get some advise on my gem ?
Hi All

I am a newbie to this community.

I recently published a gem called event_watcher for one of my project

http://github.com/zzurang/event_watcher

It is a simple dsl thing that is designed to help monitor certain function calls

I wonder if I can get some feedback or advise in general

Thanks


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "validating input values(integer)" by Matt Jones
Matt Jones  
View profile  
 More options May 4 2012, 8:03 am
From: Matt Jones <al2o...@gmail.com>
Date: Fri, 4 May 2012 05:03:58 -0700 (PDT)
Local: Fri, May 4 2012 8:03 am
Subject: Re: validating input values(integer)

On Friday, 4 May 2012 03:16:32 UTC-4, Ruby-Forum.com User wrote:

> Rails 3.1.3

> I have a table 'Contribution' having a column, 'price' which must be
> integers.

>   validate :price => true, :numericality => { :only_intger => true }

I'm not sure what effect this syntax will actually have, but it's not the
one you're looking for. The line in your model should look like this

validates :price, :numericality => { :only_integer => true }

(note the plural 'validates' rather than 'validate' - they do two quite
different things!)

--Matt Jones


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Soichi Ishida  
View profile  
 More options May 5 2012, 12:02 am
From: Soichi Ishida <li...@ruby-forum.com>
Date: Sat, 05 May 2012 06:02:41 +0200
Local: Sat, May 5 2012 12:02 am
Subject: Re: validating input values(integer)

> validates :price, :numericality => { :only_integer => true }

> (note the plural 'validates' rather than 'validate' - they do two quite
> different things!)

I must be blind! It should have been

  validates :price, :presence => true, ...

and plural, yes, it reminds me Rails like plural nouns.

Thanks.

soichi

--
Posted via http://www.ruby-forum.com/.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »