Text Area Character Limit in Commerce Customizable Products

I'm using Commerce Customizable Products and text area in a project. I'd like to limit the number of characters someone can submit in the text area. My temptation is to assume I'll need to just manually create a different text area type with javascript or some post form test to limit the characters. But, I thought I'd ask the maintainer before hacking on your project. Surely this isn't a single use need.

Example: advertising company, selling text ads online. They sell an ad for $10 at 100 characters, $12 for 125 characters, and $15 for 175 characters max. So, if a customer wants to buy a 120 character ad, they'll buy the second product.

Product 1 -- 100 character max
Product 2 -- 125 character max
Product 3 -- 175 character max

Using Commerce Customizable Products I can choose text area, but there's no way to limit the characters like you can with text field. This appears to be a core behavior, but we would want to control it at this level since you would create limits to fit the needs of the given product offering, not all text area throughout the website.

If you have an opinion, direction, suggestions, advice, or questions, I'd love to hear from you.

Thanks in advance for your participation!

Posted: Nov 5, 2013


Ryan Ryan Szrama on November 5, 2013

I'm pretty sure I've seen textfield / area widgets out there that support character maximums. To do this without any code, you'd have to create a different line item type for each level and then a different node type for each line item type, because you can only configure one widget per line item type (as opposed to using different display formatters in different view modes) and you have to specify the line item type used for an Add to Cart form in the node type's product reference field display formatter settings.

What I'd probably do in your situation is put a new field on the product called "character max" where I set the limit. Then I'd alter the Add to Cart form, find the character max from the currently selected product (available in $form_state['default_product']), and then adds the appropriate JavaScript / #validate handler to the message textarea. This form element should be refreshed if the customer were to select a different product, so I don't even think you'd need more than one product display / Add to Cart form in this scenario.

tekany on November 20, 2013


I found Maxlength does the trick managing this and other textfield length limits. It appears to be a Javascript only approach, so a better form test limiter would be preferable (didn't really test this theory by reading his code yet).

You can install, turn on, and add the textarea text length limit creating 1 unique line item per limit requirement. In my stated example, it's not that messy. 3 products, 3 unique line items added using Commerce Customizable Products, then simply add the products. If you had 2 or 3 limits and could reuse this custom product line item this may be a good solution. So far, it seems resilient enough.

More testing :)