How to get Address attribute to show as 'required' like the rest of the user attributes

Looking for a simple way to get the Address attribute to show as ‘Required’ on the /register page. Apparently, some attributes just don’t have that ability. Why not? I have over-ridden the /register page and found I can shove this code into the loop which finds attributes where the ‘required’ flag was set in the Dashboard but then it spits out 2 ‘required’ labels on all the other attributes.

if ($ak->isAttributeKeyRequiredOnRegister()){echo "<div class='required'>Required...</div>";}

I have edited my post because the correct solution has been provided by @mnakalay in post number 4

LOL… I’m not sure if you’re being a wise guy but that’s what I did. The problem is that now the word ‘Required’ shows twice. Once for those attributes that honor the ‘Require on Registration Form’ in the Dashboard and a second time when this code modification sees that it’s supposed to show on the Register form. I’ve solved it by wrapping things in a DIV and using CSS to hide the core’s inconsistent presentation of ‘Required’. To my thinking, these hoops are a complete and unnecessary hack. Why have a “Require on Registration Form” checkbox at all if it’s not applied consistently? I would think that requiring a person’s address might be ‘required’ in lots of contexts but perhaps I’m missing something deeper in the implementation of the ‘required’ feature.

Actually there’s an easier way.

The address attribute is considered a grouped form element. In Concrete v9 Its rendering depends on the file concrete\elements\form\grouped\bootstrap5.php

The other non grouped ones depend on concrete\elements\form\bootstrap5.php

In v8 it will be the file bootstrap3.php

Simply copy concrete\elements\form\grouped\bootstrap5.php to application\elements\form\grouped\bootstrap5.php and modify it to your liking. It will apply right away.

Take a look at how it’s done in concrete\elements\form\bootstrap5.php to see how to deal with labels and if it’s required.

For instance, I modified it like this

<?php
defined('C5_EXECUTE') or die("Access Denied.");
?>

<fieldset class="mb-3">
    <?php if ($view->supportsLabel()) { ?>
        <label class="form-label fs-5" for="<?=$view->getControlID()?>"><?=$view->getLabel()?></label>
    <?php } ?>

    <?php if ($view->isRequired()) { ?>
        <span class="text-muted small"><?=t('Required')?></span>
    <?php } ?>

    <?php $view->renderControl()?>
</fieldset>

I added the class name fs-5 to make the main label slightly bigger than the fields labels.

I kept the fieldset since it indicates grouped elements but got rid of the legend and replaced it with a label.

I hope this helps.

1 Like

Duh… it was just that simple. I got lost looking much deeper. Why the missing ‘isRequired’ logic for the grouped form elements?

I’m not sure, You’d have to ask the core team.

I’m guessing being a multiple-input thing it creates challenges. Accessibility challenges for instance.

So maybe keeping the legend for the fieldset is the best choice and add the required there. But then is it understandable what fields are actually required? Typically, address line 1 is required but not line 2. How do we indicate that?

I’m not sure if there is a perfect solution to be honest.

And last but not least, as far as the core is concerned I think only the address attribute is grouped but third-party attributes might also use that grouping and modifying it might have unexpected results.