【发布时间】:2013-04-07 17:41:59
【问题描述】:
在 Joomla 组件形式中:
有没有办法动态更改表单字段属性:“只读”?
例子:
if( _condition_ )
$this->form->getInput('Name')->readonly = true;
【问题讨论】:
在 Joomla 组件形式中:
有没有办法动态更改表单字段属性:“只读”?
例子:
if( _condition_ )
$this->form->getInput('Name')->readonly = true;
【问题讨论】:
根据我在 API 中看到的,您基本上可以更改它。
我是这样看的:
当您调用$this->form->getInput('Name') 时,您在JFormField 对象内部(实际上是在一个与JFormField 交互的类内部,它是抽象的——例如由JFormFieldText 继承),调用方法getInput()。
这个方法getInput() 获取它的参数$element(描述表单字段的 XML 元素的 SimpleXMLElement 对象。)从我可以直接从您定义的 XML 中看到,它只返回一个字符串(实际的 HTML ),所以设置和不存在的属性显然是行不通的。
但是 JForm 有一个很好的方法叫做 setFieldAttribute(),请看下面的签名:
/**
* Method to set an attribute value for a field XML element.
*
* @param string $name The name of the form field for which to set the attribute value.
* @param string $attribute The name of the attribute for which to set a value.
* @param mixed $value The value to set for the attribute.
* @param string $group The optional dot-separated form group path on which to find the field.
*
* @return boolean True on success.
*
* @since 11.1
*/
public function setFieldAttribute($name, $attribute, $value, $group = null)
所以你的代码看起来像:
if( _condition_ )
{
$this->form->setFieldAttribute('Name', 'readonly', 'true', $group = null);
echo $this->form->getInput('name');
}
希望这会有所帮助。
【讨论】: