クラス yii\validators\UniqueValidator
UniqueValidatorは、指定されたデータベーステーブル内で属性値が一意であることを検証します。
UniqueValidatorは、検証対象の値が、ActiveRecordクラス$targetClassおよび属性$targetAttributeで指定されたテーブル列で一意であるかどうかを確認します。
以下は、このバリデーターを使用した検証ルールの例です。
// a1 needs to be unique
['a1', 'unique']
// a1 needs to be unique, but column a2 will be used to check the uniqueness of the a1 value
['a1', 'unique', 'targetAttribute' => 'a2']
// a1 and a2 need to be unique together, and they both will receive error message
[['a1', 'a2'], 'unique', 'targetAttribute' => ['a1', 'a2']]
// a1 and a2 need to be unique together, only a1 will receive error message
['a1', 'unique', 'targetAttribute' => ['a1', 'a2']]
// a1 needs to be unique by checking the uniqueness of both a2 and a3 (using a1 value)
['a1', 'unique', 'targetAttribute' => ['a2', 'a1' => 'a3']]
公開プロパティ
公開メソッド
保護されたメソッド
メソッド | 説明 | 定義元 |
---|---|---|
formatMessage() | I18Nを使用してメッセージをフォーマットするか、\Yii::$app が利用できない場合は単純なstrtrを使用します。 |
yii\validators\Validator |
validateValue() | 値を検証します。 | yii\validators\Validator |
プロパティの詳細
属性値の一意性をチェックするために使用されるDBクエリに適用される追加のフィルター。これは、追加のクエリ条件を表す文字列または配列(クエリ条件の形式についてはyii\db\Query::where()を参照)または署名function ($query)
を持つ匿名関数にすることができます。ここで、$query
は関数内で変更できるQueryオブジェクトです。
このバリデーターが常にマスターDBを使用するように強制されるかどうか
ユーザー定義のエラーメッセージ。
単一の属性を検証する場合、バリデーターによって適切に置き換えられる次のプレースホルダーを含めることができます。
{attribute}
: 検証される属性のラベル{value}
: 検証される属性の値
複数の属性を検証する場合、次のプレースホルダーを含めることができます。
{attributes}
: 検証される属性のラベル{values}
: 検証される属性の値
現在の属性値の一意性を検証するために使用する必要があるActiveRecord属性の名前。設定しない場合は、現在検証されている属性の名前を使用します。複数の列の一意性を同時に検証するために配列を使用できます。配列の値は一意性を検証するために使用される属性であり、配列のキーは値が検証される属性です。
ターゲット属性がどのように関連付けられているかを定義するAnd|or
現在の属性値の一意性を検証するために使用する必要があるActiveRecordクラスの名前。設定しない場合は、検証される属性のActiveRecordクラスを使用します。
$targetAttributeも参照してください。
メソッドの詳細
定義元: yii\base\Component::__call()
クラスメソッドではない名前付きメソッドを呼び出します。
このメソッドは、付加されたビヘイビアに指定されたメソッドがあるかどうかをチェックし、利用可能な場合は実行します。
未知のメソッドが呼び出されたときに暗黙的に呼び出されるPHPのマジックメソッドであるため、このメソッドを直接呼び出さないでください。
public mixed __call ( $name, $params ) | ||
$name | 文字列 |
メソッド名 |
$params | 配列 |
メソッドパラメータ |
return | mixed |
メソッドの戻り値 |
---|---|---|
throws | 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 | 配列 |
オブジェクトのプロパティを初期化するために使用される名前と値のペア |
public function __construct($config = [])
{
if (!empty($config)) {
Yii::configure($this, $config);
}
$this->init();
}
定義元: yii\base\Component::__get()
コンポーネントプロパティの値を返します。
このメソッドは、次の順序でチェックを行い、それに応じて動作します。
- ゲッターによって定義されたプロパティ: ゲッターの結果を返します。
- ビヘイビアのプロパティ: ビヘイビアのプロパティ値を返します。
このメソッドは、$value = $component->property;
を実行する際に暗黙的に呼び出される PHP のマジックメソッドであるため、直接呼び出さないでください。
関連項目: __set()。
public mixed __get ( $name ) | ||
$name | 文字列 |
プロパティ名 |
return | mixed |
プロパティ値またはビヘイビアのプロパティの値 |
---|---|---|
throws | yii\base\UnknownPropertyException |
プロパティが定義されていない場合 |
throws | 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
を返します。
このメソッドは、isset($component->property)
を実行する際に暗黙的に呼び出される PHP のマジックメソッドであるため、直接呼び出さないでください。
public boolean __isset ( $name ) | ||
$name | 文字列 |
プロパティ名またはイベント名 |
return | ブール値 |
指定されたプロパティが設定されているかどうか |
---|
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" という名前のビヘイビアをアタッチします。
- ビヘイビアのプロパティ: ビヘイビアのプロパティ値を設定します。
このメソッドは、$component->property = $value;
を実行する際に暗黙的に呼び出される PHP のマジックメソッドであるため、直接呼び出さないでください。
関連項目: __get()。
public void __set ( $name, $value ) | ||
$name | 文字列 |
プロパティ名またはイベント名 |
$value | mixed |
プロパティ値 |
throws | yii\base\UnknownPropertyException |
プロパティが定義されていない場合 |
---|---|---|
throws | 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 に設定します。
このメソッドは、unset($component->property)
を実行する際に暗黙的に呼び出される PHP のマジックメソッドであるため、直接呼び出さないでください。
public void __unset ( $name ) | ||
$name | 文字列 |
プロパティ名 |
throws | 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 | 文字列 |
検証される属性 |
$message | 文字列 |
エラーメッセージ |
$params | 配列 |
エラーメッセージ内のプレースホルダーの値 |
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 | 文字列 |
ビヘイビアの名前。 |
$behavior | string|array|yii\base\Behavior |
ビヘイビア設定。これは以下のいずれかになります。
|
return | 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 | 配列 |
コンポーネントにアタッチされるビヘイビアのリスト |
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 ( ) | ||
return | 配列 |
ビヘイビア設定。 |
---|
public function behaviors()
{
return [];
}
定義元: yii\base\Component::canGetProperty()
プロパティを読み取ることができるかどうかを示す値を返します。
プロパティは、次の条件が満たされた場合に読み取り可能です。
- クラスに、指定された名前に関連付けられたゲッターメソッドがある場合(この場合、プロパティ名は大文字と小文字を区別しません)。
- クラスに、指定された名前のメンバ変数がある場合 (
$checkVars
が true の場合)。 - アタッチされたビヘイビアに、指定された名前の読み取り可能なプロパティがある場合 (
$checkBehaviors
が true の場合)。
関連項目: canSetProperty()。
public boolean canGetProperty ( $name, $checkVars = true, $checkBehaviors = true ) | ||
$name | 文字列 |
プロパティ名 |
$checkVars | ブール値 |
メンバ変数をプロパティとして扱うかどうか。 |
$checkBehaviors | ブール値 |
ビヘイビアのプロパティをこのコンポーネントのプロパティとして扱うかどうか。 |
return | ブール値 |
プロパティが読み取り可能かどうか。 |
---|
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 | 文字列 |
プロパティ名 |
$checkVars | ブール値 |
メンバ変数をプロパティとして扱うかどうか。 |
$checkBehaviors | ブール値 |
ビヘイビアのプロパティをこのコンポーネントのプロパティとして扱うかどうか。 |
return | ブール値 |
プロパティが書き込み可能かどうか。 |
---|
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;
}
::class
を使用してください。
定義元: yii\base\BaseObject::className()
このクラスの完全修飾名を返します。
public static string className ( ) | ||
return | 文字列 |
このクラスの完全修飾名。 |
---|
public static function className()
{
return get_called_class();
}
定義元: yii\validators\Validator::clientValidateAttribute()
クライアント側検証を実行するために必要な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 | 文字列 |
検証する属性の名前。 |
$view | yii\web\View |
このバリデーターが適用されたモデルフォームを含むビューまたはビューファイルをレンダリングするために使用されるビューオブジェクト。 |
return | 文字列|null |
クライアントサイドの検証スクリプト。バリデーターがクライアントサイドの検証をサポートしていない場合は Null。 |
---|
public function clientValidateAttribute($model, $attribute, $view)
{
return null;
}
定義元: yii\validators\Validator::createValidator()
バリデーターオブジェクトを作成します。
public static yii\validators\Validator createValidator ( $type, $model, $attributes, $params = [] ) | ||
$type | string|Closure |
バリデーターのタイプ。これは、次のいずれかになります。
|
$model | yii\base\Model |
検証されるデータモデル。 |
$attributes | 配列|文字列 |
検証する属性のリスト。これは、属性名の配列、またはコンマ区切りの属性名の文字列のいずれかになります。 |
$params | 配列 |
バリデーターのプロパティに適用する初期値。 |
return | 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 | 文字列 |
ビヘイビアの名前。 |
return | 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 | 文字列 | |
$params | 配列 |
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 ( ) | ||
return | 配列 |
属性名。 |
---|
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 | 文字列 |
ビヘイビアの名前 |
return | 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 ( ) | ||
return | yii\base\Behavior[] |
このコンポーネントにアタッチされたビヘイビアのリスト |
---|
public function getBehaviors()
{
$this->ensureBehaviors();
return $this->_behaviors;
}
定義元: yii\validators\Validator::getClientOptions()
クライアント側検証オプションを返します。
通常、このメソッドは clientValidateAttribute() から呼び出されます。クライアントサイドのバリデーションに渡されるオプションを修正するために、このメソッドをオーバーライドすることができます。
public array getClientOptions ( $model, $attribute ) | ||
$model | yii\base\Model |
バリデーションされるモデル |
$attribute | 文字列 |
バリデーションされる属性名 |
return | 配列 |
クライアントサイドのバリデーションオプション |
---|
public function getClientOptions($model, $attribute)
{
return [];
}
定義元: yii\validators\Validator::getValidationAttributes()
このバリデーターが適用される属性のリストを返します。
public array|null getValidationAttributes ( $attributes = null ) | ||
$attributes | array|string|null |
バリデーションされる属性のリスト。
|
return | 配列|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 | 文字列 |
イベント名 |
return | ブール値 |
イベントにアタッチされたハンドラがあるかどうか。 |
---|
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 | 文字列 |
プロパティ名 |
$checkBehaviors | ブール値 |
ビヘイビアのメソッドをこのコンポーネントのメソッドとして扱うかどうか |
return | ブール値 |
メソッドが定義されているかどうか |
---|
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 | 文字列 |
プロパティ名 |
$checkVars | ブール値 |
メンバ変数をプロパティとして扱うかどうか。 |
$checkBehaviors | ブール値 |
ビヘイビアのプロパティをこのコンポーネントのプロパティとして扱うかどうか。 |
return | ブール値 |
プロパティが定義されているかどうか |
---|
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 ($this->message !== null) {
return;
}
if (is_array($this->targetAttribute) && count($this->targetAttribute) > 1) {
// fallback for deprecated `comboNotUnique` property - use it as message if is set
if ($this->comboNotUnique === null) {
$this->message = Yii::t('yii', 'The combination {values} of {attributes} has already been taken.');
} else {
$this->message = $this->comboNotUnique;
}
} else {
$this->message = Yii::t('yii', '{attribute} "{value}" has already been taken.');
}
}
定義元: yii\validators\Validator::isActive()
指定されたシナリオと属性に対してバリデーターがアクティブかどうかを示す値を返します。
バリデーターは、以下の場合にアクティブになります。
- バリデーターの
on
プロパティが空の場合、または - バリデーターの
on
プロパティが指定されたシナリオを含む場合
public boolean isActive ( $scenario ) | ||
$scenario | 文字列 |
シナリオ名 |
return | ブール値 |
バリデーターが指定されたシナリオに適用されるかどうか。 |
---|
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 |
チェックする値 |
return | ブール値 |
値が空かどうか |
---|
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 | 文字列 |
イベント名 |
$handler | callable|null |
削除するイベントハンドラー。null の場合、指定されたイベントにアタッチされたすべてのハンドラーが削除されます。 |
return | ブール値 |
ハンドラーが見つかり、デタッチされた場合 |
---|
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;
}
イベントハンドラーをイベントに付加します。
イベントハンドラーは、有効な 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 | 文字列 |
イベント名 |
$handler | callable |
イベントハンドラー |
$data | mixed |
イベントがトリガーされたときにイベントハンドラーに渡されるデータ。イベントハンドラーが呼び出されると、このデータは yii\base\Event::$data を介してアクセスできます。 |
$append | ブール値 |
新しいイベントハンドラーを既存のハンドラーリストの末尾に追加するかどうか。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 | 文字列 |
イベント名 |
$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 | 文字列|null |
検証に失敗した場合に返されるエラーメッセージ。 |
return | ブール値 |
データが有効かどうか。 |
---|
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 | 文字列 |
検証する属性の名前。 |
public function validateAttribute($model, $attribute)
{
$targetAttribute = $this->targetAttribute === null ? $attribute : $this->targetAttribute;
if ($this->skipOnError) {
foreach ((array)$targetAttribute as $k => $v) {
if ($model->hasErrors(is_int($k) ? $v : $k)) {
return;
}
}
}
$rawConditions = $this->prepareConditions($targetAttribute, $model, $attribute);
$conditions = [$this->targetAttributeJunction === 'or' ? 'or' : 'and'];
foreach ($rawConditions as $key => $value) {
if (is_array($value)) {
$this->addError($model, $attribute, Yii::t('yii', '{attribute} is invalid.'));
return;
}
$conditions[] = [$key => $value];
}
/* @var $targetClass ActiveRecordInterface */
$targetClass = $this->getTargetClass($model);
$db = $targetClass::getDb();
$modelExists = false;
if ($this->forceMasterDb && method_exists($db, 'useMaster')) {
$db->useMaster(function () use ($targetClass, $conditions, $model, &$modelExists) {
$modelExists = $this->modelExists($targetClass, $conditions, $model);
});
} else {
$modelExists = $this->modelExists($targetClass, $conditions, $model);
}
if ($modelExists) {
if (is_array($targetAttribute) && count($targetAttribute) > 1) {
$this->addComboNotUniqueError($model, $attribute);
} else {
$this->addError($model, $attribute, $this->message);
}
}
}
定義元: 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);
}
}
}
}
定義元: yii\validators\Validator::validateValue()
値を検証します。
バリデータークラスは、データモデルのコンテキスト外でデータ検証をサポートするためにこのメソッドを実装できます。
protected array|null validateValue ( $value ) | ||
$value | mixed |
検証するデータ値。 |
return | 配列|null |
エラーメッセージと、エラーメッセージに挿入されるパラメーターの配列。
} return null; データが有効な場合は Null を返す必要があります。 |
---|---|---|
throws | yii\base\NotSupportedException |
バリデーターがモデルなしのデータ検証をサポートしていない場合 |
protected function validateValue($value)
{
throw new NotSupportedException(get_class($this) . ' does not support validateValue().');
}
サインアップ または ログイン してコメントしてください。