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

Discussion in 'XenForo Questions and Support' started by giorgino, Feb 21, 2012.

  1. giorgino

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

    How to do this? o_O
     
  2. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    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".
     
    giorgino likes this.
  3. giorgino

    giorgino Well-Known Member

    :( Isn't possibile to define something here?

    Google Chrome.png
     
  4. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    That is for displaying the value of a field after a user has chosen one of the options. It doesn't specify a default value which is what you need.
     
    radu81 and giorgino like this.
  5. giorgino

    giorgino Well-Known Member

    :unsure: ok...

    And adding manually a piece of code in registration form? Can solve the problem?
     
  6. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    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 likes this.
  7. giorgino

    giorgino Well-Known Member

    I'll test it and report back for future reference :)
    You're great Jack! (y)
     
  8. giorgino

    giorgino Well-Known Member

    Work great! :love:

    Parallels Desktop.png
     
  9. giorgino

    giorgino Well-Known Member

    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. Gabriel Pinho

    Gabriel Pinho Member

    Can you give me a trick to Drop Down menus?
     
  11. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    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>
    
     
    Gabriel Pinho likes this.
  12. Gabriel Pinho

    Gabriel Pinho Member

    Thanks again.... :love:
     
  13. Allan

    Allan Well-Known Member

    Same problem. The box is checked by defaul, but it is not applied.
     
  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>
     
  15. Allan

    Allan Well-Known Member

    giorgino likes this.
  16. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    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
     
    Allan likes this.
  17. giorgino

    giorgino Well-Known Member

    But is this true for registration form also? o_O
     
  18. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    Yes. The registration form must be submitted for the value to be saved, which will always happen in the case of new registrations so it's not relevant.
     
    giorgino likes this.
  19. giorgino

    giorgino Well-Known Member

    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?
     
  20. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    The template modification does that.
     

Share This Page