Php 乔姆拉';s JForms在编辑时设置为只读

Php 乔姆拉';s JForms在编辑时设置为只读,php,joomla,joomla3.0,Php,Joomla,Joomla3.0,我正在创建Joomla 3.0组件,我有一个问题 我想使某些字段在编辑模式下为只读。它们应该在创建时设置,然后不可编辑 在我看来,我知道三种可能的方法 在后台将字段设置为只读 如果在编辑模式下字段是只读定义的,请加载其他表单 使用javascript将字段设置为只读 我更喜欢方法2(或者更简单的方法),但我不知道如何实现。如果我们进入编辑模式,我怎么知道在getForm()函数中?有什么建议吗 编辑: 目前,我正在使用我不喜欢的方法3: <?PHP if ($this->item-&

我正在创建Joomla 3.0组件,我有一个问题

我想使某些字段在编辑模式下为只读。它们应该在创建时设置,然后不可编辑

在我看来,我知道三种可能的方法

  • 在后台将字段设置为只读
  • 如果在编辑模式下字段是只读定义的,请加载其他表单
  • 使用javascript将字段设置为只读
  • 我更喜欢方法2(或者更简单的方法),但我不知道如何实现。如果我们进入编辑模式,我怎么知道在getForm()函数中?有什么建议吗

    编辑: 目前,我正在使用我不喜欢的方法3:

    <?PHP if ($this->item->id > 0) { ?>
    <script type="text/javascript">
    var text_box = document.getElementById('jform_name');
    text_box.setAttribute('readonly', 'readonly'); 
    </script>
    <?PHP } ?>
    
    
    var text_box=document.getElementById('jform_name');
    text_box.setAttribute('readonly','readonly');
    
    我不建议创建一个“只读”表单,因为在更新表单时,您必须在不同版本之间保持同步,基本上是中断的

    您可以在运行时使用
    setFieldAttribtue()
    设置JForm的各种属性

    例如,在许多核心组件中,您可以找到正在修改的表单:

    com\u admin/models/profile.php

    public function getForm($data = array(), $loadData = true)
    {
        // Get the form.
        $form = $this->loadForm('com_admin.profile', 'profile', array('control' => 'jform', 'load_data' => $loadData));
        if (empty($form))
        {
            return false;
        }
        if (!JComponentHelper::getParams('com_users')->get('change_login_name'))
        {
            $form->setFieldAttribute('username', 'required', 'false');
            $form->setFieldAttribute('username', 'readonly', 'true');
            $form->setFieldAttribute('username', 'description', 'COM_ADMIN_USER_FIELD_NOCHANGE_USERNAME_DESC');
        }
    
        return $form;
    }
    
    在我们的一些组件中,我们不仅设置了
    readonly
    属性,而且还设置了
    ,以便可以适当地设置字段的样式

    $form->setFieldAttribute('name', 'class', 'readonly');
    $form->setFieldAttribute('name', 'readonly', 'true');
    

    您应该能够在JForm表单的xml贴花中添加readonly=“true”(如果您使用它),这很好,我不知道。但最后一个问题仍然存在。如何确定我是否正在编辑或创建产品?我自己找到的,它是$this->getState('profile.id')(profile=loadForm的第二个参数,id=field的值),它返回id,如果它是我正在编辑的设置;)