Standing on the Shoulder of Linus

Home / 2012 / 9月 / 11 / FuelPHP のバリデーション。文字幅で制限

FuelPHP のバリデーション。文字幅で制限

FuelPHP のバリデーションについてです。標準では文字長でのバリデーションが用意されています。core/classes/validation.phpでは、

	public function _validation_min_length($val, $length)
	{
		return $this->_empty($val) || (MBSTRING ? mb_strlen($val) : strlen($val)) >= $length;
	}

というように、min_length, max_length, exact_length が用意されています。これらは、マルチバイト文字を1文字と判定して文字長を決めます。もちろんこの形式が望ましい場合が多いでしょうけど、画面表示などで使う文字列の場合、文字幅のほうが望ましい場合があるかもしれません。

FuelPHP 本体には無いようなので、つくってみました。

class MbWidthValidation
{
	/**
	 * Minimum string width
	 *
	 * @param   string
	 * @param   int
	 * @return  bool
	 */
	public static function _validation_min_width($val, $length)
	{
		return Validation::_empty($val) || (MBSTRING ? mb_strwidth($val) : strlen($val)*2) >= $length;
	}

	/**
	 * Maximum string width
	 *
	 * @param   string
	 * @param   int
	 * @return  bool
	 */
	public static function _validation_max_width($val, $length)
	{
		return Validation::_empty($val) || (MBSTRING ? mb_strwidth($val) : strlen($val)*2) <= $length;
	}

	/**
	 * Exact string width
	 *
	 * @param   string
	 * @param   int
	 * @return  bool
	 */
	public static function _validation_exact_width($val, $length)
	{
		return Validation::_empty($val) || (MBSTRING ? mb_strwidth($val) : strlen($val)*2) == $length;
	}
}

長さ判定は、mb_strwidthを使っています。全角文字=2、半角文字=1として判定します。

テストコードは下記です。

/**
 * Test_MbWidthValidation 
 * 
 * @group App
 */
class Test_MbWidthValidation extends TestCase
{
	/**
	 * Validation:  min_width
	 * Expecting:   success
	 *
	 */
	public function test_validation_min_width_success()
	{
		$input = 'あ';
		$output = MbWidthValidation::_validation_min_width($input,2);

		$expected = true;

		$this->assertEquals($expected, $output);
	}
	/**
	 * Validation:  min_width
	 * Expecting:   failure
	 *
	 */
	public function test_validation_min_width_failure()
	{
		$input = 'あo';
		$output = MbWidthValidation::_validation_min_width($input,4);

		$expected = false;

		$this->assertEquals($expected, $output);
	}
	
	/**
	 * Validation:  max_width
	 * Expecting:   success
	 *
	 */
	public function test_validation_max_width_success()
	{
		$input = 'あ';
		$output = MbWidthValidation::_validation_max_width($input,4);

		$expected = true;

		$this->assertEquals($expected, $output);
	}
	/**
	 * Validation:  max_width
	 * Expecting:   failure
	 *
	 */
	public function test_validation_max_width_failure()
	{
		$input = 'あo';
		$output = MbWidthValidation::_validation_max_width($input,2);

		$expected = false;

		$this->assertEquals($expected, $output);
	}
	
	/**
	 * Validation:  exact_width
	 * Expecting:   success
	 *
	 */
	public function test_validation_exact_width_success()
	{
		$input = 'あ';
		$output = MbWidthValidation::_validation_exact_width($input,2);

		$expected = true;

		$this->assertEquals($expected, $output);
	}
	/**
	 * Validation:  exact_width
	 * Expecting:   failure
	 *
	 */
	public function test_validation_exact_width_failure()
	{
		$input = 'あo';
		$output = MbWidthValidation::_validation_exact_width($input,4);

		$expected = false;

		$this->assertEquals($expected, $output);
	}
	
}

FuelPHP の本体のコードが、mb_strlen($val) という形で、文字エンコーディング指定が無いのですが、指定しておいたほうが良いかもしれません。

関連

Posted in fuelphp | Tagged オープンソース, バリデーション, 文字長
← オープンストリートマップのルート検索 Your Posts on Dashboard →

アーカイブ

人気の投稿とページ

  • キンドル本を印刷する(PDFに変換する)方法
  • 名古屋駅から国際センターまでの道のり(徒歩)
  • AGPL ライセンス(GPLとは似ているが違いもある)
  • 6年使ったイーモバイル(Y!mobile)を解約手続。店頭でSIM返却
  • JP-Secure SiteGuard WP Pluginは不正ログイン防止に役立つか

プロフィール

水野史土:月70万PVホームページ制作会社のレスキューワーク株式会社で、PHPソフトウェアのサポートを行っている。concrete5コミュニティリーダー、Novius OSコアコード貢献者でもある。 詳しくは管理者詳細参照。
大好評WordPress書籍「WordPressユーザーのためのPHP入門 はじめから、ていねいに。」サポートページ

Copyright © 2015 Standing on the Shoulder of Linus.