2 追蹤者

類別 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 boolean 為輸入框添加 aria HTML 屬性 aria-requiredaria-invalid yii\widgets\ActiveField
$attribute string 此欄位關聯的模型屬性。 yii\widgets\ActiveField
$behaviors yii\base\Behavior[] 附加到此組件的行為列表。 yii\base\Component
$enableAjaxValidation boolean|null 是否啟用基於 AJAX 的資料驗證。 yii\widgets\ActiveField
$enableClientValidation boolean|null 是否啟用客戶端資料驗證。 yii\widgets\ActiveField
$errorOptions array 錯誤標籤的預設選項。 yii\widgets\ActiveField
$form yii\widgets\ActiveForm 此欄位關聯的表單。 yii\widgets\ActiveField
$hintOptions array 提示標籤的預設選項。 yii\widgets\ActiveField
$inputOptions array 輸入標籤的預設選項。 yii\widgets\ActiveField
$labelOptions array 標籤的預設選項。 yii\widgets\ActiveField
$model yii\base\Model 此欄位關聯的資料模型。 yii\widgets\ActiveField
$options array 欄位容器標籤的 HTML 屬性(名稱-值 對)。 yii\widgets\ActiveField
$parts array 欄位的不同部分(例如:輸入框、標籤)。 yii\widgets\ActiveField
$selectors array 用於選取容器、輸入框和錯誤標籤的 jQuery 選擇器。 yii\widgets\ActiveField
$template string 用於排列標籤、輸入欄位、錯誤訊息和提示文字的模板。 yii\widgets\ActiveField
$validateOnBlur boolean|null 是否在輸入欄位失去焦點時執行驗證。 yii\widgets\ActiveField
$validateOnChange boolean|null 是否在輸入欄位的值變更時執行驗證。 yii\widgets\ActiveField
$validateOnType boolean|null 是否在使用者於輸入欄位輸入時執行驗證。 yii\widgets\ActiveField
$validationDelay integer|null 當使用者在欄位中輸入且 $validateOnType 設為 true 時,驗證應延遲的毫秒數。 yii\widgets\ActiveField

公共方法

隱藏繼承的方法

方法 描述 定義於
__call() 呼叫指定的非類別方法。 yii\base\Component
__clone() 在通過複製現有物件建立物件後,會呼叫此方法。 yii\base\Component
__construct() 建構子。 yii\base\BaseObject
__get() 返回組件屬性的值。 yii\base\Component
__isset() 檢查屬性是否已設定,即已定義且非空值。 yii\base\Component
__set() 設定組件屬性的值。 yii\base\Component
__toString() PHP 魔術方法,返回此物件的字串表示形式。 yii\widgets\ActiveField
__unset() 將組件屬性設定為空值。 yii\base\Component
attachBehavior() 將行為附加到此組件。 yii\base\Component
attachBehaviors() 將行為列表附加到組件。 yii\base\Component
begin() 渲染欄位容器的開始標籤。 yii\widgets\ActiveField
behaviors() 返回此組件應表現為的行為列表。 yii\base\Component
canGetProperty() 返回一個值,指示屬性是否可讀取。 yii\base\Component
canSetProperty() 返回一個值,指示屬性是否可設定。 yii\base\Component
checkbox() 渲染一個核取方塊。 yii\widgets\ActiveField
checkboxList() 渲染一個核取方塊列表。 yii\widgets\ActiveField
className() 返回此類別的完整限定名稱。 yii\base\BaseObject
detachBehavior() 從組件分離行為。 yii\base\Component
detachBehaviors() 從組件分離所有行為。 yii\base\Component
end() 渲染欄位容器的結束標籤。 yii\widgets\ActiveField
ensureBehaviors() 確保在 behaviors() 中宣告的行為已附加到此組件。 yii\base\Component
error() 產生一個標籤,其中包含 $attribute 的第一個驗證錯誤。 yii\widgets\ActiveField
fileInput() 渲染一個檔案輸入框。 yii\widgets\ActiveField
getBehavior() 返回指定的行為物件。 yii\base\Component
getBehaviors() 返回附加到此組件的所有行為。 yii\base\Component
hasEventHandlers() 返回一個值,指示是否有名稱事件附加任何處理程序。 yii\base\Component
hasMethod() 返回一個值,指示是否已定義方法。 yii\base\Component
hasProperty() 返回一個值,指示是否為此組件定義了屬性。 yii\base\Component
hiddenInput() 渲染一個隱藏的輸入框。 yii\widgets\ActiveField
hint() 渲染提示標籤。 yii\widgets\ActiveField
init() 初始化物件。 yii\base\BaseObject
input() 渲染一個輸入標籤。 yii\widgets\ActiveField
label() $attribute 產生標籤標記。 yii\widgets\ActiveField
listBox() 渲染一個列表框。 yii\widgets\ActiveField
off() 從此元件移除現有的事件處理器。 yii\base\Component
on() 將事件處理器附加到事件。 yii\base\Component
passwordInput() 渲染一個密碼輸入框。 yii\widgets\ActiveField
radio() 渲染一個單選按鈕。 yii\widgets\ActiveField
radioList() 渲染單選按鈕列表。 yii\widgets\ActiveField
render() 渲染整個欄位。 yii\widgets\ActiveField
textInput() 渲染一個文字輸入框。 yii\widgets\ActiveField
textarea() 渲染一個文字區域。 yii\widgets\ActiveField
trigger() 觸發一個事件。 yii\base\Component
widget() 渲染一個小工具作為欄位的輸入。 yii\widgets\ActiveField

