類別 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() | 將 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 |
屬性詳情
為輸入框添加 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() 時,將替換以下 token:{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 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()");
}
public void __clone ( ) |
public function __clone()
{
$this->_events = [];
$this->_eventWildcards = [];
$this->_behaviors = null;
}
定義於: 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();
}
定義於: 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);
}
定義於: yii\base\Component::__isset()
檢查屬性是否已設定,即已定義且非空值。
此方法將依以下順序檢查並相應地執行動作
- setter 定義的屬性:傳回屬性是否已設定
- 行為的屬性:傳回屬性是否已設定
- 為不存在的屬性傳回
false
不要直接呼叫此方法,因為它是一個 PHP magic method,當執行 isset($component->property)
時,它將被隱式呼叫。
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()
設定組件屬性的值。
此方法將依以下順序檢查並相應地執行動作
- 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);
}
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()
將組件屬性設定為空值。
此方法將依以下順序檢查並相應地執行動作
- setter 定義的屬性:將屬性值設定為 null
- 行為的屬性:將屬性值設定為 null
不要直接呼叫此方法,因為它是一個 PHP magic method,當執行 unset($component->property)
時,它將被隱式呼叫。
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);
}
}
將 role 屬性新增到輸入選項
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 |
行為設定。這可以是下列其中一項
|
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();
}
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);
}
}
}
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;
}
渲染一個檔案輸入框。
此方法將自動為模型屬性產生 name
和 value
標籤屬性,除非它們在 $options
中明確指定。
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()
返回一個值,指示是否為此組件定義了屬性。
如果符合以下條件,則定義了屬性
- 類別具有與指定名稱相關聯的 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);
}
渲染提示標籤。
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]);
}
}
渲染一個密碼輸入框。
此方法將自動為模型屬性產生 name
和 value
標籤屬性,除非它們在 $options
中明確指定。
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();
}
渲染一個文字輸入框。
此方法將自動為模型屬性產生 name
和 value
標籤屬性,除非它們在 $options
中明確指定。
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;
}
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 必須同時具有 model
和 attribute
屬性。它們將分別使用此欄位的 $model 和 $attribute 進行初始化。
如果您想使用不具有 model
和 attribute
屬性的 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;
}
註冊 或 登入 以發表評論。