Zend_Validateでひらがな・カタカナ・日本語の入力チェック

| | コメント(0) | トラックバック(1)
Zend_Validate使いづらい!使いづらいというのは正確な言い方じゃないかも。
なんかこう、めんどくさいというか。たとえばエラーメッセージの"{入力文字列}は正しくない。"みたいなところがめんどくさい気分にさせる。そこは入力文字列じゃなくて項目名にしたいよ、みたいな。

まあ、いじっちゃえばいいんだけど、なんていうのかな。付け足すのはいいけど、あるものを修正するのはだるい気分になる、みたいな。

あと、当たり前かも知れないけど、日本語チェックがなかった。からコピペ中心で作った。



 Zend_とかしちゃってるけどいいのかな。まあしちゃってるから、Zend Frameworkのlibrary配下のそれぞれのところに配置することになる。
下記はカタカナチェックのだけど、ひらがなとか日本語のも同じノリでできるよね。

Filter
<?php

/**
 * @author piroshi
 */

/**
 * @see Zend_Filter_Interface
 */
require_once 'Zend/Filter/Interface.php';

class Zend_Filter_Katakana implements Zend_Filter_Interface
{
    /**
     * Whether to allow white space characters; off by default
     *
     * @var boolean
     */
    public $allowWhiteSpace;
    public $allowChouon;

    /**
     * Is PCRE is compiled with UTF-8 and Unicode support
     *
     * @var mixed
     **/
    protected static $_unicodeEnabled;

    /**
     * Sets default option values for this instance
     *
     * @param  boolean $allowWhiteSpace
     * @return void
     */
    public function __construct($allowWhiteSpace = false, $allowChouon = true)
    {
        $this->allowWhiteSpace = (boolean) $allowWhiteSpace;
        $this->allowChouon = (boolean) $allowChouon;
        if (null === self::$_unicodeEnabled) {
            self::$_unicodeEnabled = (@preg_match('/\pL/u', 'a')) ? true : false;
        }
 
    }

    /**
     * Defined by Zend_Filter_Interface
     *
     * Returns the string $value, removing all but Katakana characters
     *
     * @param  string $value
     * @return string
     */
    public function filter($value)
    {
        $whiteSpace = $this->allowWhiteSpace ? '\s' : '';
        $chouon = $this->allowChouon ? 'ー' : '';
        if (!self::$_unicodeEnabled) {
            // POSIX named classes are not supported, use alternative a-zA-Z match
            $pattern = '/[^ァ-ン' . $chouon . $whiteSpace . ']/';
        } else {
            //The Alphabet means each language's alphabet.
            $pattern = '/[^ァ-ン' . $chouon . $whiteSpace . ']/u';
        }

        return preg_replace($pattern, '', (string) $value);
    }
}


Validator

<?php

/**
 * @author piroshi
 */

/**
 * @see Zend_Validate_Abstract
 */
require_once 'Zend/Validate/Abstract.php';

class Zend_Validate_Katakana extends Zend_Validate_Abstract
{
    /**
     * Validation failure message key for when the value contains non-Katakana characters
     */
    const NOT_Katakana = 'notKatakana';

    /**
     * Validation failure message key for when the value is an empty string
     */
    const STRING_EMPTY = 'stringEmpty';

    /**
     * Whether to allow white space characters; off by default
     *
     * @var boolean
     */
    public $allowWhiteSpace;
    public $allowChouon;
   
    /**
     * Alphabetic filter used for validation
     *
     * @var Zend_Filter_Katakana
     */
    protected static $_filter = null;

    /**
     * Validation failure message template definitions
     *
     * @var array
     */
    protected $_messageTemplates = array(
        self::NOT_Katakana => "'%value%' はカタカナで入力してください。",
        self::STRING_EMPTY => "'%value%' をカタカナで入力してください。"
    );

    /**
     * Sets default option values for this instance
     *
     * @param  boolean $allowWhiteSpace
     * @return void
     */
    public function __construct($allowWhiteSpace = false, $allowChouon = true)
    {
        $this->allowWhiteSpace = (boolean) $allowWhiteSpace;
        $this->allowChouon = (boolean) $allowChouon;
    }

    /**
     * Defined by Zend_Validate_Interface
     *
     * Returns true if and only if $value contains only Katakana characters
     *
     * @param  string $value
     * @return boolean
     */
    public function isValid($value)
    {
        $valueString = (string) $value;

        $this->_setValue($valueString);

        if ('' === $valueString) {
            $this->_error(self::STRING_EMPTY);
            return false;
        }

        if (null === self::$_filter) {
            /**
             * @see Zend_Filter_Katakana
             */
            require_once 'Zend/Filter/Katakana.php';
            self::$_filter = new Zend_Filter_Katakana();
        }

        self::$_filter->allowWhiteSpace = $this->allowWhiteSpace;
        self::$_filter->allowChouon = $this->allowChouon;
       
        if ($valueString !== self::$_filter->filter($valueString)) {
            $this->_error(self::NOT_Katakana);
            return false;
        }

        return true;
    }

}
使う。
        $validator = new Zend_Validate_Katakana();
        if($isRequired && !$this->notEmptyValidator->isValid($value)){
            $msgs = $this->notEmptyValidator->getMessages();
            $msg = $msgs[Zend_Validate_Katakana::NOT_Katakana];
        }

        if((!$isRequired && $value) || ($isRequired && !$msg)){
            if(!$validator->isValid($value)){
                $msgs = $validator->getMessages();
                $msg = $msgs[Zend_Validate_Katakana::NOT_Katakana];
            }
        }

echo $msg;

こんな感じ。実際は$msgはエコーしないで、画面に持ってく感じになると思うんだけど、今その辺は製作中。


トラックバック(1)

このブログ記事を参照しているブログ一覧: Zend_Validateでひらがな・カタカナ・日本語の入力チェック

このブログ記事に対するトラックバックURL: http://hirop0164.s326.xrea.com/mt/mt-tb.cgi/209

「携帯サイト制作には欠かせないデータ入力チェック『Zend Validate』の派生クラスを公開します」の1つ目として入力データに「ひらがな」以外の文字が... 続きを読む

コメントする


画像の中に見える文字を入力してください。

このブログ記事について

このページは、ぴろしが2008年10月17日 21:06に書いたブログ記事です。

ひとつ前のブログ記事は「Fatal error: Class 'Zend_View_Helper_Abstract' not found in ・・・バグ?」です。

次のブログ記事は「ひらがな、カタカナ、日本語?の正規表現」です。

最近のコンテンツはインデックスページで見られます。過去に書かれたものはアーカイブのページで見られます。