受保護的方法

隱藏繼承的方法

方法 描述 定義於
addAriaAttributes() 將 aria 屬性新增到輸入選項。 yii\widgets\ActiveField
addErrorClassIfNeeded() 在需要時將驗證類別新增到輸入選項。 yii\widgets\ActiveField
addRoleAttributes() 將 role 屬性新增到輸入選項 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

屬性詳情

隱藏繼承的屬性

$addAriaAttributes 公開屬性 (自版本 2.0.11 起可用)

為輸入框添加 aria HTML 屬性 aria-requiredaria-invalid

$attribute 公開屬性

此欄位關聯的模型屬性。

public string $attribute null
$enableAjaxValidation 公開屬性

是否啟用基於 AJAX 的資料驗證。如果未設定,則會採用 yii\widgets\ActiveForm::$enableAjaxValidation 的值。

$enableClientValidation 公開屬性

是否啟用客戶端資料驗證。如果未設定,則會採用 yii\widgets\ActiveForm::$enableClientValidation 的值。

$errorOptions 公開屬性

錯誤標記的預設選項。當渲染錯誤標記時,傳遞給 error() 的參數將與此屬性合併。以下特殊選項被識別

  • tag:容器元素的標記名稱。預設為 div。將其設定為 false 將不會渲染容器標記。另請參閱 yii\helpers\Html::tag()
  • encode:是否編碼錯誤輸出。預設為 true

如果您為錯誤元素設定了自訂 id,您可能需要相應地調整 $selectors

另請參閱 yii\helpers\Html::renderTagAttributes() 以了解有關屬性如何被渲染的詳細資訊。

public array $errorOptions = [
    
'class' => 'help-block',
]
$form 公開屬性

此欄位關聯的表單。

$hintOptions 公開屬性

提示標記的預設選項。當渲染提示標記時,傳遞給 hint() 的參數將與此屬性合併。以下特殊選項被識別

  • tag:容器元素的標記名稱。預設為 div。將其設定為 false 將不會渲染容器標記。另請參閱 yii\helpers\Html::tag()

另請參閱 yii\helpers\Html::renderTagAttributes() 以了解有關屬性如何被渲染的詳細資訊。

public array $hintOptions = [
    
'class' => 'hint-block',
]
$inputOptions 公開屬性

輸入標記的預設選項。當渲染輸入標記時,傳遞給個別輸入方法 (例如 textInput()) 的參數將與此屬性合併。

如果您為輸入元素設定了自訂 id,您可能需要相應地調整 $selectors

