• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

How to set a default checked custom field in registration form

giorgino

Well-known member
#1
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.jpg

How to do this? o_O
 

Jake Bunce

XenForo moderator
Staff member
#2
There is no such option.

What about changing the field to an "opt out"? That way if they don't check anything then they are considered "in".
 

Jake Bunce

XenForo moderator
Staff member
#6
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:

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.
 

giorgino

Well-known member
#9
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.png
 
#10
I suppose that would work:

Admin CP -> Appearance -> Templates -> custom_field_edit

Add the red code:

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?
 

Jake Bunce

XenForo moderator
Staff member
#11
Can you give me a trick to Drop Down menus?
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>
 
#12
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:
 
#14
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>
 

Jake Bunce

XenForo moderator
Staff member
#16
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:

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
 

giorgino

Well-known member
#19
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?