クラス yii\validators\RangeValidator
RangeValidator は、属性値が値のリスト内にあることを検証します。
範囲は、$range プロパティで指定できます。$not プロパティを true に設定すると、バリデーターは属性値が指定された範囲に含まれていないことを確認します。
公開プロパティ
公開メソッド
保護されたメソッド
メソッド | 説明 | 定義元 |
---|---|---|
formatMessage() | I18Nを使用してメッセージをフォーマットします。\Yii::$app が利用できない場合は、単純なstrtrを使用します。 |
yii\validators\Validator |
validateValue() | 値を検証します。 | yii\validators\RangeValidator |
プロパティの詳細
検証ロジックを反転するかどうか。デフォルトはfalse。trueに設定すると、属性値は$rangeで定義された値のリストに含まれていなければなりません。
属性値が含まれるべき有効な値のリスト、またはそのようなリストを返す無名関数です。無名関数のシグネチャは以下のようになります。
function($model, $attribute) {
// compute range
return $range;
}
メソッドの詳細
定義先: yii\base\Component::__call()
クラスメソッドではない名前付きメソッドを呼び出します。
このメソッドは、アタッチされたビヘイビアに名前付きのメソッドがあるかどうかをチェックし、利用可能な場合は実行します。
これはPHPのマジックメソッドであり、不明なメソッドが呼び出された際に暗黙的に呼び出されるため、直接呼び出さないでください。
public mixed __call ( $name, $params ) | ||
$name | string |
メソッド名 |
$params | array |
メソッドパラメータ |
戻り値 | mixed |
メソッドの戻り値 |
---|---|---|
例外 | yii\base\UnknownMethodException |
不明なメソッドを呼び出した場合 |
public function __call($name, $params)
{
$this->ensureBehaviors();
foreach ($this->_behaviors as $object) {
if ($object->hasMethod($name)) {
return call_user_func_array([$object, $name], $params);
}
}
throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}
定義先: yii\base\Component::__clone()
このメソッドは、既存のオブジェクトをクローンしてオブジェクトが作成された後に呼び出されます。
ビヘイビアは古いオブジェクトにアタッチされているため、すべてのビヘイビアを削除します。
public void __clone ( ) |
public function __clone()
{
$this->_events = [];
$this->_eventWildcards = [];
$this->_behaviors = null;
}
定義先: yii\base\BaseObject::__construct()
コンストラクター。
デフォルトの実装は2つのことを行います。
- 指定された設定
$config
を使用してオブジェクトを初期化します。 - init()を呼び出します。
このメソッドを子クラスでオーバーライドする場合は、
- コンストラクタの最後のパラメータは、ここの
$config
のように、設定配列であることをお勧めします。 - コンストラクタの最後に親の実装を呼び出してください。
public void __construct ( $config = [] ) | ||
$config | array |
オブジェクトのプロパティを初期化するために使用される名前と値のペア |
public function __construct($config = [])
{
if (!empty($config)) {
Yii::configure($this, $config);
}
$this->init();
}
定義先: yii\base\Component::__get()
コンポーネントプロパティの値を返します。
このメソッドは、以下の順番でチェックし、それに応じて動作します。
- ゲッターで定義されたプロパティ:ゲッターの結果を返す
- ビヘイビアのプロパティ:ビヘイビアのプロパティ値を返す
これはPHPのマジックメソッドであり、$value = $component->property;
を実行した際に暗黙的に呼び出されるため、直接呼び出さないでください。
__set()も参照してください。
public mixed __get ( $name ) | ||
$name | string |
プロパティ名 |
戻り値 | mixed |
プロパティ値またはビヘイビアのプロパティの値 |
---|---|---|
例外 | yii\base\UnknownPropertyException |
プロパティが定義されていない場合 |
例外 | yii\base\InvalidCallException |
プロパティが書き込み専用の場合。 |
public function __get($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
// read property, e.g. getName()
return $this->$getter();
}
// behavior property
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->canGetProperty($name)) {
return $behavior->$name;
}
}
if (method_exists($this, 'set' . $name)) {
throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
}
throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
}
定義先: yii\base\Component::__isset()
プロパティが設定されているかどうか(つまり、定義されていてnullではない)を確認します。
このメソッドは、以下の順番でチェックし、それに応じて動作します。
- セッターで定義されたプロパティ:プロパティが設定されているかどうかを返す
- ビヘイビアのプロパティ:プロパティが設定されているかどうかを返す
- 存在しないプロパティに対しては
false
を返す
これはPHPのマジックメソッドであり、isset($component->property)
を実行した際に暗黙的に呼び出されるため、直接呼び出さないでください。
public boolean __isset ( $name ) | ||
$name | string |
プロパティ名またはイベント名 |
戻り値 | boolean |
名前付きのプロパティが設定されているかどうか |
---|
public function __isset($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
return $this->$getter() !== null;
}
// behavior property
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->canGetProperty($name)) {
return $behavior->$name !== null;
}
}
return false;
}
定義場所: yii\base\Component::__set()
コンポーネントプロパティの値を設定します。
このメソッドは、以下の順番でチェックし、それに応じて動作します。
- セッターによって定義されたプロパティ: プロパティの値を設定します。
- "on xyz"形式のイベント: イベント"xyz"にハンドラーをアタッチします。
- "as xyz"形式のビヘイビア: "xyz"という名前のビヘイビアをアタッチします。
- ビヘイビアのプロパティ: ビヘイビアのプロパティの値を設定します。
これはPHPのマジックメソッドであり、`$component->property = $value;`を実行した際に暗黙的に呼び出されるため、直接呼び出さないでください。
関連情報: __get().
public void __set ( $name, $value ) | ||
$name | string |
プロパティ名またはイベント名 |
$value | mixed |
プロパティの値 |
例外 | yii\base\UnknownPropertyException |
プロパティが定義されていない場合 |
---|---|---|
例外 | yii\base\InvalidCallException |
プロパティが読み取り専用の場合。 |
public function __set($name, $value)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
// set property
$this->$setter($value);
return;
} elseif (strncmp($name, 'on ', 3) === 0) {
// on event: attach event handler
$this->on(trim(substr($name, 3)), $value);
return;
} elseif (strncmp($name, 'as ', 3) === 0) {
// as behavior: attach behavior
$name = trim(substr($name, 3));
$this->attachBehavior($name, $value instanceof Behavior ? $value : Yii::createObject($value));
return;
}
// behavior property
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->canSetProperty($name)) {
$behavior->$name = $value;
return;
}
}
if (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
}
throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
}
定義場所: yii\base\Component::__unset()
コンポーネントプロパティをnullに設定します。
このメソッドは、以下の順番でチェックし、それに応じて動作します。
- セッターによって定義されたプロパティ: プロパティの値をnullに設定します。
- ビヘイビアのプロパティ: プロパティの値をnullに設定します。
これはPHPのマジックメソッドであり、`unset($component->property)`を実行した際に暗黙的に呼び出されるため、直接呼び出さないでください。
public void __unset ( $name ) | ||
$name | string |
プロパティ名 |
例外 | yii\base\InvalidCallException |
プロパティが読み取り専用の場合。 |
---|
public function __unset($name)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
$this->$setter(null);
return;
}
// behavior property
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->canSetProperty($name)) {
$behavior->$name = null;
return;
}
}
throw new InvalidCallException('Unsetting an unknown or read-only property: ' . get_class($this) . '::' . $name);
}
定義場所: yii\validators\Validator::addError()
指定された属性に関するエラーをモデルオブジェクトに追加します。
これは、メッセージの選択と国際化を行うヘルパーメソッドです。
public void addError ( $model, $attribute, $message, $params = [] ) | ||
$model | yii\base\Model |
検証対象のデータモデル |
$attribute | string |
検証対象の属性 |
$message | string |
エラーメッセージ |
$params | array |
エラーメッセージ内のプレースホルダーの値 |
public function addError($model, $attribute, $message, $params = [])
{
$params['attribute'] = $model->getAttributeLabel($attribute);
if (!isset($params['value'])) {
$value = $model->$attribute;
if (is_array($value)) {
$params['value'] = 'array()';
} elseif (is_object($value) && !method_exists($value, '__toString')) {
$params['value'] = '(object)';
} else {
$params['value'] = $value;
}
}
$model->addError($attribute, $this->formatMessage($message, $params));
}
定義場所: yii\base\Component::attachBehavior()
このコンポーネントにビヘイビアをアタッチします。
このメソッドは、指定された設定に基づいてビヘイビアオブジェクトを作成します。その後、yii\base\Behavior::attach()メソッドを呼び出すことで、ビヘイビアオブジェクトがこのコンポーネントにアタッチされます。
関連情報: detachBehavior().
public yii\base\Behavior attachBehavior ( $name, $behavior ) | ||
$name | string |
ビヘイビアの名前。 |
$behavior | string|array|yii\base\Behavior |
ビヘイビアの設定。以下のいずれかになります。
|
戻り値 | yii\base\Behavior |
ビヘイビアオブジェクト |
---|
public function attachBehavior($name, $behavior)
{
$this->ensureBehaviors();
return $this->attachBehaviorInternal($name, $behavior);
}
定義場所: yii\base\Component::attachBehaviors()
コンポーネントにビヘイビアのリストをアタッチします。
各ビヘイビアは名前でインデックスされ、yii\base\Behaviorオブジェクト、ビヘイビアクラスを指定する文字列、またはビヘイビアを作成するための設定配列のいずれかである必要があります。
関連情報: attachBehavior().
public void attachBehaviors ( $behaviors ) | ||
$behaviors | array |
コンポーネントにアタッチするビヘイビアのリスト |
public function attachBehaviors($behaviors)
{
$this->ensureBehaviors();
foreach ($behaviors as $name => $behavior) {
$this->attachBehaviorInternal($name, $behavior);
}
}
定義場所: yii\base\Component::behaviors()
このコンポーネントが従うべきビヘイビアのリストを返します。
子クラスはこのメソッドをオーバーライドして、ビヘイビアとして動作させたいビヘイビアを指定できます。
このメソッドの戻り値は、ビヘイビア名でインデックスされたビヘイビアオブジェクトまたは設定の配列である必要があります。ビヘイビアの設定は、ビヘイビアクラスを指定する文字列、または以下の構造の配列のいずれかになります。
'behaviorName' => [
'class' => 'BehaviorClass',
'property1' => 'value1',
'property2' => 'value2',
]
ビヘイビアクラスはyii\base\Behaviorを拡張する必要があることに注意してください。ビヘイビアは名前付きで、または匿名でアタッチできます。配列キーとして名前を使用した場合、この名前を使用して、getBehavior()を使用して後でビヘイビアを取得したり、detachBehavior()を使用してデタッチしたりできます。匿名のビヘイビアは取得またはデタッチできません。
このメソッドで宣言されたビヘイビアは、コンポーネントに自動的に(オンデマンドで)アタッチされます。
public array behaviors ( ) | ||
戻り値 | array |
ビヘイビアの設定。 |
---|
public function behaviors()
{
return [];
}
定義場所: yii\base\Component::canGetProperty()
プロパティを読み取ることができるかどうかを示す値を返します。
プロパティは、以下の場合に読み取ることができます。
- クラスに指定された名前と関連付けられたゲッターメソッドがある場合(この場合、プロパティ名はケースインセンシティブです)。
- クラスに指定された名前のメンバ変数がある場合(`$checkVars`がtrueの場合)。
- アタッチされたビヘイビアに、指定された名前の読み取り可能なプロパティがある場合(`$checkBehaviors`がtrueの場合)。
関連情報: canSetProperty().
public boolean canGetProperty ( $name, $checkVars = true, $checkBehaviors = true ) | ||
$name | string |
プロパティ名 |
$checkVars | boolean |
メンバ変数をプロパティとして扱うかどうか |
$checkBehaviors | boolean |
ビヘイビアのプロパティをこのコンポーネントのプロパティとして扱うかどうか |
戻り値 | boolean |
プロパティを読み取ることができるかどうか |
---|
public function canGetProperty($name, $checkVars = true, $checkBehaviors = true)
{
if (method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name)) {
return true;
} elseif ($checkBehaviors) {
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->canGetProperty($name, $checkVars)) {
return true;
}
}
}
return false;
}
定義場所: yii\base\Component::canSetProperty()
プロパティを設定できるかどうかを示す値を返します。
プロパティは、以下の場合に書き込むことができます。
- クラスに指定された名前と関連付けられたセッターメソッドがある場合(この場合、プロパティ名はケースインセンシティブです)。
- クラスに指定された名前のメンバ変数がある場合(`$checkVars`がtrueの場合)。
- アタッチされたビヘイビアに、指定された名前の書き込み可能なプロパティがある場合(`$checkBehaviors`がtrueの場合)。
関連情報: canGetProperty().
public boolean canSetProperty ( $name, $checkVars = true, $checkBehaviors = true ) | ||
$name | string |
プロパティ名 |
$checkVars | boolean |
メンバ変数をプロパティとして扱うかどうか |
$checkBehaviors | boolean |
ビヘイビアのプロパティをこのコンポーネントのプロパティとして扱うかどうか |
戻り値 | boolean |
プロパティを書き込むことができるかどうか |
---|
public function canSetProperty($name, $checkVars = true, $checkBehaviors = true)
{
if (method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name)) {
return true;
} elseif ($checkBehaviors) {
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->canSetProperty($name, $checkVars)) {
return true;
}
}
}
return false;
}
定義場所: yii\base\BaseObject::className()
このクラスの完全修飾名を返します。
public static string className ( ) | ||
戻り値 | string |
このクラスの完全修飾名。 |
---|
public static function className()
{
return get_called_class();
}
クライアントサイド検証を実行するために必要なJavaScriptを返します。
getClientOptions() を呼び出して、クライアントサイド検証のためのオプション配列を生成します。
バリデータがクライアントサイド検証をサポートできる場合、このメソッドをオーバーライドしてJavaScript検証コードを返すことができます。
以下のJavaScript変数は事前に定義されており、検証コードで使用できます。
attribute
: 検証対象の属性を表すオブジェクト。value
: 検証対象の値。messages
: 属性の検証エラーメッセージを保持するために使用される配列。deferred
: 非同期検証のための遅延オブジェクトを保持するために使用される配列。$form
: フォーム要素を含むjQueryオブジェクト。
attribute
オブジェクトは以下のプロパティを含みます。
id
: フォーム内の属性を一意に識別するID(例: "loginform-username")。name
: 属性名または式(例: 表形式入力の場合は "[0]content")。container
: 入力フィールドのコンテナのjQueryセレクタ。input
: フォームのコンテキストにおける入力フィールドのjQueryセレクタ。error
: コンテナのコンテキストにおけるエラータグのjQueryセレクタ。status
: 入力フィールドの状態。0: 空、未入力、1: 検証済み、2: 検証待ち、3: 検証中。
public string|null clientValidateAttribute ( $model, $attribute, $view ) | ||
$model | yii\base\Model |
検証対象のデータモデル |
$attribute | string |
検証対象の属性名。 |
$view | yii\web\View |
このバリデータが適用されたモデルフォームを含むビューまたはビューファイルをレンダリングするために使用されるビューオブジェクト。 |
戻り値 | string|null |
クライアントサイド検証スクリプト。バリデータがクライアントサイド検証をサポートしていない場合はNull。 |
---|
public function clientValidateAttribute($model, $attribute, $view)
{
if ($this->range instanceof \Closure) {
$this->range = call_user_func($this->range, $model, $attribute);
}
ValidationAsset::register($view);
$options = $this->getClientOptions($model, $attribute);
return 'yii.validation.range(value, messages, ' . Json::htmlEncode($options) . ');';
}
定義位置: yii\validators\Validator::createValidator()
バリデータオブジェクトを作成します。
public static yii\validators\Validator createValidator ( $type, $model, $attributes, $params = [] ) | ||
$type | string|Closure |
バリデータの種類。以下のいずれかです。
|
$model | yii\base\Model |
検証対象のデータモデル。 |
$attributes | array|string |
検証対象の属性のリスト。属性名の配列、またはコンマ区切りの属性名の文字列のいずれかです。 |
$params | array |
バリデータのプロパティに適用される初期値。 |
戻り値 | yii\validators\Validator |
バリデータ |
---|
public static function createValidator($type, $model, $attributes, $params = [])
{
$params['attributes'] = $attributes;
if ($type instanceof \Closure) {
$params['class'] = __NAMESPACE__ . '\InlineValidator';
$params['method'] = $type;
} elseif (!isset(static::$builtInValidators[$type]) && $model->hasMethod($type)) {
// method-based validator
$params['class'] = __NAMESPACE__ . '\InlineValidator';
$params['method'] = [$model, $type];
} else {
unset($params['current']);
if (isset(static::$builtInValidators[$type])) {
$type = static::$builtInValidators[$type];
}
if (is_array($type)) {
$params = array_merge($type, $params);
} else {
$params['class'] = $type;
}
}
return Yii::createObject($params);
}
定義位置: yii\base\Component::detachBehavior()
コンポーネントからビヘイビアをデタッチします。
ビヘイビアのyii\base\Behavior::detach()メソッドが呼び出されます。
public yii\base\Behavior|null detachBehavior ( $name ) | ||
$name | string |
ビヘイビアの名前。 |
戻り値 | yii\base\Behavior|null |
デタッチされたビヘイビア。ビヘイビアが存在しない場合はNull。 |
---|
public function detachBehavior($name)
{
$this->ensureBehaviors();
if (isset($this->_behaviors[$name])) {
$behavior = $this->_behaviors[$name];
unset($this->_behaviors[$name]);
$behavior->detach();
return $behavior;
}
return null;
}
定義位置: yii\base\Component::detachBehaviors()
コンポーネントからすべてのビヘイビアをデタッチします。
public void detachBehaviors ( ) |
public function detachBehaviors()
{
$this->ensureBehaviors();
foreach ($this->_behaviors as $name => $behavior) {
$this->detachBehavior($name);
}
}
定義位置: yii\base\Component::ensureBehaviors()
behaviors()で宣言されたビヘイビアがこのコンポーネントにアタッチされていることを確認します。
public void ensureBehaviors ( ) |
public function ensureBehaviors()
{
if ($this->_behaviors === null) {
$this->_behaviors = [];
foreach ($this->behaviors() as $name => $behavior) {
$this->attachBehaviorInternal($name, $behavior);
}
}
}
定義位置: yii\validators\Validator::formatMessage()
I18Nを使用してメッセージをフォーマットします。\Yii::$app
が利用できない場合は、単純なstrtrを使用します。
protected string formatMessage ( $message, $params ) | ||
$message | string | |
$params | array |
protected function formatMessage($message, $params)
{
if (Yii::$app !== null) {
return \Yii::$app->getI18n()->format($message, $params, Yii::$app->language);
}
$placeholders = [];
foreach ((array) $params as $name => $value) {
$placeholders['{' . $name . '}'] = $value;
}
return ($placeholders === []) ? $message : strtr($message, $placeholders);
}
定義位置: yii\validators\Validator::getAttributeNames()
先頭に!
文字がないクリーンな属性名を返します。
public array getAttributeNames ( ) | ||
戻り値 | array |
属性名。 |
---|
public function getAttributeNames()
{
return array_map(function ($attribute) {
return ltrim($attribute, '!');
}, $this->attributes);
}
定義位置: yii\base\Component::getBehavior()
名前付きのビヘイビアオブジェクトを返します。
public yii\base\Behavior|null getBehavior ( $name ) | ||
$name | string |
ビヘイビア名 |
戻り値 | yii\base\Behavior|null |
ビヘイビアオブジェクト。ビヘイビアが存在しない場合はnull。 |
---|
public function getBehavior($name)
{
$this->ensureBehaviors();
return isset($this->_behaviors[$name]) ? $this->_behaviors[$name] : null;
}
定義位置: yii\base\Component::getBehaviors()
このコンポーネントにアタッチされているすべてのビヘイビアを返します。
public yii\base\Behavior[] getBehaviors ( ) | ||
戻り値 | yii\base\Behavior[] |
このコンポーネントにアタッチされているビヘイビアのリスト |
---|
public function getBehaviors()
{
$this->ensureBehaviors();
return $this->_behaviors;
}
クライアントサイド検証のオプションを返します。
このメソッドは通常、clientValidateAttribute() から呼び出されます。クライアント側の検証に渡されるオプションを変更するために、このメソッドをオーバーライドできます。
public array getClientOptions ( $model, $attribute ) | ||
$model | yii\base\Model |
検証対象のモデル |
$attribute | string |
検証対象の属性名 |
戻り値 | array |
クライアント側の検証オプション |
---|
public function getClientOptions($model, $attribute)
{
$range = [];
foreach ($this->range as $value) {
$range[] = (string) $value;
}
$options = [
'range' => $range,
'not' => $this->not,
'message' => $this->formatMessage($this->message, [
'attribute' => $model->getAttributeLabel($attribute),
]),
];
if ($this->skipOnEmpty) {
$options['skipOnEmpty'] = 1;
}
if ($this->allowArray) {
$options['allowArray'] = 1;
}
return $options;
}
定義されている場所: yii\validators\Validator::getValidationAttributes()
このバリデータが適用される属性のリストを返します。
public array|null getValidationAttributes ( $attributes = null ) | ||
$attributes | array|string|null |
検証される属性のリスト。
|
戻り値 | array|null |
属性名のリスト。 |
---|
public function getValidationAttributes($attributes = null)
{
if ($attributes === null) {
return $this->getAttributeNames();
}
if (is_scalar($attributes)) {
$attributes = [$attributes];
}
$newAttributes = [];
$attributeNames = $this->getAttributeNames();
foreach ($attributes as $attribute) {
// do not strict compare, otherwise int attributes would fail due to to string conversion in getAttributeNames() using ltrim().
if (in_array($attribute, $attributeNames, false)) {
$newAttributes[] = $attribute;
}
}
return $newAttributes;
}
定義されている場所: yii\base\Component::hasEventHandlers()
名前付きのイベントにハンドラがアタッチされているかどうかを示す値を返します。
public boolean hasEventHandlers ( $name ) | ||
$name | string |
イベント名 |
戻り値 | boolean |
イベントにハンドラがアタッチされているかどうか。 |
---|
public function hasEventHandlers($name)
{
$this->ensureBehaviors();
if (!empty($this->_events[$name])) {
return true;
}
foreach ($this->_eventWildcards as $wildcard => $handlers) {
if (!empty($handlers) && StringHelper::matchWildcard($wildcard, $name)) {
return true;
}
}
return Event::hasHandlers($this, $name);
}
定義されている場所: yii\base\Component::hasMethod()
メソッドが定義されているかどうかを示す値を返します。
メソッドは、以下の場合に定義済みとみなされます。
- クラスが指定された名前のメソッドを持つ場合
- アタッチされたビヘイビアが指定された名前のメソッドを持つ場合 (
$checkBehaviors
が true の場合)。
public boolean hasMethod ( $name, $checkBehaviors = true ) | ||
$name | string |
プロパティ名 |
$checkBehaviors | boolean |
ビヘイビアのメソッドをこのコンポーネントのメソッドとして扱うかどうか |
戻り値 | boolean |
メソッドが定義されているかどうか |
---|
public function hasMethod($name, $checkBehaviors = true)
{
if (method_exists($this, $name)) {
return true;
} elseif ($checkBehaviors) {
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->hasMethod($name)) {
return true;
}
}
}
return false;
}
定義されている場所: yii\base\Component::hasProperty()
このコンポーネントにプロパティが定義されているかどうかを示す値を返します。
プロパティは、以下の場合に定義済みとみなされます。
- クラスが指定された名前と関連付けられたゲッターまたはセッターメソッドを持つ場合(この場合、プロパティ名はケースインセンシティブです)。
- クラスに指定された名前のメンバ変数がある場合(`$checkVars`がtrueの場合)。
- アタッチされたビヘイビアが指定された名前のプロパティを持つ場合 (
$checkBehaviors
が true の場合)。
参照
public boolean hasProperty ( $name, $checkVars = true, $checkBehaviors = true ) | ||
$name | string |
プロパティ名 |
$checkVars | boolean |
メンバ変数をプロパティとして扱うかどうか |
$checkBehaviors | boolean |
ビヘイビアのプロパティをこのコンポーネントのプロパティとして扱うかどうか |
戻り値 | boolean |
プロパティが定義されているかどうか |
---|
public function hasProperty($name, $checkVars = true, $checkBehaviors = true)
{
return $this->canGetProperty($name, $checkVars, $checkBehaviors) || $this->canSetProperty($name, false, $checkBehaviors);
}
オブジェクトを初期化します。
このメソッドは、オブジェクトが指定された設定で初期化された後、コンストラクタの最後に呼び出されます。
public void init ( ) |
public function init()
{
parent::init();
if (
!is_array($this->range)
&& !($this->range instanceof \Closure)
&& !($this->range instanceof \Traversable)
) {
throw new InvalidConfigException('The "range" property must be set.');
}
if ($this->message === null) {
$this->message = Yii::t('yii', '{attribute} is invalid.');
}
}
定義されている場所: yii\validators\Validator::isActive()
指定されたシナリオと属性に対してバリデータがアクティブかどうかを示す値を返します。
バリデータは、以下の場合にアクティブです。
- バリデータの
on
プロパティが空の場合、または - バリデータの
on
プロパティに指定されたシナリオが含まれている場合
public boolean isActive ( $scenario ) | ||
$scenario | string |
シナリオ名 |
戻り値 | boolean |
バリデータが指定されたシナリオに適用されるかどうか。 |
---|
public function isActive($scenario)
{
return !in_array($scenario, $this->except, true) && (empty($this->on) || in_array($scenario, $this->on, true));
}
定義されている場所: yii\validators\Validator::isEmpty()
指定された値が空かどうかをチェックします。
値は、null、空の配列、または空の文字列の場合、空とみなされます。このメソッドはPHPのempty()とは異なることに注意してください。値が0の場合はfalseを返します。
public boolean isEmpty ( $value ) | ||
$value | mixed |
チェックする値 |
戻り値 | boolean |
値が空かどうか |
---|
public function isEmpty($value)
{
if ($this->isEmpty !== null) {
return call_user_func($this->isEmpty, $value);
}
return $value === null || $value === [] || $value === '';
}
定義されている場所: yii\base\Component::off()
このコンポーネントから既存のイベントハンドラをデタッチします。
このメソッドはon()の反対です。
注:イベント名にワイルドカードパターンが渡された場合、このワイルドカードで登録されたハンドラのみが削除され、このワイルドカードと一致するプレーン名で登録されたハンドラは残ります。
参照 on().
public boolean off ( $name, $handler = null ) | ||
$name | string |
イベント名 |
$handler | callable|null |
削除するイベントハンドラ。これがnullの場合、指定されたイベントにアタッチされているすべてのハンドラが削除されます。 |
戻り値 | boolean |
ハンドラが見つかり、デタッチされた場合 |
---|
public function off($name, $handler = null)
{
$this->ensureBehaviors();
if (empty($this->_events[$name]) && empty($this->_eventWildcards[$name])) {
return false;
}
if ($handler === null) {
unset($this->_events[$name], $this->_eventWildcards[$name]);
return true;
}
$removed = false;
// plain event names
if (isset($this->_events[$name])) {
foreach ($this->_events[$name] as $i => $event) {
if ($event[0] === $handler) {
unset($this->_events[$name][$i]);
$removed = true;
}
}
if ($removed) {
$this->_events[$name] = array_values($this->_events[$name]);
return true;
}
}
// wildcard event names
if (isset($this->_eventWildcards[$name])) {
foreach ($this->_eventWildcards[$name] as $i => $event) {
if ($event[0] === $handler) {
unset($this->_eventWildcards[$name][$i]);
$removed = true;
}
}
if ($removed) {
$this->_eventWildcards[$name] = array_values($this->_eventWildcards[$name]);
// remove empty wildcards to save future redundant regex checks:
if (empty($this->_eventWildcards[$name])) {
unset($this->_eventWildcards[$name]);
}
}
}
return $removed;
}
定義されている場所: yii\base\Component::on()
イベントにイベントハンドラをアタッチします。
イベントハンドラは有効なPHPコールバックでなければなりません。いくつかの例を以下に示します。
function ($event) { ... } // anonymous function
[$object, 'handleClick'] // $object->handleClick()
['Page', 'handleClick'] // Page::handleClick()
'handleClick' // global function handleClick()
イベントハンドラは次のシグネチャで定義する必要があります。
function ($event)
ここで、$event
はイベントに関連付けられたパラメータを含むyii\base\Eventオブジェクトです。
2.0.14以降、イベント名をワイルドカードパターンとして指定できます。
$component->on('event.group.*', function ($event) {
Yii::trace($event->name . ' is triggered.');
});
参照 off().
public void on ( $name, $handler, $data = null, $append = true ) | ||
$name | string |
イベント名 |
$handler | callable |
イベントハンドラ |
$data | mixed |
イベントが発生したときにイベントハンドラに渡されるデータ。イベントハンドラが呼び出されると、このデータはyii\base\Event::$dataからアクセスできます。 |
$append | boolean |
新しいイベントハンドラを既存のハンドラリストの最後に追加するかどうかを指定します。falseの場合、新しいハンドラは既存のハンドラリストの先頭に挿入されます。 |
public function on($name, $handler, $data = null, $append = true)
{
$this->ensureBehaviors();
if (strpos($name, '*') !== false) {
if ($append || empty($this->_eventWildcards[$name])) {
$this->_eventWildcards[$name][] = [$handler, $data];
} else {
array_unshift($this->_eventWildcards[$name], [$handler, $data]);
}
return;
}
if ($append || empty($this->_events[$name])) {
$this->_events[$name][] = [$handler, $data];
} else {
array_unshift($this->_events[$name], [$handler, $data]);
}
}
定義されている場所: yii\base\Component::trigger()
イベントをトリガーします。
このメソッドはイベントの発生を表します。クラスレベルのハンドラを含む、イベントにアタッチされているすべてのハンドラを呼び出します。
public void trigger ( $name, yii\base\Event $event = null ) | ||
$name | string |
イベント名 |
$event | yii\base\Event|null |
イベントインスタンス。設定されていない場合、デフォルトのyii\base\Eventオブジェクトが作成されます。 |
public function trigger($name, Event $event = null)
{
$this->ensureBehaviors();
$eventHandlers = [];
foreach ($this->_eventWildcards as $wildcard => $handlers) {
if (StringHelper::matchWildcard($wildcard, $name)) {
$eventHandlers[] = $handlers;
}
}
if (!empty($this->_events[$name])) {
$eventHandlers[] = $this->_events[$name];
}
if (!empty($eventHandlers)) {
$eventHandlers = call_user_func_array('array_merge', $eventHandlers);
if ($event === null) {
$event = new Event();
}
if ($event->sender === null) {
$event->sender = $this;
}
$event->handled = false;
$event->name = $name;
foreach ($eventHandlers as $handler) {
$event->data = $handler[1];
call_user_func($handler[0], $event);
// stop further handling if the event is handled
if ($event->handled) {
return;
}
}
}
// invoke class-level attached handlers
Event::trigger($this, $name, $event);
}
public boolean validate ( $value, &$error = null ) | ||
$value | mixed |
検証されるデータ値。 |
$error | string|null |
検証に失敗した場合に返されるエラーメッセージ。 |
戻り値 | boolean |
データが有効かどうか。 |
---|
public function validate($value, &$error = null)
{
$result = $this->validateValue($value);
if (empty($result)) {
return true;
}
list($message, $params) = $result;
$params['attribute'] = Yii::t('yii', 'the input value');
if (is_array($value)) {
$params['value'] = 'array()';
} elseif (is_object($value)) {
$params['value'] = 'object';
} else {
$params['value'] = $value;
}
$error = $this->formatMessage($message, $params);
return false;
}
単一の属性を検証します。
子クラスは、実際の検証ロジックを提供するためにこのメソッドを実装する必要があります。
public void validateAttribute ( $model, $attribute ) | ||
$model | yii\base\Model |
検証されるデータモデル。 |
$attribute | string |
検証対象の属性名。 |
public function validateAttribute($model, $attribute)
{
if ($this->range instanceof \Closure) {
$this->range = call_user_func($this->range, $model, $attribute);
}
parent::validateAttribute($model, $attribute);
}
定義されている場所: yii\validators\Validator::validateAttributes()
指定されたオブジェクトを検証します。
public void validateAttributes ( $model, $attributes = null ) | ||
$model | yii\base\Model |
検証対象のデータモデル |
$attributes | array|string|null |
検証される属性のリスト。属性がバリデータに関連付けられていない場合、無視されます。このパラメータがnullの場合、$attributesにリストされているすべての属性が検証されます。 |
public function validateAttributes($model, $attributes = null)
{
$attributes = $this->getValidationAttributes($attributes);
foreach ($attributes as $attribute) {
$skip = $this->skipOnError && $model->hasErrors($attribute)
|| $this->skipOnEmpty && $this->isEmpty($model->$attribute);
if (!$skip) {
if ($this->when === null || call_user_func($this->when, $model, $attribute)) {
$this->validateAttribute($model, $attribute);
}
}
}
}
値を検証します。
バリデータクラスは、データモデルのコンテキスト外でのデータ検証をサポートするためにこのメソッドを実装できます。
protected array|null validateValue ( $value ) | ||
$value | mixed |
検証されるデータ値。 |
戻り値 | array|null |
エラーメッセージと、エラーメッセージに挿入されるパラメータの配列。
} return null; データが有効な場合はnullを返す必要があります。 |
---|---|---|
例外 | yii\base\NotSupportedException |
バリデータがモデルなしのデータ検証をサポートしていない場合 |
protected function validateValue($value)
{
$in = false;
if (
$this->allowArray
&& ($value instanceof \Traversable || is_array($value))
&& ArrayHelper::isSubset($value, $this->range, $this->strict)
) {
$in = true;
}
if (!$in && ArrayHelper::isIn($value, $this->range, $this->strict)) {
$in = true;
}
return $this->not !== $in ? null : [$this->message, []];
}
コメントするにはサインアップまたはログインしてください。