クラス yii\widgets\ActiveField
継承 | yii\widgets\ActiveField » yii\base\Component » yii\base\BaseObject |
---|---|
実装 | yii\base\Configurable |
利用可能なバージョン | 2.0 |
ソースコード | https://github.com/yiisoft/yii2/blob/master/framework/widgets/ActiveField.php |
ActiveField は、yii\widgets\ActiveForm 内のフォーム入力フィールドを表します。
ActiveField の詳細および使用方法については、フォームに関するガイド記事を参照してください。
パブリックプロパティ
パブリックメソッド
保護されたメソッド
メソッド | 説明 | 定義元 |
---|---|---|
addAriaAttributes() | 入力オプションに aria 属性を追加します。 | yii\widgets\ActiveField |
addErrorClassIfNeeded() | 必要に応じて入力オプションに検証クラスを追加します。 | yii\widgets\ActiveField |
addRoleAttributes() | 入力オプションにロール属性を追加します | yii\widgets\ActiveField |
adjustLabelFor() | 入力オプションに基づいてラベルの for 属性を調整します。 |
yii\widgets\ActiveField |
getClientOptions() | フィールドの JS オプションを返します。 | yii\widgets\ActiveField |
getInputId() | このフォームフィールドの入力要素の HTML id を返します。 |
yii\widgets\ActiveField |
isAjaxValidationEnabled() | フィールドで AJAX 検証が有効になっているかどうかを確認します。 | yii\widgets\ActiveField |
isClientValidationEnabled() | フィールドでクライアント側の検証が有効になっているかどうかを確認します。 | yii\widgets\ActiveField |
プロパティの詳細
入力に対して、aria HTML属性 aria-required
および aria-invalid
を追加します
AJAXベースのデータ検証を有効にするかどうか。設定されていない場合、yii\widgets\ActiveForm::$enableAjaxValidation の値が使用されます。
クライアントサイドのデータ検証を有効にするかどうか。設定されていない場合、yii\widgets\ActiveForm::$enableClientValidation の値が使用されます。
エラータグのデフォルトオプション。 error() に渡されるパラメータは、エラータグをレンダリングする際にこのプロパティとマージされます。以下の特別なオプションが認識されます。
tag
: コンテナ要素のタグ名。デフォルトはdiv
。false
に設定すると、コンテナタグはレンダリングされません。 yii\helpers\Html::tag() も参照してください。encode
: エラー出力をエンコードするかどうか。デフォルトはtrue
。
エラー要素にカスタム id
を設定した場合、それに応じて $selectors を調整する必要があるかもしれません。
属性がどのようにレンダリングされるかの詳細については、yii\helpers\Html::renderTagAttributes() を参照してください。
ヒントタグのデフォルトオプション。 hint() に渡されるパラメータは、ヒントタグをレンダリングする際にこのプロパティとマージされます。以下の特別なオプションが認識されます。
tag
: コンテナ要素のタグ名。デフォルトはdiv
。false
に設定すると、コンテナタグはレンダリングされません。 yii\helpers\Html::tag() も参照してください。
属性がどのようにレンダリングされるかの詳細については、yii\helpers\Html::renderTagAttributes() を参照してください。
入力タグのデフォルトオプション。個々の入力メソッド(例:textInput())に渡されるパラメータは、入力タグをレンダリングする際にこのプロパティとマージされます。
入力要素にカスタム id
を設定した場合、それに応じて $selectors を調整する必要があるかもしれません。
属性がどのようにレンダリングされるかの詳細については、yii\helpers\Html::renderTagAttributes() を参照してください。
ラベルタグのデフォルトオプション。 label() に渡されるパラメータは、ラベルタグをレンダリングする際にこのプロパティとマージされます。
属性がどのようにレンダリングされるかの詳細については、yii\helpers\Html::renderTagAttributes() を参照してください。
フィールドコンテナタグのHTML属性(名前と値のペア)。値は yii\helpers\Html::encode() を使用してHTMLエンコードされます。値が null
の場合、対応する属性はレンダリングされません。以下の特別なオプションが認識されます。
tag
: コンテナ要素のタグ名。デフォルトはdiv
。false
に設定すると、コンテナタグはレンダリングされません。 yii\helpers\Html::tag() も参照してください。
コンテナ要素にカスタム id
を設定した場合、それに応じて $selectors を調整する必要があるかもしれません。
属性がどのようにレンダリングされるかの詳細については、yii\helpers\Html::renderTagAttributes() を参照してください。
コンテナ、入力、およびエラータグを選択するためのjQueryセレクター。配列のキーは container
、input
、および/または error
である必要があり、配列の値は対応するセレクターです。例えば、['input' => '#my-input']
のようになります。
コンテナセレクターはフォームのコンテキストで使用され、入力およびエラーセレクターはコンテナのコンテキストで使用されます。
通常、デフォルトのセレクターはほとんどの場合でうまく機能するため、このプロパティを設定する必要はありません。
ラベル、入力フィールド、エラーメッセージ、およびヒントテキストを配置するために使用されるテンプレート。 render() が呼び出されると、以下のトークンが置き換えられます。{label}
、{input}
、{error}
、および {hint}
。
入力フィールドがフォーカスを失ったときに検証を実行するかどうか。設定されていない場合は、yii\widgets\ActiveForm::$validateOnBlur の値が使用されます。
入力フィールドの値が変更されたときに検証を実行するかどうか。設定されていない場合は、yii\widgets\ActiveForm::$validateOnChange の値が使用されます。
ユーザーが入力フィールドに入力している間に検証を実行するかどうか。設定されていない場合は、yii\widgets\ActiveForm::$validateOnType の値が使用されます。
$validationDelay も参照してください。
ユーザーがフィールドに入力し、$validateOnType が true
に設定されている場合に、検証を遅延させる必要があるミリ秒数。設定されていない場合は、yii\widgets\ActiveForm::$validationDelay の値が使用されます。
メソッドの詳細
定義元: 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);
}
このオブジェクトの文字列表現を返す PHP マジックメソッド。
public string __toString ( ) | ||
return | string |
このオブジェクトの文字列表現。 |
---|
public function __toString()
{
// __toString cannot throw exception
// use trigger_error to bypass this limitation
try {
return $this->render();
} catch (\Exception $e) {
ErrorHandler::convertExceptionToError($e);
return '';
} catch (\Throwable $e) {
ErrorHandler::convertExceptionToError($e);
return '';
}
}
定義元: 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);
}
入力オプションに aria 属性を追加します。
protected void addAriaAttributes ( &$options ) | ||
$options |
配列入力オプション |
protected function addAriaAttributes(&$options)
{
// Get proper attribute name when attribute name is tabular.
$attributeName = Html::getAttributeName($this->attribute);
if ($this->addAriaAttributes) {
if (!isset($options['aria-required']) && $this->model->isAttributeRequired($attributeName)) {
$options['aria-required'] = 'true';
}
if (!isset($options['aria-invalid']) && $this->model->hasErrors($attributeName)) {
$options['aria-invalid'] = 'true';
}
}
}
必要に応じて入力オプションに検証クラスを追加します。
protected void addErrorClassIfNeeded ( &$options ) | ||
$options |
配列入力オプション |
protected function addErrorClassIfNeeded(&$options)
{
// Get proper attribute name when attribute name is tabular.
$attributeName = Html::getAttributeName($this->attribute);
if ($this->model->hasErrors($attributeName)) {
Html::addCssClass($options, $this->form->errorCssClass);
}
}
入力オプションにロール属性を追加します
protected void addRoleAttributes ( &$options, $role ) | ||
$options |
配列入力オプション |
|
$role | string |
protected function addRoleAttributes(&$options, $role)
{
if (!isset($options['role'])) {
$options['role'] = $role;
}
}
入力オプションに基づいてラベルの for
属性を調整します。
protected void adjustLabelFor ( $options ) | ||
$options | array |
入力オプション。 |
protected function adjustLabelFor($options)
{
if (!isset($options['id'])) {
return;
}
$this->_inputId = $options['id'];
if (!isset($this->labelOptions['for'])) {
$this->labelOptions['for'] = $options['id'];
}
}
定義元: yii\base\Component::attachBehavior()
このコンポーネントにビヘイビアをアタッチします。
このメソッドは、指定された構成に基づいてビヘイビアオブジェクトを作成します。その後、yii\base\Behavior::attach()メソッドを呼び出すことにより、ビヘイビアオブジェクトはこのコンポーネントにアタッチされます。
関連項目: detachBehavior()。
public yii\base\Behavior attachBehavior ( $name, $behavior ) | ||
$name | string |
ビヘイビアの名前。 |
$behavior | string|array|yii\base\Behavior |
ビヘイビアの構成。以下の中から1つを指定できます。
|
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);
}
}
フィールドコンテナの開始タグをレンダリングします。
public string begin ( ) | ||
return | string |
レンダリング結果。 |
---|
public function begin()
{
if ($this->form->enableClientScript) {
$clientOptions = $this->getClientOptions();
if (!empty($clientOptions)) {
$this->form->attributes[] = $clientOptions;
}
}
$inputID = $this->getInputId();
$attribute = Html::getAttributeName($this->attribute);
$options = $this->options;
$class = isset($options['class']) ? (array) $options['class'] : [];
$class[] = "field-$inputID";
if ($this->model->isAttributeRequired($attribute)) {
$class[] = $this->form->requiredCssClass;
}
$options['class'] = implode(' ', $class);
if ($this->form->validationStateOn === ActiveForm::VALIDATION_STATE_ON_CONTAINER) {
$this->addErrorClassIfNeeded($options);
}
$tag = ArrayHelper::remove($options, 'tag', 'div');
return Html::beginTag($tag, $options);
}
定義元: 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()
プロパティを読み取ることができるかどうかを示す値を返します。
プロパティは、以下の場合に読み取り可能です。
- クラスが、指定された名前に関連付けられた getter メソッドを持っている場合 (この場合、プロパティ名は大小文字を区別しません)。
- クラスが、指定された名前のメンバ変数を持っている場合 (
$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()
プロパティを設定できるかどうかを示す値を返します。
プロパティは、以下の場合に書き込み可能です。
- クラスが、指定された名前に関連付けられた setter メソッドを持っている場合 (この場合、プロパティ名は大小文字を区別しません)。
- クラスが、指定された名前のメンバ変数を持っている場合 (
$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;
}
チェックボックスをレンダリングします。
このメソッドは、モデル属性値に従って checked
タグ属性を生成します。
public $this checkbox ( $options = [], $enclosedByLabel = true ) | ||
$options | array |
名前と値のペアの形式のタグオプション。以下のオプションは特別に処理されます。
残りのオプションは、結果のタグの属性としてレンダリングされます。値は yii\helpers\Html::encode() を使用して HTML エンコードされます。値が 入力要素にカスタム |
$enclosedByLabel | boolean |
チェックボックスをラベル内に囲むかどうか。 |
return | $this |
フィールドオブジェクト自体。 |
---|
public function checkbox($options = [], $enclosedByLabel = true)
{
if ($this->form->validationStateOn === ActiveForm::VALIDATION_STATE_ON_INPUT) {
$this->addErrorClassIfNeeded($options);
}
$this->addAriaAttributes($options);
$this->adjustLabelFor($options);
if ($enclosedByLabel) {
$this->parts['{input}'] = Html::activeCheckbox($this->model, $this->attribute, $options);
$this->parts['{label}'] = '';
} else {
if (isset($options['label']) && !isset($this->parts['{label}'])) {
$this->parts['{label}'] = $options['label'];
if (!empty($options['labelOptions'])) {
$this->labelOptions = $options['labelOptions'];
}
}
unset($options['labelOptions']);
$options['label'] = null;
$this->parts['{input}'] = Html::activeCheckbox($this->model, $this->attribute, $options);
}
return $this;
}
チェックボックスのリストをレンダリングします。
チェックボックスリストでは、listBox() のように、複数選択が可能です。その結果、対応する送信値は配列になります。チェックボックスリストの選択は、モデル属性の値から取得されます。
public $this checkboxList ( $items, $options = [] ) | ||
$items | array |
チェックボックスの生成に使用されるデータ項目。配列の値はラベルであり、配列キーは対応するチェックボックスの値です。 |
$options | array |
チェックボックスリストのオプション (name => config)。利用可能なオプションの一覧については、yii\helpers\Html::activeCheckboxList() の |
return | $this |
フィールドオブジェクト自体。 |
---|
public function checkboxList($items, $options = [])
{
if ($this->form->validationStateOn === ActiveForm::VALIDATION_STATE_ON_INPUT) {
$this->addErrorClassIfNeeded($options);
}
$this->addAriaAttributes($options);
$this->adjustLabelFor($options);
$this->_skipLabelFor = true;
$this->parts['{input}'] = Html::activeCheckboxList($this->model, $this->attribute, $items, $options);
return $this;
}
::class
を使用してください。
定義元: yii\base\BaseObject::className()
このクラスの完全修飾名を返します。
public static string className ( ) | ||
return | string |
このクラスの完全修飾名。 |
---|
public static function className()
{
return get_called_class();
}
定義元: 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);
}
}
ドロップダウンリストをレンダリングします。
ドロップダウンリストの選択は、モデル属性の値から取得されます。
public $this dropDownList ( $items, $options = [] ) | ||
$items | array |
オプションのデータ項目。配列のキーはオプションの値、配列の値は対応するオプションのラベルです。配列はネストすることもできます(つまり、一部の配列の値も配列です)。各サブ配列に対して、ラベルがサブ配列に関連付けられたキーであるオプショングループが生成されます。データモデルのリストがある場合は、yii\helpers\ArrayHelper::map() を使用して上記形式に変換できます。 注意: 値とラベルは、このメソッドによって自動的に HTML エンコードされ、ラベルの空白も HTML エンコードされます。 |
$options | array |
名前と値のペアの形式のタグオプション。 利用可能なオプションのリストについては、yii\helpers\Html::activeDropDownList() の 入力要素にカスタム |
return | $this |
フィールドオブジェクト自体。 |
---|
public function dropDownList($items, $options = [])
{
$options = array_merge($this->inputOptions, $options);
if ($this->form->validationStateOn === ActiveForm::VALIDATION_STATE_ON_INPUT) {
$this->addErrorClassIfNeeded($options);
}
$this->addAriaAttributes($options);
$this->adjustLabelFor($options);
$this->parts['{input}'] = Html::activeDropDownList($this->model, $this->attribute, $items, $options);
return $this;
}
フィールドコンテナの終了タグをレンダリングします。
public string end ( ) | ||
return | string |
レンダリング結果。 |
---|
public function end()
{
return Html::endTag(ArrayHelper::keyExists('tag', $this->options) ? $this->options['tag'] : 'div');
}
定義場所: 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);
}
}
}
$attribute の最初の検証エラーを含むタグを生成します。
バリデーションエラーがない場合でも、このメソッドは空のエラータグを返すことに注意してください。
$errorOptions も参照してください。
public $this error ( $options = [] ) | ||
$options | array|false |
名前と値のペアの形式のタグオプション。これは $errorOptions とマージされます。オプションは、結果として得られるタグの属性としてレンダリングされます。値は、yii\helpers\Html::encode() を使用して HTML エンコードされます。このパラメータが 次のオプションは特別に処理されます
エラー要素にカスタム |
return | $this |
フィールドオブジェクト自体。 |
---|
public function error($options = [])
{
if ($options === false) {
$this->parts['{error}'] = '';
return $this;
}
$options = array_merge($this->errorOptions, $options);
$this->parts['{error}'] = Html::error($this->model, $this->attribute, $options);
return $this;
}
ファイル入力をレンダリングします。
このメソッドは、$options
で明示的に指定されていない限り、モデル属性の name
および value
タグ属性を自動的に生成します。
public $this fileInput ( $options = [] ) | ||
$options | array |
名前と値のペアの形式のタグオプション。これらは、結果として得られるタグの属性としてレンダリングされます。値は、yii\helpers\Html::encode() を使用して HTML エンコードされます。 入力要素にカスタム |
return | $this |
フィールドオブジェクト自体。 |
---|
public function fileInput($options = [])
{
// https://github.com/yiisoft/yii2/pull/795
if ($this->inputOptions !== ['class' => 'form-control']) {
$options = array_merge($this->inputOptions, $options);
}
// https://github.com/yiisoft/yii2/issues/8779
if (!isset($this->form->options['enctype'])) {
$this->form->options['enctype'] = 'multipart/form-data';
}
if ($this->form->validationStateOn === ActiveForm::VALIDATION_STATE_ON_INPUT) {
$this->addErrorClassIfNeeded($options);
}
$this->addAriaAttributes($options);
$this->adjustLabelFor($options);
$this->parts['{input}'] = Html::activeFileInput($this->model, $this->attribute, $options);
return $this;
}
定義場所: 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;
}
フィールドの JS オプションを返します。
protected array getClientOptions ( ) | ||
return | array |
JS オプション。 |
---|
protected function getClientOptions()
{
$attribute = Html::getAttributeName($this->attribute);
if (!in_array($attribute, $this->model->activeAttributes(), true)) {
return [];
}
$clientValidation = $this->isClientValidationEnabled();
$ajaxValidation = $this->isAjaxValidationEnabled();
if ($clientValidation) {
$validators = [];
foreach ($this->model->getActiveValidators($attribute) as $validator) {
/* @var $validator \yii\validators\Validator */
$js = $validator->clientValidateAttribute($this->model, $attribute, $this->form->getView());
if ($validator->enableClientValidation && $js != '') {
if ($validator->whenClient !== null) {
$js = "if (({$validator->whenClient})(attribute, value)) { $js }";
}
$validators[] = $js;
}
}
}
if (!$ajaxValidation && (!$clientValidation || empty($validators))) {
return [];
}
$options = [];
$inputID = $this->getInputId();
$options['id'] = $inputID ?: Html::getInputId($this->model, $this->attribute);
$options['name'] = $this->attribute;
$options['container'] = isset($this->selectors['container']) ? $this->selectors['container'] : ".field-$inputID";
$options['input'] = isset($this->selectors['input']) ? $this->selectors['input'] : "#$inputID";
if (isset($this->selectors['error'])) {
$options['error'] = $this->selectors['error'];
} elseif (isset($this->errorOptions['class'])) {
$options['error'] = '.' . implode('.', preg_split('/\s+/', $this->errorOptions['class'], -1, PREG_SPLIT_NO_EMPTY));
} else {
$options['error'] = isset($this->errorOptions['tag']) ? $this->errorOptions['tag'] : 'span';
}
$options['encodeError'] = !isset($this->errorOptions['encode']) || $this->errorOptions['encode'];
if ($ajaxValidation) {
$options['enableAjaxValidation'] = true;
}
foreach (['validateOnChange', 'validateOnBlur', 'validateOnType', 'validationDelay'] as $name) {
$options[$name] = $this->$name === null ? $this->form->$name : $this->$name;
}
if (!empty($validators)) {
$options['validate'] = new JsExpression('function (attribute, value, messages, deferred, $form) {' . implode('', $validators) . '}');
}
if ($this->addAriaAttributes === false) {
$options['updateAriaInvalid'] = false;
}
// only get the options that are different from the default ones (set in yii.activeForm.js)
return array_diff_assoc($options, [
'validateOnChange' => true,
'validateOnBlur' => true,
'validateOnType' => false,
'validationDelay' => 500,
'encodeError' => true,
'error' => '.help-block',
'updateAriaInvalid' => true,
]);
}
このフォームフィールドの入力要素の HTML id
を返します。
protected string getInputId ( ) | ||
return | string |
入力 ID。 |
---|
protected function getInputId()
{
return $this->_inputId ?: Html::getInputId($this->model, $this->attribute);
}
定義場所: 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()
このコンポーネントにプロパティが定義されているかどうかを示す値を返します。
プロパティは以下の場合に定義されます
- クラスが指定された名前に関連付けられたゲッターまたはセッターメソッドを持っている場合(この場合、プロパティ名は大文字と小文字を区別しません)。
- クラスが、指定された名前のメンバ変数を持っている場合 (
$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 $this hint ( $content, $options = [] ) | ||
$content | string|boolean|null |
ヒントの内容。 |
$options | array |
名前と値のペアで表されるタグオプション。これらはヒントタグの属性としてレンダリングされます。値は yii\helpers\Html::encode() を使用して HTML エンコードされます。 次のオプションは特別に処理されます
|
return | $this |
フィールドオブジェクト自体。 |
---|
public function hint($content, $options = [])
{
if ($content === false) {
$this->parts['{hint}'] = '';
return $this;
}
$options = array_merge($this->hintOptions, $options);
if ($content !== null) {
$options['hint'] = $content;
}
$this->parts['{hint}'] = Html::activeHint($this->model, $this->attribute, $options);
return $this;
}
public void init ( ) |
public function init()
{
}
入力タグをレンダリングします。
public $this input ( $type, $options = [] ) | ||
$type | string |
入力タイプ (例: |
$options | array |
名前と値のペアの形式のタグオプション。これらは、結果として得られるタグの属性としてレンダリングされます。値は、yii\helpers\Html::encode() を使用して HTML エンコードされます。 入力要素にカスタム |
return | $this |
フィールドオブジェクト自体。 |
---|
public function input($type, $options = [])
{
$options = array_merge($this->inputOptions, $options);
if ($this->form->validationStateOn === ActiveForm::VALIDATION_STATE_ON_INPUT) {
$this->addErrorClassIfNeeded($options);
}
$this->addAriaAttributes($options);
$this->adjustLabelFor($options);
$this->parts['{input}'] = Html::activeInput($type, $this->model, $this->attribute, $options);
return $this;
}
フィールドで AJAX 検証が有効になっているかどうかを確認します。
protected boolean isAjaxValidationEnabled ( ) |
protected function isAjaxValidationEnabled()
{
return $this->enableAjaxValidation || $this->enableAjaxValidation === null && $this->form->enableAjaxValidation;
}
フィールドでクライアント側の検証が有効になっているかどうかを確認します。
protected boolean isClientValidationEnabled ( ) |
protected function isClientValidationEnabled()
{
return $this->enableClientValidation || $this->enableClientValidation === null && $this->form->enableClientValidation;
}
$attribute のラベルタグを生成します。
public $this label ( $label = null, $options = [] ) | ||
$label | string|null|false |
使用するラベル。 |
$options | array|null |
名前と値のペアで表されるタグオプション。$labelOptions とマージされます。オプションは結果のタグの属性としてレンダリングされます。値は yii\helpers\Html::encode() を使用して HTML エンコードされます。値が |
return | $this |
フィールドオブジェクト自体。 |
---|
public function label($label = null, $options = [])
{
if ($label === false) {
$this->parts['{label}'] = '';
return $this;
}
$options = array_merge($this->labelOptions, $options);
if ($label !== null) {
$options['label'] = $label;
}
if ($this->_skipLabelFor) {
$options['for'] = null;
}
$this->parts['{label}'] = Html::activeLabel($this->model, $this->attribute, $options);
return $this;
}
リストボックスをレンダリングします。
リストボックスの選択は、モデル属性の値から取得されます。
public $this listBox ( $items, $options = [] ) | ||
$items | array |
オプションのデータ項目。配列のキーはオプションの値、配列の値は対応するオプションのラベルです。配列はネストすることもできます(つまり、一部の配列の値も配列です)。各サブ配列に対して、ラベルがサブ配列に関連付けられたキーであるオプショングループが生成されます。データモデルのリストがある場合は、yii\helpers\ArrayHelper::map() を使用して上記形式に変換できます。 注意: 値とラベルは、このメソッドによって自動的に HTML エンコードされ、ラベルの空白も HTML エンコードされます。 |
$options | array |
名前と値のペアの形式のタグオプション。 利用可能なオプションのリストについては、yii\helpers\Html::activeListBox() の 入力要素にカスタム |
return | $this |
フィールドオブジェクト自体。 |
---|
public function listBox($items, $options = [])
{
$options = array_merge($this->inputOptions, $options);
if ($this->form->validationStateOn === ActiveForm::VALIDATION_STATE_ON_INPUT) {
$this->addErrorClassIfNeeded($options);
}
$this->addAriaAttributes($options);
$this->adjustLabelFor($options);
$this->parts['{input}'] = Html::activeListBox($this->model, $this->attribute, $items, $options);
return $this;
}
定義元: 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]);
}
}
パスワード入力をレンダリングします。
このメソッドは、$options
で明示的に指定されていない限り、モデル属性の name
および value
タグ属性を自動的に生成します。
public $this passwordInput ( $options = [] ) | ||
$options | array |
名前と値のペアの形式のタグオプション。これらは、結果として得られるタグの属性としてレンダリングされます。値は、yii\helpers\Html::encode() を使用して HTML エンコードされます。 入力要素にカスタム |
return | $this |
フィールドオブジェクト自体。 |
---|
public function passwordInput($options = [])
{
$options = array_merge($this->inputOptions, $options);
if ($this->form->validationStateOn === ActiveForm::VALIDATION_STATE_ON_INPUT) {
$this->addErrorClassIfNeeded($options);
}
$this->addAriaAttributes($options);
$this->adjustLabelFor($options);
$this->parts['{input}'] = Html::activePasswordInput($this->model, $this->attribute, $options);
return $this;
}
ラジオボタンをレンダリングします。
このメソッドは、モデル属性値に従って checked
タグ属性を生成します。
public $this radio ( $options = [], $enclosedByLabel = true ) | ||
$options | array |
名前と値のペアの形式のタグオプション。以下のオプションは特別に処理されます。
残りのオプションは、結果のタグの属性としてレンダリングされます。値は yii\helpers\Html::encode() を使用して HTML エンコードされます。値が 入力要素にカスタム |
$enclosedByLabel | boolean |
ラジオをラベルで囲むかどうか。 |
return | $this |
フィールドオブジェクト自体。 |
---|
public function radio($options = [], $enclosedByLabel = true)
{
if ($this->form->validationStateOn === ActiveForm::VALIDATION_STATE_ON_INPUT) {
$this->addErrorClassIfNeeded($options);
}
$this->addAriaAttributes($options);
$this->adjustLabelFor($options);
if ($enclosedByLabel) {
$this->parts['{input}'] = Html::activeRadio($this->model, $this->attribute, $options);
$this->parts['{label}'] = '';
} else {
if (isset($options['label']) && !isset($this->parts['{label}'])) {
$this->parts['{label}'] = $options['label'];
if (!empty($options['labelOptions'])) {
$this->labelOptions = $options['labelOptions'];
}
}
unset($options['labelOptions']);
$options['label'] = null;
$this->parts['{input}'] = Html::activeRadio($this->model, $this->attribute, $options);
}
return $this;
}
ラジオボタンのリストをレンダリングします。
ラジオボタンリストは、チェックボックスリストと似ていますが、単一選択のみを許可します。ラジオボタンの選択は、モデル属性の値から取得されます。
public $this radioList ( $items, $options = [] ) | ||
$items | array |
ラジオボタンの生成に使用されるデータアイテム。配列の値はラベルであり、配列のキーは対応するラジオの値です。 |
$options | array |
ラジオボタンリストのオプション(name => config)。利用可能なオプションのリストについては、yii\helpers\Html::activeRadioList() の |
return | $this |
フィールドオブジェクト自体。 |
---|
public function radioList($items, $options = [])
{
if ($this->form->validationStateOn === ActiveForm::VALIDATION_STATE_ON_INPUT) {
$this->addErrorClassIfNeeded($options);
}
$this->addRoleAttributes($options, 'radiogroup');
$this->addAriaAttributes($options);
$this->adjustLabelFor($options);
$this->_skipLabelFor = true;
$this->parts['{input}'] = Html::activeRadioList($this->model, $this->attribute, $items, $options);
return $this;
}
フィールド全体をレンダリングします。
このメソッドは、ラベル、エラータグ、入力タグ、ヒントタグ(もしあれば)を生成し、$templateに従ってHTMLに組み立てます。
public string render ( $content = null ) | ||
$content | string|callable|null |
フィールドコンテナ内のコンテンツ。
|
return | string |
レンダリング結果。 |
---|
public function render($content = null)
{
if ($content === null) {
if (!isset($this->parts['{input}'])) {
$this->textInput();
}
if (!isset($this->parts['{label}'])) {
$this->label();
}
if (!isset($this->parts['{error}'])) {
$this->error();
}
if (!isset($this->parts['{hint}'])) {
$this->hint(null);
}
$content = strtr($this->template, $this->parts);
} elseif (!is_string($content)) {
$content = call_user_func($content, $this);
}
return $this->begin() . "\n" . $content . "\n" . $this->end();
}
テキスト入力をレンダリングします。
このメソッドは、$options
で明示的に指定されていない限り、モデル属性の name
および value
タグ属性を自動的に生成します。
public $this textInput ( $options = [] ) | ||
$options | array |
名前と値のペアの形式のタグオプション。これらは、結果として得られるタグの属性としてレンダリングされます。値は、yii\helpers\Html::encode() を使用して HTML エンコードされます。 次の特別なオプションが認識されます。
入力要素にカスタム |
return | $this |
フィールドオブジェクト自体。 |
---|
public function textInput($options = [])
{
$options = array_merge($this->inputOptions, $options);
if ($this->form->validationStateOn === ActiveForm::VALIDATION_STATE_ON_INPUT) {
$this->addErrorClassIfNeeded($options);
}
$this->addAriaAttributes($options);
$this->adjustLabelFor($options);
$this->parts['{input}'] = Html::activeTextInput($this->model, $this->attribute, $options);
return $this;
}
テキストエリアをレンダリングします。
モデル属性の値は、textareaのコンテンツとして使用されます。
public $this textarea ( $options = [] ) | ||
$options | array |
名前と値のペアの形式のタグオプション。これらは、結果として得られるタグの属性としてレンダリングされます。値は、yii\helpers\Html::encode() を使用して HTML エンコードされます。 textarea要素にカスタム |
return | $this |
フィールドオブジェクト自体。 |
---|
public function textarea($options = [])
{
$options = array_merge($this->inputOptions, $options);
if ($this->form->validationStateOn === ActiveForm::VALIDATION_STATE_ON_INPUT) {
$this->addErrorClassIfNeeded($options);
}
$this->addAriaAttributes($options);
$this->adjustLabelFor($options);
$this->parts['{input}'] = Html::activeTextarea($this->model, $this->attribute, $options);
return $this;
}
定義元: 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);
}
フィールドの入力としてウィジェットをレンダリングします。
ウィジェットには、model
プロパティと attribute
プロパティの両方が必要であることに注意してください。それらは、このフィールドの $model および $attribute でそれぞれ初期化されます。
model
および attribute
プロパティを持たないウィジェットを使用する場合は、代わりに render() を使用してください。
yii\base\Widget から拡張されたウィジェットはアクティブフィールドで動作しますが、ベースクラスとして yii\widgets\InputWidget を使用することをお勧めします。
たとえば、日付入力を取得するために yii\widgets\MaskedInput ウィジェットを使用するには、$form
が yii\widgets\ActiveForm インスタンスであると仮定して、次のコードを使用できます。
$form->field($model, 'date')->widget(\yii\widgets\MaskedInput::class, [
'mask' => '99/99/9999',
]);
入力要素にカスタム id
を設定した場合、それに応じて $selectors を調整する必要があるかもしれません。
public $this widget ( $class, $config = [] ) | ||
$class | string |
ウィジェットのクラス名。 |
$config | array |
ウィジェットの初期化に使用される名前と値のペア。 |
return | $this |
フィールドオブジェクト自体。 |
---|---|---|
throws | 例外 |
public function widget($class, $config = [])
{
/* @var $class \yii\base\Widget */
$config['model'] = $this->model;
$config['attribute'] = $this->attribute;
$config['view'] = $this->form->getView();
if (is_subclass_of($class, 'yii\widgets\InputWidget')) {
foreach ($this->inputOptions as $key => $value) {
if (!isset($config['options'][$key])) {
$config['options'][$key] = $value;
}
}
$config['field'] = $this;
if (!isset($config['options'])) {
$config['options'] = [];
}
if ($this->form->validationStateOn === ActiveForm::VALIDATION_STATE_ON_INPUT) {
$this->addErrorClassIfNeeded($config['options']);
}
$this->addAriaAttributes($config['options']);
$this->adjustLabelFor($config['options']);
}
$this->parts['{input}'] = $class::widget($config);
return $this;
}
サインアップ または ログイン してコメントしてください。