[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

validate a select (boolean

Tahura Chaudhry

6/13/2008 9:03:00 PM

I am using
Ruby Version: 1.8.4
Rails Version: 1.2.2

I have an issue regarding validation of a boolean field in my
applicaiton.

On the UI, I had to show a dropdown with options: "yes", "no" and ""
(blank)

My HTML view contains:

<%= select 'guest', 'attending', [['Yes', true], ['No', false]],
{:include_blank => true, :custom_label => 'Will you be attending?'} %>

The view is rendered correctly.


The validation for this dropdown requires that blank is invalid. Only
"yes" or "no" are valid.

My Model (guest.rb) contains:

validates_inclusion_of (:attending, :in => [true, false])

The Issue is:

When I select blank in the UI dropdown component and post the form, the
validation succeeds and when the form is redisplayed 'No' is selected in
the dropdown. I see that in the database (MySQL) attending column,
false gets saved in that column.

How do I make validation to fail when blank is selected in the dropdown.

thanks,
--
Posted via http://www.ruby-....

5 Answers

Oscar Del Ben

6/13/2008 9:11:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

Try to pass to the validator :allow_blank => false

2008/6/13, Tahura Chaudhry <tahura@hotmail.com>:
>
> I am using
> Ruby Version: 1.8.4
> Rails Version: 1.2.2
>
> I have an issue regarding validation of a boolean field in my
> applicaiton.
>
> On the UI, I had to show a dropdown with options: "yes", "no" and ""
> (blank)
>
> My HTML view contains:
>
> <%= select 'guest', 'attending', [['Yes', true], ['No', false]],
> {:include_blank => true, :custom_label => 'Will you be attending?'} %>
>
> The view is rendered correctly.
>
>
> The validation for this dropdown requires that blank is invalid. Only
> "yes" or "no" are valid.
>
> My Model (guest.rb) contains:
>
> validates_inclusion_of (:attending, :in => [true, false])
>
> The Issue is:
>
> When I select blank in the UI dropdown component and post the form, the
> validation succeeds and when the form is redisplayed 'No' is selected in
> the dropdown. I see that in the database (MySQL) attending column,
> false gets saved in that column.
>
> How do I make validation to fail when blank is selected in the dropdown.
>
> thanks,
>
> --
> Posted via http://www.ruby-....
>
>

Oscar Del Ben

6/13/2008 9:12:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

You can also use validates_presence_of, that will chech for the empty field

2008/6/13, Oscar Del Ben <thehcdreamer@gmail.com>:
>
> Try to pass to the validator :allow_blank => false
>
> 2008/6/13, Tahura Chaudhry <tahura@hotmail.com>:
>>
>> I am using
>> Ruby Version: 1.8.4
>> Rails Version: 1.2.2
>>
>> I have an issue regarding validation of a boolean field in my
>> applicaiton.
>>
>> On the UI, I had to show a dropdown with options: "yes", "no" and ""
>> (blank)
>>
>> My HTML view contains:
>>
>> <%= select 'guest', 'attending', [['Yes', true], ['No', false]],
>> {:include_blank => true, :custom_label => 'Will you be attending?'} %>
>>
>> The view is rendered correctly.
>>
>>
>> The validation for this dropdown requires that blank is invalid. Only
>> "yes" or "no" are valid.
>>
>> My Model (guest.rb) contains:
>>
>> validates_inclusion_of (:attending, :in => [true, false])
>>
>> The Issue is:
>>
>> When I select blank in the UI dropdown component and post the form, the
>> validation succeeds and when the form is redisplayed 'No' is selected in
>> the dropdown. I see that in the database (MySQL) attending column,
>> false gets saved in that column.
>>
>> How do I make validation to fail when blank is selected in the dropdown.
>>
>> thanks,
>>
>> --
>> Posted via http://www.ruby-....
>>
>>
>

Tahura Chaudhry

6/13/2008 9:28:00 PM

0

Oscar Del ben wrote:
> Try to pass to the validator :allow_blank => false
>
> 2008/6/13, Tahura Chaudhry <tahura@hotmail.com>:

I tried that and it did not work.
--
Posted via http://www.ruby-....

Tahura Chaudhry

6/13/2008 9:32:00 PM

0

Oscar Del ben wrote:
> You can also use validates_presence_of, that will check for the empty
> field
>
> 2008/6/13, Oscar Del Ben <thehcdreamer@gmail.com>:

If I use validate_presence_of then it the field is only considered valid
if 'Yes' (true) is selected. Selecting 'No' (false) results in
validation error.


After some reading this is what I found which is why I chose to use
validate_presence_of:

"If you want to validate the presence of a boolean field (where the real
values are true and false), you will want to use validates_inclusion_of
:field_name, :in => [true, false] This is due to the way Object#blank?
handles boolean values. false.blank? # => true"
--
Posted via http://www.ruby-....

Tahura Chaudhry

6/13/2008 9:36:00 PM

0

Tahura Chaudhry wrote:
> I am using
> Ruby Version: 1.8.4
> Rails Version: 1.2.2
>
> I have an issue regarding validation of a boolean field in my
> applicaiton.
>
> On the UI, I had to show a dropdown with options: "yes", "no" and ""
> (blank)
>
> My HTML view contains:
>
> <%= select 'guest', 'attending', [['Yes', true], ['No', false]],
> {:include_blank => true, :custom_label => 'Will you be attending?'} %>
>
> The view is rendered correctly.
>
>
> The validation for this dropdown requires that blank is invalid. Only
> "yes" or "no" are valid.
>
> My Model (guest.rb) contains:
>
> validates_inclusion_of (:attending, :in => [true, false])
>
> The Issue is:
>
> When I select blank in the UI dropdown component and post the form, the
> validation succeeds and when the form is redisplayed 'No' is selected in
> the dropdown. I see that in the database (MySQL) attending column,
> false gets saved in that column.
>
> How do I make validation to fail when blank is selected in the dropdown.
>
> thanks,


I'm beginning to think it has to do with with the Model field's (i.e
'attending' in guest.rb) mapping to the MySQL column ("attending") which
is declared in the schema as:

(`attending` tinyint(1) unsigned default NULL)



I have tried debugging the code but can't seem to find the source of
this problem. Any ideas?

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