另請參閱 yii\helpers\Html::renderTagAttributes() 以了解有關屬性如何被渲染的詳細資訊。

public array $inputOptions = [
    
'class' => 'form-control',
]
$labelOptions 公開屬性

標籤標記的預設選項。當渲染標籤標記時,傳遞給 label() 的參數將與此屬性合併。

另請參閱 yii\helpers\Html::renderTagAttributes() 以了解有關屬性如何被渲染的詳細資訊。

public array $labelOptions = [
    
'class' => 'control-label',
]
$model 公開屬性

此欄位關聯的資料模型。

public yii\base\Model $model null
$options 公開屬性

欄位容器標記的 HTML 屬性 (名稱-值 組)。這些值將使用 yii\helpers\Html::encode() 進行 HTML 編碼。如果值為 null,則不會渲染對應的屬性。以下特殊選項被識別

  • tag:容器元素的標記名稱。預設為 div。將其設定為 false 將不會渲染容器標記。另請參閱 yii\helpers\Html::tag()

如果您為容器元素設定了自訂 id,您可能需要相應地調整 $selectors

另請參閱 yii\helpers\Html::renderTagAttributes() 以了解有關屬性如何被渲染的詳細資訊。

public array $options = [
    
'class' => 'form-group',
]
$parts 公開屬性

欄位的不同部分 (例如,輸入、標籤)。這將與 $template 一起使用,以產生最終的欄位 HTML 程式碼。鍵是 $template 中的 token 名稱,而值是相應的 HTML 程式碼。有效的 token 包括 {input}{label}{error}。請注意,您通常不需要直接存取此屬性,因為它由此類別的各種方法維護。

public array $parts = []
$selectors 公開屬性

用於選擇容器、輸入和錯誤標記的 jQuery 選擇器。陣列鍵應為 containerinput 和/或 error,陣列值為對應的選擇器。例如,['input' => '#my-input']

容器選擇器在表單的上下文中使用,而輸入和錯誤選擇器在容器的上下文中使用。

您通常不需要設定此屬性,因為預設選擇器在大多數情況下都應該運作良好。

public array $selectors = []
$template 公開屬性

用於排列標籤、輸入欄位、錯誤訊息和提示文字的範本。當呼叫 render() 時,將替換以下 token:{label}{input}{error}{hint}

public string $template "{label}\n{input}\n{hint}\n{error}"
$validateOnBlur 公開屬性

是否在輸入欄位失去焦點時執行驗證。如果未設定,則會採用 yii\widgets\ActiveForm::$validateOnBlur 的值。

$validateOnChange 公開屬性

是否在輸入欄位的值變更時執行驗證。如果未設定,則會採用 yii\widgets\ActiveForm::$validateOnChange 的值。

$validateOnType 公開屬性

是否在使用者於輸入欄位中輸入時執行驗證。如果未設定,則會採用 yii\widgets\ActiveForm::$validateOnType 的值。

另請參閱 $validationDelay

$validationDelay 公開屬性

當使用者在欄位中輸入且 $validateOnType 設定為 true 時,驗證應延遲的毫秒數。如果未設定,則會採用 yii\widgets\ActiveForm::$validationDelay 的值。

方法詳情

隱藏繼承的方法

__call() 公開方法

定義於: yii\base\Component::__call()

呼叫指定的非類別方法。

此方法將檢查是否有任何附加的行為具有指定的名稱方法,如果有則執行它。

不要直接呼叫此方法,因為它是一個 PHP magic method,當未知方法被調用時,它將被隱式呼叫。

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()");
}

            
__clone() 公開方法

定義於: yii\base\Component::__clone()

在通過複製現有物件建立物件後,會呼叫此方法。

它會移除所有行為,因為它們附加到舊的物件。

public void __clone ( )

                public function __clone()
{
    $this->_events = [];
    $this->_eventWildcards = [];
    $this->_behaviors = null;
}

            
__construct() 公開方法

定義於: yii\base\BaseObject::__construct()

建構子。

預設實作執行兩件事

  • 使用給定的組態 $config 初始化物件。
  • 呼叫 init()

如果此方法在子類別中被覆寫,建議

  • 建構子的最後一個參數是組態陣列,如這裡的 $config
  • 在建構子的結尾呼叫父類別實作。
public void __construct ( $config = [] )
$config array

將用於初始化物件屬性的 名稱-值 組

                public function __construct($config = [])
{
    if (!empty($config)) {
        Yii::configure($this, $config);
    }
    $this->init();
}

            
__get() 公開方法

定義於: yii\base\Component::__get()

返回組件屬性的值。

此方法將依以下順序檢查並相應地執行動作

  • getter 定義的屬性:傳回 getter 結果
  • 行為的屬性:傳回行為屬性值

不要直接呼叫此方法,因為它是一個 PHP magic method,當執行 $value = $component->property; 時,它將被隱式呼叫。

另請參閱 __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);
}

            
__isset() 公開方法

定義於: yii\base\Component::__isset()

檢查屬性是否已設定,即已定義且非空值。

此方法將依以下順序檢查並相應地執行動作

  • setter 定義的屬性:傳回屬性是否已設定
  • 行為的屬性:傳回屬性是否已設定
  • 為不存在的屬性傳回 false

不要直接呼叫此方法,因為它是一個 PHP magic method,當執行 isset($component->property) 時,它將被隱式呼叫。

另請參閱 https://php.dev.org.tw/manual/en/function.isset.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;
}

            
__set() 公開方法

定義於: yii\base\Component::__set()

設定組件屬性的值。

此方法將依以下順序檢查並相應地執行動作

  • setter 定義的屬性:設定屬性值
  • 格式為 "on xyz" 的事件:將處理常式附加到事件 "xyz"
  • 格式為 "as xyz" 的行為:附加名為 "xyz" 的行為
  • 行為的屬性:設定行為屬性值

不要直接呼叫此方法,因為它是一個 PHP magic method,當執行 $component->property = $value; 時,它將被隱式呼叫。

另請參閱 __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);
}

            
__toString() 公開方法

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 '';
    }
}

            
__unset() 公開方法

定義於: yii\base\Component::__unset()

將組件屬性設定為空值。

此方法將依以下順序檢查並相應地執行動作

  • setter 定義的屬性:將屬性值設定為 null
  • 行為的屬性:將屬性值設定為 null

不要直接呼叫此方法,因為它是一個 PHP magic method,當執行 unset($component->property) 時,它將被隱式呼叫。

另請參閱 https://php.dev.org.tw/manual/en/function.unset.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);
}

            
addAriaAttributes() 受保護的方法 (自版本 2.0.11 起可用)

將 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';
        }
    }
}

            
addErrorClassIfNeeded() 受保護的方法 (自版本 2.0.14 起可用)

在需要時將驗證類別新增到輸入選項。

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);
    }
}

            
addRoleAttributes() 受保護的方法 (自版本 2.0.16 起可用)

將 role 屬性新增到輸入選項

protected void addRoleAttributes ( &$options, $role )
$options

陣列輸入選項

$role string

                protected function addRoleAttributes(&$options, $role)
{
    if (!isset($options['role'])) {
        $options['role'] = $role;
    }
}

            
adjustLabelFor() protected method

根據輸入選項調整標籤的 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'];
    }
}

            
attachBehavior() public method

定義於: 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);
}

            
attachBehaviors() public method

定義於: 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);
    }
}

            
begin() public method

渲染欄位容器的開始標籤。

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);
}

            
behaviors() public method

定義於: 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 [];
}

            
canGetProperty() public method

定義於: 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;
}

            
canSetProperty() public method

定義於: 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;
}

            
checkbox() public method

渲染一個核取方塊。

此方法將根據模型屬性值產生 checked 標籤屬性。

public $this checkbox ( $options = [], $enclosedByLabel true )
$options array

以名稱-值對形式表示的標籤選項。以下選項會被特別處理

  • uncheck:字串,與核取方塊的未核取狀態相關聯的值。如果未設定,它將採用預設值 0。此方法將呈現一個隱藏的輸入欄位,以便在未核取核取方塊並提交時,此屬性的值仍將透過隱藏的輸入欄位提交到伺服器。如果您不想要任何隱藏的輸入欄位,則應明確地將此選項設定為 null
  • label:字串,顯示在核取方塊旁邊的標籤。它將 **不會** 進行 HTML 編碼。因此,您可以傳入 HTML 程式碼,例如圖片標籤。如果這是來自終端使用者,您應該 encode 它以防止 XSS 攻擊。當指定此選項時,核取方塊將包含在 label 標籤中。如果您不想要任何標籤,則應明確地將此選項設定為 null
  • labelOptions:陣列,label 標籤的 HTML 屬性。這僅在指定 label 選項時使用。

其餘選項將會呈現為結果標籤的屬性。值將使用 yii\helpers\Html::encode() 進行 HTML 編碼。如果值為 null,則不會呈現對應的屬性。

如果您為輸入元素設定了自訂 id,您可能需要相應地調整 $selectors

$enclosedByLabel boolean

是否將核取方塊包含在標籤內。如果為 true,則此方法仍將使用 $template 來佈局核取方塊和錯誤訊息,但核取方塊包含在 label 標籤中除外。

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;
}

            
checkboxList() public method

渲染一個核取方塊列表。

核取方塊列表允許多重選取,類似於 listBox()。因此,對應的提交值是一個陣列。核取方塊列表的選取是從模型屬性的值中取得的。

public $this checkboxList ( $items, $options = [] )
$items array

用於產生核取方塊的資料項目。陣列值是標籤,而陣列鍵是對應的核取方塊值。

$options array

核取方塊列表的選項(name => config)。有關可用選項的列表,請參閱 yii\helpers\Html::activeCheckboxList()$options 參數。

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;
}

            
className() public static method
自 2.0.14 版本起已棄用。在 PHP >=5.5 上,請改用 ::class

定義於: yii\base\BaseObject::className()

返回此類別的完整限定名稱。

public static string className ( )
return string

此類別的完整限定名稱。

                public static function className()
{
    return get_called_class();
}

            
detachBehavior() public method

定義於: 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;
}

            
detachBehaviors() public method

定義於: yii\base\Component::detachBehaviors()

從組件分離所有行為。

public void detachBehaviors ( )

                public function detachBehaviors()
{
    $this->ensureBehaviors();
    foreach ($this->_behaviors as $name => $behavior) {
        $this->detachBehavior($name);
    }
}

            
end() public method

渲染欄位容器的結束標籤。

public string end ( )
return string

呈現結果。

                public function end()
{
    return Html::endTag(ArrayHelper::keyExists('tag', $this->options) ? $this->options['tag'] : 'div');
}

            
ensureBehaviors() public method

定義於: 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);
        }
    }
}

            
error() public method

產生一個標籤,其中包含 $attribute 的第一個驗證錯誤。

請注意,即使沒有驗證錯誤,此方法仍將傳回一個空的錯誤標籤。

另請參閱 $errorOptions

public $this error ( $options = [] )
$options array|false

以名稱-值對形式表示的標籤選項。它將與 $errorOptions 合併。選項將會呈現為結果標籤的屬性。值將使用 yii\helpers\Html::encode() 進行 HTML 編碼。如果此參數為 false,則不會呈現錯誤標籤。

以下選項會被特別處理

如果您為錯誤元素設定了自訂 id,您可能需要相應地調整 $selectors

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;
}

            
fileInput() public method

渲染一個檔案輸入框。

此方法將自動為模型屬性產生 namevalue 標籤屬性,除非它們在 $options 中明確指定。

public $this fileInput ( $options = [] )
$options array

以名稱-值對形式表示的標籤選項。這些將會呈現為結果標籤的屬性。值將使用 yii\helpers\Html::encode() 進行 HTML 編碼。

如果您為輸入元素設定了自訂 id,您可能需要相應地調整 $selectors

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;
}

            
getBehavior() public method

定義於: 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;
}

            
getBehaviors() public method

定義於: yii\base\Component::getBehaviors()

返回附加到此組件的所有行為。

public yii\base\Behavior[] getBehaviors ( )
return yii\base\Behavior[]

附加到此元件的行為列表

                public function getBehaviors()
{
    $this->ensureBehaviors();
    return $this->_behaviors;
}

            
getClientOptions() protected method

傳回欄位的 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,
    ]);
}

            
getInputId() protected method (available since version 2.0.7)

傳回此表單欄位輸入元素的 HTML id

protected string getInputId ( )
return string

輸入 ID。

                protected function getInputId()
{
    return $this->_inputId ?: Html::getInputId($this->model, $this->attribute);
}

            
hasEventHandlers() public method

定義於: 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);
}

            
hasMethod() public method

定義於: 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;
}

            
hasProperty() public method

定義於: yii\base\Component::hasProperty()

返回一個值,指示是否為此組件定義了屬性。

如果符合以下條件,則定義了屬性

  • 類別具有與指定名稱相關聯的 getter 或 setter 方法(在這種情況下,屬性名稱不區分大小寫);
  • 類別具有具有指定名稱的成員變數(當 $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);
}

            
hiddenInput() public method

渲染一個隱藏的輸入框。

請注意,提供此方法是為了完整性。在大多數情況下,因為您不需要驗證隱藏的輸入欄位,所以您應該不需要使用此方法。相反地,您應該使用 yii\helpers\Html::activeHiddenInput()

此方法將自動為模型屬性產生 namevalue 標籤屬性,除非它們在 $options 中明確指定。

public $this hiddenInput ( $options = [] )
$options array

以名稱-值對形式表示的標籤選項。這些將會呈現為結果標籤的屬性。值將使用 yii\helpers\Html::encode() 進行 HTML 編碼。

如果您為輸入元素設定了自訂 id,您可能需要相應地調整 $selectors

return $this

欄位物件本身。

                public function hiddenInput($options = [])
{
    $options = array_merge($this->inputOptions, $options);
    $this->adjustLabelFor($options);
    $this->parts['{input}'] = Html::activeHiddenInput($this->model, $this->attribute, $options);
    return $this;
}

            
hint() public method

渲染提示標籤。

public $this hint ( $content, $options = [] )
$content string|boolean|null

提示內容。如果為 null,提示將會透過 yii\base\Model::getAttributeHint() 產生。如果為 false,則產生的欄位將不會包含提示部分。請注意,這將 **不會** 被 encoded

$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;
}

            
init() public method

定義於: yii\base\BaseObject::init()

初始化物件。

在使用給定的設定初始化物件後,此方法會在建構函式的結尾被調用。

public void init ( )

                public function init()
{
}

            
input() public method

渲染一個輸入標籤。

public $this input ( $type, $options = [] )
$type string

輸入類型 (例如:textpassword)

$options array

以名稱-值對形式表示的標籤選項。這些將會呈現為結果標籤的屬性。值將使用 yii\helpers\Html::encode() 進行 HTML 編碼。

如果您為輸入元素設定了自訂 id,您可能需要相應地調整 $selectors

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;
}

            
isAjaxValidationEnabled() protected method (available since version 2.0.11)

檢查是否為欄位啟用 Ajax 驗證。

protected boolean isAjaxValidationEnabled ( )

                protected function isAjaxValidationEnabled()
{
    return $this->enableAjaxValidation || $this->enableAjaxValidation === null && $this->form->enableAjaxValidation;
}

            
isClientValidationEnabled() protected method (available since version 2.0.11)

檢查是否為欄位啟用客戶端驗證。

protected boolean isClientValidationEnabled ( )

                protected function isClientValidationEnabled()
{
    return $this->enableClientValidation || $this->enableClientValidation === null && $this->form->enableClientValidation;
}

            
label() public method

$attribute 產生標籤標記。

public $this label ( $label null, $options = [] )
$label string|null|false

