2

在 Joomla 组件形式中:

有没有办法动态更改表单字段属性:“只读”?

例子 :

if( _condition_ )
   $this->form->getInput('Name')->readonly = true;
4

1 回答 1

6

根据我在 API 中看到的,您基本上可以更改它。

我是这样看的:

当您调用时,$this->form->getInput('Name')您在 JFormField 对象内部(实际上在一个与 JFormField 交互的类内部,该类是抽象的 - 例如由 JFormFieldText 继承),调用方法getInput()

这个方法,从我可以直接从您定义的 XML 中看到的getInput()获取它的参数(描述表单字段的 XML 元素的 SimpleXMLElement 对象。)它只返回一个字符串(实际的 HTML),因此显然设置和不存在的属性$element不会工作。

但是 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');
}

希望这可以帮助。

于 2013-04-07T18:26:07.053 回答