0
votes

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

2 Answers

0
votes

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.

0
votes

I use $form->loadFile('profile');, it works too.