【问题标题】:Joomla - setting field attribute in a pluginJoomla - 在插件中设置字段属性
【发布时间】:2017-01-04 12:22:14
【问题描述】:

我创建了一个用户插件,用于在 Joomla 3.6.5 中扩展默认用户配置文件。 我在 profiles/profile.xml 中添加了以下字段 sample_title

<field
    name="sample_title"
    type="list"
    id="sample_title" description="sample description..." label="sample title"  default="">
    <option value="mr">Mr</option>
    <option value="mrs">Mrs</option>                
</field>

在我的插件类中,我有一个 onContentPrepareForm($form, $data) 函数。在这个函数中,我尝试通过执行 $form->setFieldAttribute('sample_title', 'required', 'true', 'profilecustom'); 来更改 sample_title 的必需属性; 如果我在下一行看到所需的属性值,似乎可以,所需的是“true”。此代码 echo $form->getFieldAttribute('sample_title', 'required', 'false', 'profilecustom' ); 打印 true 这对我来说没问题。 但是当我显示注册视图时(我使用来自 com_users 的 registration/default.php 视图),sample_title 字段被声明为可选。在注册视图中,我们有

<?php if (!$field->required && $field->type != 'Spacer') : ?>
    <span class="optional"><?php echo JText::_('COM_USERS_OPTIONAL');?></span>
<?php endif; ?>

当我将必填字段属性设置为 true 时,会为 sample_title 显示这个跨度,所以它不是 OK...

作为附加信息:

  • 我试过 $form->setFieldAttribute('sample_title', 'required', 'true', 'profilecustom');$form->setFieldAttribute('sample_title', '必需的', true, 'profilecustom');.结果是一样的。
  • 我按照以下文档 (https://docs.joomla.org/Creating_a_profile_plugin) 创建了我的插件。

我做错了吗? 我知道我可以将所需的值放在字段声明中

<field
    name="sample_title"
    type="list"
    id="sample_title" description="sample description..." label="sample_title"  default="" required="true">

但这不是我想要的。我想在某些条件下设置这个值,所以我需要以编程方式进行。


我继续调查 :-) 在注册视图中,如果我使用默认方式显示字段

<?php foreach ($fields as $field) : ?> 
....
<?php if (!$field->required && $field->type != 'Spacer') : ?>
<span class="optional"><?php echo JText::_('COM_USERS_OPTIONAL');?></span>
<?php endif; ?>
...
<?php endforeach;?>

$field->required 是假的,但如果我尝试使用 $sample_title_field = $this->form->getField('sample_title', 'profilecustom'); 然后 $sample_title_field->required 为真,所以 $this->form->getFieldset($fieldset->name); 返回的字段之间似乎存在一些不一致 以及那些使用 $this->form->getField(...);

【问题讨论】:

    标签: php forms joomla


    【解决方案1】:

    在示例代码 (https://docs.joomla.org/Creating_a_profile_plugin) 中,它们执行 $form->loadFile('profile', false);。我用 $form->loadFile('profile', true); 替换了这段代码,现在字段属性已正确更新。

    【讨论】:

      【解决方案2】:

      我用$form->loadFile('profile');,它也可以。

      【讨论】:

        猜你喜欢
        • 2020-12-13
        • 2011-11-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-04
        • 2015-03-02
        • 1970-01-01
        相关资源
        最近更新 更多