:
有没有办法动态更改表单字段属性:“readonly”?
示例:
if( _condition_ )
$this->form->getInput('Name')->readonly = true;
答案 0 :(得分:5)
从我在API中看到的,你基本上可以改变它。
我这样看:
当你调用$this->form->getInput('Name')
时,你在JFormField对象内部(实际上是在JFormField之间的一个类中,它是抽象的 - 例如由JFormFieldText继承),调用方法getInput()
。
这个方法getInput()
从我定义的XML中直接看到它的参数$element
(描述表单字段的XML元素的SimpleXMLElement对象),它只返回一个字符串(实际的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');
}
希望这有帮助。