How to set a default checked custom field in registration form

giorgino

Well-known member
I've set up a custom user field "newsletter" as radio button ("yes" or "not" values) but I need to make "yes" the default choice of the field on registration.

Registrati | immobilio | Forum Immobiliare.webp

How to do this? o_O
 
And adding manually a piece of code in registration form? Can solve the problem?

I suppose that would work:

Admin CP -> Appearance -> Templates -> custom_field_edit

Add the red code:

Rich (BB code):
		<xen:elseif is="{$field.field_type} == 'radio'" />
			<ul class="checkboxColumns">
			<xen:if is="!{$field.required}">
				<li><label><input type="radio" name="custom_fields[{$field.field_id}]" value="" {xen:checked '{$field.field_value} == ""'} /> <span class="muted">{xen:phrase no_selection}</span></label></li>
			</xen:if>
			<xen:foreach loop="$field.fieldChoices" key="$choice" value="$text">
				<li><label><input type="radio" name="custom_fields[{$field.field_id}]" value="{$choice}" {xen:checked '{$field.field_value} == {$choice} OR ({$field.field_id} == "radiofield" AND {$choice} == "no" AND !{$field.field_value})'} /> {xen:raw $text}</label></li>
			</xen:foreach>
			</ul>

You need to name the field_id and the option that is to be the default. Kind of hackish, but it works.
 
One issue. The same "default" checked field is in user preferences page also... but it's only cosmetic. No real preference is selected if user don't click the save button.

Schermata 02-2455982 alle 16.40.06.webp
 
I suppose that would work:

Admin CP -> Appearance -> Templates -> custom_field_edit

Add the red code:

Rich (BB code):
<xen:elseif is="{$field.field_type} == 'radio'" />
<ul class="checkboxColumns">
<xen:if is="!{$field.required}">
<li><label><input type="radio" name="custom_fields[{$field.field_id}]" value="" {xen:checked '{$field.field_value} == ""'} /> <span class="muted">{xen:phrase no_selection}</span></label></li>
</xen:if>
<xen:foreach loop="$field.fieldChoices" key="$choice" value="$text">
<li><label><input type="radio" name="custom_fields[{$field.field_id}]" value="{$choice}" {xen:checked '{$field.field_value} == {$choice} OR ({$field.field_id} == "radiofield" AND {$choice} == "no" AND !{$field.field_value})'} /> {xen:raw $text}</label></li>
</xen:foreach>
</ul>

You need to name the field_id and the option that is to be the default. Kind of hackish, but it works.

Can you give me a trick to Drop Down menus?
 
Can you give me a trick to Drop Down menus?

Rich (BB code):
		<xen:elseif is="{$field.field_type} == 'select'" />
			<select name="custom_fields[{$field.field_id}]" id="ctrl_custom_field_{$field.field_id}" class="textCtrl">
			<xen:if is="!{$field.required} OR !{$field.hasValue}">
				<option value="">&nbsp;</option>
			</xen:if>
			<xen:foreach loop="$field.fieldChoices" key="$choice" value="$text">
				<option value="{$choice}" {xen:selected '{$field.field_value} == {$choice} OR ({$field.field_id} == "fieldid" AND {$choice} == "defaultvalue" AND !{$field.field_value})'}>{xen:raw $text}</option>
			</xen:foreach>
			</select>
 
Rich (BB code):
<xen:elseif is="{$field.field_type} == 'select'" />
<select name="custom_fields[{$field.field_id}]" id="ctrl_custom_field_{$field.field_id}" class="textCtrl">
<xen:if is="!{$field.required} OR !{$field.hasValue}">
<option value="">&nbsp;</option>
</xen:if>
<xen:foreach loop="$field.fieldChoices" key="$choice" value="$text">
<option value="{$choice}" {xen:selected '{$field.field_value} == {$choice} OR ({$field.field_id} == "fieldid" AND {$choice} == "defaultvalue" AND !{$field.field_value})'}>{xen:raw $text}</option>
</xen:foreach>
</select>

Thanks again.... :love:
 
Will this work?
Code:
            <ul class="checkboxColumns">
            <xen:foreach loop="$field.fieldChoices" key="$choice" value="$text">
                <li><label><input type="checkbox" name="{$customFieldInputName}[{$field.field_id}][{$choice})]" value="{$choice}" {xen:checked 'isset({$field.field_value.{$choice}}) OR ({$field.field_id} == "newsletter_news" AND !{$field.field_value})'} /> {xen:raw $text}</label></li>
            </xen:foreach>
            </ul>
 
One issue. The same "default" checked field is in user preferences page also... but it's only cosmetic. No real preference is selected if user don't click the save button.

View attachment 26237
Same problem. The box is checked by defaul, but it is not applied.

That is expected because the template edit only affects the default value in the form. The form must be submitted.

To force an update to existing user records you must run a query:

Rich (BB code):
INSERT INTO xf_user_field_value (user_id, field_id, field_value)
	SELECT user_id, 'field_id', 'value'
	FROM xf_user
ON DUPLICATE KEY UPDATE
	field_value = VALUES(field_value);

You need to specify the field_id of the custom field and the stored value which you want to write to everyone's profile.

Then rebuild the user cache:

Admin CP -> Tools -> Rebuild Caches -> Rebuild User Caches
 
Sorry Jake, but I need to make "yes" the default choice of the field on registration, not after registration.
I don't understand how a query on existing records can help me with my question.

Is there something that I don't understand?
 
Top Bottom