要使用的標籤。如果為 null,標籤將透過 yii\base\Model::getAttributeLabel() 產生。如果為 false,產生的欄位將不包含標籤部分。請注意,這將不會被 編碼

$options array|null

以名稱-值對形式表示的標籤選項。它將與 $labelOptions 合併。選項將被渲染為結果標籤的屬性。這些值將使用 yii\helpers\Html::encode() 進行 HTML 編碼。如果值為 null,則不會渲染相應的屬性。

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;
}

            
listBox() public method

渲染一個列表框。

列表框的選取是從模型屬性的值中取得的。

public $this listBox ( $items, $options = [] )
$items array

選項資料項目。陣列鍵是選項值,而陣列值是對應的選項標籤。陣列也可以是巢狀的(即某些陣列值也是陣列)。對於每個子陣列,將會產生一個選項群組,其標籤是與子陣列相關聯的鍵。如果您有資料模型列表,則可以使用 yii\helpers\ArrayHelper::map() 將它們轉換為上述格式。

請注意,此方法會自動對值和標籤進行 HTML 編碼,並且標籤中的空白也會進行 HTML 編碼。

$options array

以名稱-值對形式表示的標籤選項。

有關可用選項的列表,請參閱 yii\helpers\Html::activeListBox()$options 參數。

如果您為輸入元素設定了自訂 id,您可能需要相應地調整 $selectors

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;
}

            
off() public method

定義於: 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;
}

            
on() public method

定義於: yii\base\Component::on()

將事件處理器附加到事件。

事件處理程序必須是有效的 PHP 回呼。以下是一些範例

function ($event) { ... }         // anonymous function
[$object, 'handleClick']          // $object->handleClick()
['Page', 'handleClick']           // Page::handleClick()
'handleClick'                     // global function handleClick()

事件處理程序必須使用以下簽名定義:

function ($event)

其中 $event 是一個 yii\base\Event 物件,其中包含與事件關聯的參數。

自 2.0.14 版本起,您可以將事件名稱指定為萬用字元模式

$component->on('event.group.*', function ($event) {
    Yii::trace($event->name . ' is triggered.');
});

另請參閱 off()

public void on ( $name, $handler, $data null, $append true )
$name string

事件名稱

$handler callable

事件處理程序

$data mixed

當事件被觸發時,要傳遞給事件處理程序的資料。當事件處理程序被調用時,可以透過 yii\base\Event::$data 存取此資料。

$append boolean

是否將新的事件處理程序附加到現有處理程序列表的末尾。如果為 false,則新的處理程序將插入到現有處理程序列表的開頭。

                public function on($name, $handler, $data = null, $append = true)
{
    $this->ensureBehaviors();
    if (strpos($name, '*') !== false) {
        if ($append || empty($this->_eventWildcards[$name])) {
            $this->_eventWildcards[$name][] = [$handler, $data];
        } else {
            array_unshift($this->_eventWildcards[$name], [$handler, $data]);
        }
        return;
    }
    if ($append || empty($this->_events[$name])) {
        $this->_events[$name][] = [$handler, $data];
    } else {
        array_unshift($this->_events[$name], [$handler, $data]);
    }
}

            
passwordInput() public method

渲染一個密碼輸入框。

此方法將自動為模型屬性產生 namevalue 標籤屬性,除非它們在 $options 中明確指定。

public $this passwordInput ( $options = [] )
$options array

以名稱-值對形式表示的標籤選項。這些將會呈現為結果標籤的屬性。值將使用 yii\helpers\Html::encode() 進行 HTML 編碼。

如果您為輸入元素設定了自訂 id,您可能需要相應地調整 $selectors

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;
}

            
radio() public method

渲染一個單選按鈕。

此方法將根據模型屬性值產生 checked 標籤屬性。

public $this radio ( $options = [], $enclosedByLabel true )
$options array

