How do I specify a max value for an attribute with a text input field?

9 replies [Last post]
Joined: 06/03/2009
Juice: 33

Sorry for the x-post, I tried this in the theming forum and got no response. It seems like a pretty basic thing to do, but apparently I can't do it out of the box, I'll need to write some code.

I am selling items that that can be personalized -- up to 10 characters. I need to limit the number of characters the customer can input in the attribute text field.

I'm new to Drupal but don't mind getting my hands dirty. If somebody could give me some guidelines, I'd be grateful. I'm running Ubercart 2 and Drupal 6.12.

Thanks in advance.

Joined: 06/03/2009
Juice: 33

Hi -- if anybody knows how to do this, I'd really appreciate some help.

This is a make-or-break issue for my client. If I can't limit the fields, customers can type in any old thing (e.g. 15 chars in a field that should only permit 12) and it's a huge headache going back and resolving it once the order is processed.

Thanks in advance.

Joined: 07/03/2008
Juice: 124

One way todo this is to use the hook_form_alter
With the hook you can add a validation for the required field

see: http://api.drupal.org/api/function/hook_form_alter/6
and: http://api.drupal.org/api/function/_form_validate/6

Joined: 06/03/2009
Juice: 33
Docc wrote:

One way todo this is to use the hook_form_alter
With the hook you can add a validation for the required field

see: http://api.drupal.org/api/function/hook_form_alter/6
and: http://api.drupal.org/api/function/_form_validate/6

Thanks, I appreciate that lead.

However, I don't need to *validate* the form so much as be able to set the maxlength in the first place. All the text input fields are automatically assigned "maxlength=128". I want to set "maxlength=12" or whatever I need for that particular attribute.

Honestly, I'm astonished that I cannot easily do this. There must be a way that I'm overlooking.

Joined: 06/03/2009
Juice: 33

Can anybody help me any further with this? I'm begging here. Smiling This is a deal breaker with my client.

I have a number of attributes -- all with different field lengths. If I cannot assign a field length at the time I set up the product, how am I supposed to know what to test for when the form is processed?

I have no idea how to solve this and I'm going to be in pretty deep trouble if I can't. My client cannot understand how such a simple thing is so difficult to implement.

Go ahead and call me a stupid idiot for not knowing this. As long as you spell the solution out to me in terms I can understand, I will take your abuse gratefully.

TR
TR's picture
User is online Online
Bug FinderFAQ ModeratorGetting busy with the Ubercode.
Joined: 11/05/2007
Juice: 3225

Docc gave you the answer - implement hook_form_alter() and change #maxlength to 12 instead of the Drupal default. There are all sorts of tutorials on how to use hook_form_alter() - look on drupal.org, on ubercart.org, or in any Drupal book. There's also a contributed module called Max Chars for Text Attributes that will do this for you.

<tr>.
Joined: 06/03/2009
Juice: 33

I'm not modifying a single form, I'm looking to set the maxlength value for dozens of different products, all with different attributes that all have different maxlengths. Some products have multiple attributes, each with different maxlengths.

Even if I wrote some custom code to handle each product's attributes, I can't see how this would be a scalable solution. My client wants to be able to enter new products himself, and I can't predict what the maxlength fields will be for those products' attributes.

Yes, I've searched drupal.org, ubercart.org, and several Drupal books. I haven't found a tutorial that has helped me yet, I am hoping that somebody here with far more Drupal experience than I have will kindly direct me to a *specific* resource, as I've come up empty.

The contributed module you suggested is for Drupal 5 only, unfortunately.

TR
TR's picture
User is online Online
Bug FinderFAQ ModeratorGetting busy with the Ubercode.
Joined: 11/05/2007
Juice: 3225

If you need a UI for the process I suggest porting that module to D6.

<tr>.
Joined: 06/03/2009
Juice: 33

Sigh.

Well, I found a solution on my own. There's Drupal 6 contributed module called Validation API that lets you add validation to any field. The good news is, it solved my problem, at least short term.

The bad news is that it's incredibly cumbersome to administer, and I can't really see talking my client through it. So it's a terrible long term solution.

I'm brand new to Drupal/Ubercart, and as I said above pretty surprised this basic feature isn't easy to implement. Hopefully I've helped somebody else with this thread, since everybody using Drupal these days isn't keen on developing modules (as enjoyable as that may be for some of you).

Joined: 05/05/2008
Juice: 325

cant believe this either. Something so simple as setting the max length of a text field and there's no way to do it in Drupal without writing a bunch of code and having to implement hooks and blah blah blah. What a joke!