sfLimitedTextareaPlugin - 0.5.1

textarea_tag alternative that supports limits on total characters entered

You are currently browsing
the website for symfony 1

Visit the Symfony2 website


« Back to the Plugins Home

Signin


Forgot your password?
Create an account

Tools

Stats

advanced search
Information Readme Releases Changelog Contribute
Show source | Show as Markdown

sfLimitedTextareaPlugin

Limits on the length of user input are a fact of life. But the textarea HTML tag is notorious for its lack of support for such limits. sfLimitedTextareaPlugin provides both a hard limit and visual feedback as the user begins to approach the limit.

Requirements

Since this code does require a JavaScript framework, it should work just fine with any version of Symfony, at least through the 1.2 series. It has been tested thoroughly with version 1.0.

Usage

<?php use_helper('LimitedTextarea') ?>

<?php echo limited_textarea_tag(
  'text', 
  'default text',
  array(
    'id' => 'myid',
    'max' => 1000,
    'enforce' => true
  )) ?>

Note that you may pass additional options if you wish. They will be passed on to the underlying textarea_tag helper.

In this example, the length of user input is limited to 1,000 characters. That limit is enforced: if the user types more than 1,000 characters their additional output will be removed right away so that they know there is a problem. If you do not want the enforcement behavior, which can be a bit frustrating because the text insertion point tends to move, then just don't set the enforce option.

Even if you don't set the enforce option, the user will still see a visible indication of how many characters they have used so far and the total number of characters allowed. That indication is color-coded: green for the first two-thirds of the limit, yellow for until five-sixths and, if enforcement is turned on, red for the final sixth of the allowed number of characters. If enforcement is turned off, then red is not used until the allowed number of characters is exceeded, since the user would otherwise have no way of knowing that they are past the limit.

Note that these colors actually come from CSS classes, so you can change the behavior easily.

Of course, this is purely a convenience for end users. Your code must still take steps to make sure the input string is not too long. Client-side JavaScript can never be relied upon by the server and should only be used to help honest users do something more easily.

limited_textarea_tag uses CSS class names that begin with limited-textarea. This prevents conflicts with other code.

Customizing the CSS

By default, the plugin will automatically output a style tag defining the following simple styles for the "red," "green" and "yellow" states:

.limited-textarea-red {
  color: #FF0000;
}

.limited-textarea-green {
  color: #00FF00;
}

.limited-textarea-yellow {
  color: #FFFF00;
}

If you wish to redefine these, just set the appropriate parameter in your app.yml file:

all:
  limitedTextareaPlugin:
    customcss: true

Then add your own definitions for these classes to your own CSS file.

(I could have put this in a separate stylesheet to be loaded automatically, but loading three little CSS definitions does not justify forcing the browser to load an additional, completely separate file. Merge these into your own stylesheet instead.)

Changelog

0.5.1

Documentation fixes.

Credits

Tom Boutell

tom@punkave.com

Punk Avenue

Boutell.Com, Inc.