dongshi7433 2017-01-04 12:22
浏览 27
已采纳

Joomla - 在插件中设置字段属性

I created a user plugin for extending default user profile in Joomla 3.6.5. I added the following field sample_title in profiles/profile.xml.

<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>

in my plugin class I have a function onContentPrepareForm($form, $data). In this function I try to change the required attribute of the sample_title by doing $form->setFieldAttribute('sample_title', 'required', 'true', 'profilecustom'); If i see the required attribute value on the next line it seems it is ok the required is "true".This code echo $form->getFieldAttribute('sample_title', 'required', 'false', 'profilecustom'); prints true which is OK for me. BUT when I display the registration view (I use the registration/default.php view from com_users) the sample_title field is declared as optional. In the registration view, we have

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

and this span is displayed for sample_title while I set the required field attribute to true so it is not OK...

As additional information:

  • I tried $form->setFieldAttribute('sample_title', 'required', 'true', 'profilecustom'); and $form->setFieldAttribute('sample_title', 'required', true, 'profilecustom');. The result is the same.
  • I followed the following documentation (https://docs.joomla.org/Creating_a_profile_plugin) to create my plugin.

Am I doing something wrong? I know that I can put the required value in the field declaration

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

but this is not what I want. I want to set this value under some conditions so I need to do it programmatically.


I continued my investigation :-) In the registration view, if I use the default way to display the fields

<?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 is false but if I try to get the field with $sample_title_field = $this->form->getField('sample_title', 'profilecustom'); then $sample_title_field->required is true so It seems there is some inconsistency between the fields returned by $this->form->getFieldset($fieldset->name); and those those get with $this->form->getField(...);

  • 写回答

2条回答 默认 最新

  • dqj29136 2017-01-04 16:10
    关注

    In the example code (https://docs.joomla.org/Creating_a_profile_plugin), they do $form->loadFile('profile', false);. I replaced this code by $form->loadFile('profile', true); and now the field attributes are correctly updated.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