Yii 1.1: alpha

customizable alpha / alphanumeric validator with client validation support
9 followers

alpha is a simple alpha / alphanumeric validator that allows you to customize it as you want to.

Requirements

Yii 1.1.7

Usage

to use alpha inside your model simply store its file into your application extensions directory and then use the rule

/**
 * Declares the validation rules.
 */
public function rules()
{
    return array(
        // name, email, subject and body are required
        array('email, subject, body', 'required'),
        array('name', 'ext.alpha'),
        // email has to be a valid email address
        array('email', 'email'),
    );
}

Available additional parameters

if you won't use any additional parameter alpha will validate any string with just basic alpha characters with a length of at least 1.

Here are the additional parameters you can use, and keep in mind that you can use all of them together:

  • allowNumbers is a boolean, and defaults to false. Allow the use of numbers into the string.
  • allowSpaces is a boolean and defaults to false. the name is pretty self explanatory.
  • minChars is an integer and defaults to 1.
  • maxChars is still an integer and defaults to NULL. If you don't want the validated string to have more then x characters use this parameter.
  • accentedLetters is another boolean. Enables the use of basic accented vowels into the string. See the list below.
  • allAccentedLetters still a boolean. Enable the use of every accented letter existing in the latin-derived languages. See the list below.
  • extra this is an array. If you want to allow some other characters (like -) this is the parameter you want to use.
  • message just like any other validator you can use this to change the error message. You can use {attribute} in this string as a wild card if you want to.

accentedLetters list

ÀÁÂÃÄĀĂÈÉÊËĚĔĒÌÍÎÏĪĨĬÒÓÔÕÖŌÙÚÛÜŪŬŨàáâãäāăèéêëēěĕ
ìíîïīĩĭòóôõöōŏùúûüūŭũ

allAccentedLetters list

ÀÁÂÃÄÅĀĄĂÆÇĆČĈĊĎĐÈÉÊËĒĘĚĔĖĜĞĠĢĤĦÌÍÎÏĪĨĬĮİIJĴĶŁĽĹĻ
ĿÑŃŇŅŊÒÓÔÕÖØŌŐŎŒŔŘŖŚŠŞŜȘŤŢŦȚÙÚÛÜŪŮŰŬŨŲŴÝŶŸŹŽŻàá
âãäåāąăæçćčĉċďđèéêëēęěĕėƒĝğġģĥħìíîïīĩĭįıijĵķĸłľĺ
ļŀñńňņʼnŋòóôõöøōőŏœŕřŗśšşŝșťţŧțùúûüūůűŭũųŵýÿŷžżź
ÞþßſÐð

Example of extra usage

/**
 * Declares the validation rules.
 */
public function rules()
{
    return array(
        // name, email, subject and body are required
        array('email, subject, body', 'required'),
        array('name', 'ext.alpha', 'extra' => array('-', '_')),
        // email has to be a valid email address
        array('email', 'email'),
    );
}

every character you'll use in extra will be escaped, so you shouldn't have to worry about anything.

Disclaimer

Keep in mind that this validation rule is not doing any conversion of the string (no htmlspecialchars, no htmlentities). Be always careful about how to save strings containing accented letters into your database.

Total 6 comments

#15123 report it
DerekC at 2013/10/09 09:39am
Works for me!

Thanks for the extension. Just what I wanted

#14711 report it
angelcoding at 2013/09/05 06:21am
File isn't corrupt

I know it's been a year, but just in case anyone is put off using this by the file corrupt comments here, I can assure you it's fine if you un-compress it correctly.

In windows, I had to open the archive in 7-zip and keep clicking through the contents until I got to the alpha.php file, which I simply copied into my extensions folder.

I don't think this is the first .tgz file that's fooled Windows users here and prob won't be the last.

#9708 report it
Don Felipe at 2012/09/05 04:47am
download file is still corrupt...

Hey nickcv, it's almost 3 months since FARAZFRANK's comment and the download is still not working. Please check! Thx

#8646 report it
FARAZFRANK at 2012/06/16 02:47am
File is corrupt.

your ext file is corrupt, upload new once.

#8312 report it
julianm at 2012/05/24 07:16pm
allowSpaces not working

Just in case, make sure you use false without quotes, otherwise allowSpaces and other options may not work.

Here is what I mean: http://snippet.io/snippet/29-allowspaces-yii-not-working-with-false-between-quotes

#8036 report it
Polux at 2012/05/04 03:22pm
Problem with extra

I had a problem with the extra option

array('username', 'ext.alpha', 'allowNumbers' =>true, 'allAccentedLetters' => true, 'extra' => array('-', '_', '.')),

Because the first character was not escape. I solve this with change

// add extra characters
        if (count($this->extra))
            $additionalParams .= implode('\\', $this->extra);

with

// add extra characters
        if (count($this->extra))
            $additionalParams .= '\\'.implode('\\', $this->extra);

Leave a comment

Please to leave your comment.

Create extension
Downloads