クラス yii\validators\StringValidator
StringValidator は、属性値が特定の長さであることを検証します。
このバリデーターは、文字列型の属性でのみ使用する必要があります。
パブリックプロパティ
パブリックメソッド
保護されたメソッド
メソッド | 説明 | 定義元 |
---|---|---|
formatMessage() | I18N を使用してメッセージをフォーマットします。または \Yii::$app が利用できない場合は単純な strtr を使用します。 |
yii\validators\Validator |
validateValue() | 値を検証します。 | yii\validators\StringValidator |
プロパティの詳細
検証する文字列値のエンコーディング(例:'UTF-8')。このプロパティが設定されていない場合は、yii\base\Application::$charset が使用されます。
最大長。設定されていない場合は、最大長制限がないことを意味します。
長すぎる文字列のカスタマイズされたメッセージについては、$tooLong を参照してください。
最小長。設定されていない場合は、最小長制限がないことを意味します。
短すぎる文字列のカスタマイズされたメッセージについては、$tooShort を参照してください。
値が文字列データ型であることを要求するかどうか。false の場合、任意のスカラー値は文字列と同等として扱われます。
メソッド詳細
定義元: yii\base\Component::__call()
クラスメソッドではない名前付きメソッドを呼び出します。
このメソッドは、アタッチされたビヘイビアに指定された名前のメソッドがあるかどうかを確認し、利用可能であればそれを実行します。
このメソッドは、未知のメソッドが呼び出されたときに暗黙的に呼び出されるPHPのマジックメソッドであるため、直接呼び出さないでください。
public mixed __call ( $name, $params ) | ||
$name | string |
メソッド名 |
$params | array |
メソッドのパラメータ |
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 | array |
オブジェクトのプロパティを初期化するために使用される名前と値のペア |
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 | string |
プロパティ名 |
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 | string |
プロパティ名またはイベント名 |
return | 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" という名前のビヘイビアをアタッチします。
- ビヘイビアのプロパティ:ビヘイビアのプロパティ値を設定します。
このメソッドは、$component->property = $value;
を実行したときに暗黙的に呼び出されるPHPのマジックメソッドであるため、直接呼び出さないでください。
__get()も参照してください。
public void __set ( $name, $value ) | ||
$name | string |
プロパティ名またはイベント名 |
$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 | string |
プロパティ名 |
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 | 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 |
ビヘイビアの設定。以下のいずれかです。
|
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 | 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 ( ) | ||
return | 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 |
ビヘイビアのプロパティをこのコンポーネントのプロパティとして扱うかどうか |
return | 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 |
ビヘイビアのプロパティをこのコンポーネントのプロパティとして扱うかどうか |
return | 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;
}
::class
を使用してください。
定義元: yii\base\BaseObject::className()
このクラスの完全修飾名を返します。
public static string className ( ) | ||
return | 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 |
このバリデーターが適用されたモデルフォームを含むビューまたはビューファイルのレンダリングに使用されるビューオブジェクト。 |
return | string|null |
クライアント側の検証スクリプト。バリデーターがクライアント側の検証をサポートしていない場合は Null。 |
---|
public function clientValidateAttribute($model, $attribute, $view)
{
ValidationAsset::register($view);
$options = $this->getClientOptions($model, $attribute);
return 'yii.validation.string(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 |
バリデーターのプロパティに適用される初期値。 |
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 | string |
ビヘイビアの名前。 |
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 | 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 ( ) | ||
return | 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 |
ビヘイビアの名前 |
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;
}
クライアント側の検証オプションを返します。
このメソッドは通常、clientValidateAttribute() から呼び出されます。クライアント側の検証に渡されるオプションを修正するために、このメソッドをオーバーライドすることができます。
public array getClientOptions ( $model, $attribute ) | ||
$model | yii\base\Model |
検証されるモデル |
$attribute | string |
検証される属性名 |
return | array |
クライアント側の検証オプション |
---|
public function getClientOptions($model, $attribute)
{
$label = $model->getAttributeLabel($attribute);
$options = [
'message' => $this->formatMessage($this->message, [
'attribute' => $label,
]),
];
if ($this->min !== null) {
$options['min'] = $this->min;
$options['tooShort'] = $this->formatMessage($this->tooShort, [
'attribute' => $label,
'min' => $this->min,
]);
}
if ($this->max !== null) {
$options['max'] = $this->max;
$options['tooLong'] = $this->formatMessage($this->tooLong, [
'attribute' => $label,
'max' => $this->max,
]);
}
if ($this->length !== null) {
$options['is'] = $this->length;
$options['notEqual'] = $this->formatMessage($this->notEqual, [
'attribute' => $label,
'length' => $this->length,
]);
}
if ($this->skipOnEmpty) {
$options['skipOnEmpty'] = 1;
}
return $options;
}
定義元: yii\validators\Validator::getValidationAttributes()
このバリデーターが適用される属性のリストを返します。
public array|null getValidationAttributes ( $attributes = null ) | ||
$attributes | array|string|null |
検証される属性のリスト。
|
return | 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 |
イベント名 |
return | 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 |
ビヘイビアのメソッドをこのコンポーネントのメソッドとして扱うかどうか |
return | 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()
このコンポーネントにプロパティが定義されているかどうかを示す値を返します。
プロパティが定義されている条件:
- クラスが指定された名前に関連付けられたゲッターまたはセッターメソッドを持っている場合(この場合、プロパティ名はcase-insensitiveです)。
- クラスが指定された名前のメンバー変数を持っている場合(
$checkVars
が true の場合)。 - アタッチされたビヘイビアが指定された名前のプロパティを持っている場合 (
$checkBehaviors
が true の場合)。
こちらも参照してください。
public boolean hasProperty ( $name, $checkVars = true, $checkBehaviors = true ) | ||
$name | string |
プロパティ名 |
$checkVars | boolean |
メンバー変数をプロパティとして扱うかどうか |
$checkBehaviors | boolean |
ビヘイビアのプロパティをこのコンポーネントのプロパティとして扱うかどうか |
return | 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->length)) {
if (isset($this->length[0])) {
$this->min = $this->length[0];
}
if (isset($this->length[1])) {
$this->max = $this->length[1];
}
$this->length = null;
}
if ($this->encoding === null) {
$this->encoding = Yii::$app ? Yii::$app->charset : 'UTF-8';
}
if ($this->message === null) {
$this->message = Yii::t('yii', '{attribute} must be a string.');
}
if ($this->min !== null && $this->tooShort === null) {
$this->tooShort = Yii::t('yii', '{attribute} should contain at least {min, number} {min, plural, one{character} other{characters}}.');
}
if ($this->max !== null && $this->tooLong === null) {
$this->tooLong = Yii::t('yii', '{attribute} should contain at most {max, number} {max, plural, one{character} other{characters}}.');
}
if ($this->length !== null && $this->notEqual === null) {
$this->notEqual = Yii::t('yii', '{attribute} should contain {length, number} {length, plural, one{character} other{characters}}.');
}
}
定義元: yii\validators\Validator::isActive()
バリデーターが、指定されたシナリオと属性に対してアクティブかどうかを示す値を返します。
バリデーターがアクティブになる条件:
- バリデーターの
on
プロパティが空の場合、または - バリデーターの
on
プロパティが指定されたシナリオを含んでいる場合
public boolean isActive ( $scenario ) | ||
$scenario | string |
シナリオ名 |
return | 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 |
チェックする値 |
return | 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の場合、名前付きイベントにアタッチされたすべてのハンドラーが削除されます。 |
return | 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;
}
イベントにイベントハンドラーをアタッチします。
イベントハンドラーは有効な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 |
検証が失敗した場合に返されるエラーメッセージ。 |
return | 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)
{
$value = $model->$attribute;
if (!$this->strict && is_scalar($value) && !is_string($value)) {
$value = (string)$value;
}
if (!is_string($value)) {
$this->addError($model, $attribute, $this->message);
return;
}
$length = mb_strlen($value, $this->encoding);
if ($this->min !== null && $length < $this->min) {
$this->addError($model, $attribute, $this->tooShort, ['min' => $this->min]);
}
if ($this->max !== null && $length > $this->max) {
$this->addError($model, $attribute, $this->tooLong, ['max' => $this->max]);
}
if ($this->length !== null && $length !== $this->length) {
$this->addError($model, $attribute, $this->notEqual, ['length' => $this->length]);
}
}
定義場所: 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 |
検証するデータ値。 |
return | array|null |
エラーメッセージと、エラーメッセージに挿入されるパラメータの配列。
} return null; データが有効な場合は、Null を返す必要があります。 |
---|---|---|
throws | yii\base\NotSupportedException |
モデルなしでのデータ検証をサポートしていないバリデーターの場合 |
protected function validateValue($value)
{
if (!$this->strict && is_scalar($value) && !is_string($value)) {
$value = (string)$value;
}
if (!is_string($value)) {
return [$this->message, []];
}
$length = mb_strlen($value, $this->encoding);
if ($this->min !== null && $length < $this->min) {
return [$this->tooShort, ['min' => $this->min]];
}
if ($this->max !== null && $length > $this->max) {
return [$this->tooLong, ['max' => $this->max]];
}
if ($this->length !== null && $length !== $this->length) {
return [$this->notEqual, ['length' => $this->length]];
}
return null;
}
コメントするには、サインアップ または ログインしてください。