2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

FuelPHPのValidationに直接独自ルールを追加する(PHP>=5.4)

Last updated at Posted at 2014-12-25

Validationの拡張方法はあるけど既存ルールと同じようにFuel\Core\Validation内の内部メソッドを使って書いたりできないので不便。
traitを使って直接拡張することで$thisが使えるので便利になる。

#ソース

  • Fuel\Core\Validationを継承して新規クラスを作成
fuel/app/classes/validation.php
<?php

class Validation extends Fuel\Core\Validation
{
	use Validation_Required;
}
  • 新規ルールを作成
fuel/app/classes/validation/required.php
<?php

trait Validation_Required {

	/**
	 * いずれかひとつ必須
	 */
	public function _validation_required_in_array( $val , $fields = array() ) {
		
  • Bootstrapに新規クラスを追加
fuel/app/bootstrap.php
Autoloader::add_classes(array(
	// Add classes you want to override here
	// Example: 'View' => APPPATH.'classes/view.php',
	'Validation' => APPPATH.'classes/validation.php',
));
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?