以名稱-值對形式表示的標籤選項。以下選項會被特別處理

  • uncheck:字串,與核取方塊的未核取狀態相關聯的值。如果未設定,它將採用預設值 0。此方法將呈現一個隱藏的輸入欄位,以便在未核取核取方塊並提交時,此屬性的值仍將透過隱藏的輸入欄位提交到伺服器。如果您不想要任何隱藏的輸入欄位,則應明確地將此選項設定為 null
  • label: 字串,顯示在單選按鈕旁邊的標籤。它將不會進行 HTML 編碼。因此,您可以傳入 HTML 程式碼,例如影像標籤。如果這是來自終端使用者,您應該 編碼 以防止 XSS 攻擊。當指定此選項時,單選按鈕將被標籤標記包圍。如果您不想要任何標籤,您應該明確地將此選項設定為 null
  • labelOptions:陣列,label 標籤的 HTML 屬性。這僅在指定 label 選項時使用。

其餘選項將會呈現為結果標籤的屬性。值將使用 yii\helpers\Html::encode() 進行 HTML 編碼。如果值為 null,則不會呈現對應的屬性。

如果您為輸入元素設定了自訂 id,您可能需要相應地調整 $selectors

$enclosedByLabel boolean

是否將單選按鈕包在標籤內。如果為 true,則此方法仍將使用 $template 來佈局單選按鈕和錯誤訊息,除了單選按鈕被標籤標記包圍。

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;
}

            
radioList() public method

渲染單選按鈕列表。

單選按鈕列表就像核取方塊列表,只是它只允許單選。單選按鈕的選取是從模型屬性的值中取得的。

public $this radioList ( $items, $options = [] )
$items array

用於產生單選按鈕的資料項目。陣列值是標籤,而陣列鍵是相應的單選值。

$options array

單選按鈕列表的選項 (name => config)。有關可用選項的列表,請參閱 yii\helpers\Html::activeRadioList()$options 參數。

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;
}

            
render() public method

渲染整個欄位。

此方法將產生標籤、錯誤標籤、輸入標籤和提示標籤 (如果有的話),並根據 $template 將它們組裝成 HTML。

public string render ( $content null )
$content string|callable|null

欄位容器內的內容。如果為 null (未設定),則將呼叫預設方法以產生標籤、錯誤標籤和輸入標籤,並將它們用作內容。如果為 callable,則將呼叫它以產生內容。callable 的簽名應為

function ($field) {
    return $html;
}
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();
}

            
textInput() public method

渲染一個文字輸入框。

此方法將自動為模型屬性產生 namevalue 標籤屬性,除非它們在 $options 中明確指定。

public $this textInput ( $options = [] )
$options array

以名稱-值對形式表示的標籤選項。這些將會呈現為結果標籤的屬性。值將使用 yii\helpers\Html::encode() 進行 HTML 編碼。

以下特殊選項被識別

  • maxlength: int|bool,當 maxlength 設定為 true 且模型屬性通過字串驗證器驗證時,`maxlength` 選項將採用 yii\validators\StringValidator::$max 的值。這自 2.0.3 版本起可用。

請注意,如果您為輸入元素設定了自訂 id,您可能需要相應地調整 $selectors 的值。

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 method

渲染一個文字區域。

模型屬性值將用作 textarea 中的內容。

public $this textarea ( $options = [] )
$options array

以名稱-值對形式表示的標籤選項。這些將會呈現為結果標籤的屬性。值將使用 yii\helpers\Html::encode() 進行 HTML 編碼。

如果您為 textarea 元素設定了自訂 id,您可能需要相應地調整 $selectors

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;
}

            
trigger() public method

定義於: 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);
}

            
widget() public method

渲染一個小工具作為欄位的輸入。

請注意,widget 必須同時具有 modelattribute 屬性。它們將分別使用此欄位的 $model$attribute 進行初始化。

如果您想使用不具有 modelattribute 屬性的 widget,請改用 render()

雖然從 yii\base\Widget 擴展的 widget 可以與 active field 一起使用,但建議使用 yii\widgets\InputWidget 作為基底類別。

例如,要使用 yii\widgets\MaskedInput widget 來取得一些日期輸入,您可以使用以下程式碼,假設 $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

Widget 類別名稱。

$config array

將用於初始化 widget 的名稱-值對。

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;
}