:maxsize => 30 in form not working

Well, I’ve researched the books and the web to no avail (I tried the
search here too but kept getting page not found.I’m doing what the
examples say to do but I cannot get this work. The form allows entry of
characters beyond the maxsize which causes the app to abend giving me
the browser page showing a trace with the following info preceding the
trace:

Mysql::Error: #22001Data too long for column ‘category’ at row 1:
UPDATE categories SET category = ‘Furniture -
Bedroom11111111111111111111111111111111111’ WHERE id = 2

The “1”'s are of course for the test.

I have the following code in edit.rhtml:
<%= start_form_tag :action => ‘update’, :id => @category %>
<%= render :partial => ‘form’ %>
<%= submit_tag ‘Update’ %>
<%= end_form_tag %>

And the following code in _form.rhtml:
Category:
<%= text_field(“category”, “category”, :size => 30, :maxsize => 30) %>

I’ve tried numerous different combinations such as: maxlength=“30”,
:maxsize => “30”, without the :size => and so on. All to no avail.

Surely this doesn’t require code in validation or a before filter or
otherwise does it? Since I can’t allow the app to blow up every time a
user enters more characters than the size in the database, well, I’m at
a loss at this point.

Any help is surely appreciated. Thank you.

Scott

On Saturday, July 15, 2006, at 5:43 AM, R Scott H. wrote:

<%= text_field(“category”, “category”, :size => 30, :maxsize => 30) %>
Any help is surely appreciated. Thank you.

Scott


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

It might help to see how you implemented your test. The limits on
browser field forms are enforced by the browser itself, so if you pass a
long parameter to a controller as part of a test, the browser never sees
the long data and can’t do anything about it.

You probably should enforce a validation to make sure the length is
correct because you shouldn’t trust the client browser to do validation
for you.

For what it’s worth, I usually set columns to accept the maximum number
of characters. It doesn’t really hurt anything and it avoids a lot of
headaches like this one.

_Kevin

R Scott H. wrote:

Well, I’ve researched the books and the web to no avail

I’ve found http://www.w3schools.com/ to be an indispensible reference.

The form allows entry of characters beyond the maxsize

<%= text_field(“category”, “category”, :size => 30, :maxsize => 30) %>

According to the reference above (the attributes are at
http://www.w3schools.com/tags/tag_input.asp ), ‘size’ “defines the size
of
the input element” and ‘maxlength’ “defines the maximum number of
characters
allowed in a text field”. There is no ‘maxsize’ attribute listed.

According to the text_field documentation at http://api.rubyonrails.org/
Rails is not expecting symbols (it interprets anything with a colon in
front
of it as a symbol). I’ve been confused by the symbol vs. string thing
before myself. In some places Rails with take either. In some it
insists
on one or the other. And the particulars are subject to change :wink:

Anyway… you might try:
<%= text_field("category’, “category”, “size” => 30, “maxlength” => 30)
%>

You didn’t show how you’ve declared the category field in your database
but,
personally, I’ve never seen MySQL throw this error. I use ‘varchar(n)’
declarations for text_fields and the problem I’ve encountered is that
MySQL simply chops off anything beyond that. Kind of the opposite of
your
situation. If, as Kevin suggested, the problem turns out that the
browser
you’re using doesn’t enforce ‘maxlength’ you might want to take a look
at
this option if you’re using something like ‘string’.

One more note…
If you’re using Firefox and the Firebug extension, you can see exactly
what
attributes / options are set on your DOM elements. This could help you
figure out whether the problem lies with Rails not generating the right
stuff and your browser not handling it correctly. If you’re not using
them,
I definitely recommend you download them. Even if they’re not your
default
choice, I’ve found that they’re invaluable debugging tools.

hth,
Bill

Kevin O. wrote:

On Saturday, July 15, 2006, at 5:43 AM, R Scott H. wrote:

<%= text_field(“category”, “category”, :size => 30, :maxsize => 30) %>
Any help is surely appreciated. Thank you.

Scott


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

It might help to see how you implemented your test. The limits on
browser field forms are enforced by the browser itself, so if you pass a
long parameter to a controller as part of a test, the browser never sees
the long data and can’t do anything about it.

You probably should enforce a validation to make sure the length is
correct because you shouldn’t trust the client browser to do validation
for you.

For what it’s worth, I usually set columns to accept the maximum number
of characters. It doesn’t really hurt anything and it avoids a lot of
headaches like this one.

_Kevin

Thank you for your response Kevin. If I understand your question, my
test was simply to edit an existig record in the form itself and hold
downt eh ‘1’ key!

I’m not following you though on the ‘setting columns’.

Scott

Here’s what works:

<%= text_field(“category”, “category”, “size” => 30, “maxlength” => 30)
%>

I’d tried maxlength at one point, but I am pretty certain the combo was
something like maxlength=“30”.

The AWDwR book on page 357 shows :maxsize => “nn”, so I tried that too.
I lost track of every combo I tried to be honest.

I knew the browser wasn’t the problem as I’ve enforced this constraint
in JSPs before using Struts html tag lib.

I’m still trying to get a handle on symbols, instance variables and when
to use one or the other as well. To be honest, I have not been able to
determine any pattern yet. In the above, I really would have
thought/expected it to be a :symbol because of the =>. But no!

Anyway, thank you Kevin, Bill and Chris. Your help and time is much
appreciated and once again has led to the resolution of my problem.

Scott

Hi Scott,

By using ‘maxlength’, you’re enforcing that user’s can’t type too
many characters into the field when they’re using a browser (well,
browsers that respect ‘maxlength’, anyway!). That’s good.

But you’ll also want to validate this on the model side.

In your model, you should add a line like:

validates_length_of :category, :maximum => 30

That way you’ll be sure that you can’t save a model that has more
than 30 characters in the ‘category’ field.

For more documentation on validation, see http://wiki.rubyonrails.com/
rails/pages/HowToValidate

Make sense?

-Seth

Scott H. wrote:

Well, I’ve researched the books and the web to no avail (I tried the
search here too but kept getting page not found.I’m doing what the
examples say to do but I cannot get this work. The form allows entry of
characters beyond the maxsize which causes the app to abend giving me
the browser page showing a trace with the following info preceding the
trace:

Mysql::Error: #22001Data too long for column ‘category’ at row 1:
UPDATE categories SET category = ‘Furniture -
Bedroom11111111111111111111111111111111111’ WHERE id = 2

Snip SNip

<%= text_field(“category”, “category”, :size => 30, :maxsize => 30) %>

I’ve tried numerous different combinations such as: maxlength=“30”,
:maxsize => “30”, without the :size => and so on. All to no avail.

Surely this doesn’t require code in validation or a before filter or
otherwise does it? Since I can’t allow the app to blow up every time a
user enters more characters than the size in the database, well, I’m at
a loss at this point.

Any help is surely appreciated. Thank you.

Scott
Hi Scott

I would suggest you use validations, they will assure your app won’t
blow up, but if you really want to use the maxlength attribute, you can.
Rails expects html options to be in a hash,

<%= text_field(“category”, “category”, :size => 30, {:maxlength => 30})
%>

That should work. Good luck!

Chris

Seth Morabito wrote:

Hi Scott,

By using ‘maxlength’, you’re enforcing that user’s can’t type too
many characters into the field when they’re using a browser (well,
browsers that respect ‘maxlength’, anyway!). That’s good.

But you’ll also want to validate this on the model side.

In your model, you should add a line like:

validates_length_of :category, :maximum => 30

That way you’ll be sure that you can’t save a model that has more
than 30 characters in the ‘category’ field.

For more documentation on validation, see http://wiki.rubyonrails.com/
rails/pages/HowToValidate

Make sense?

-Seth

Yes. I wil do that too. It does make sense. If the browser doesn’t
support it, you are still covered. :slight_smile: Thank you Seth.

A number of tidbits of good advice in the responses in this thread! I
especially appreciate these kinds of responses as they address more than
the immediate problem but address the understanding of it as well.
Always more mileage in that.

Scott

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs