在 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');
}
希望这有帮助。
关于Joomla 字段 : change dynamically the readonly property,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15865597/