XF 2.3 Setting a default for custom user field options

Mr Lucky

Well-known member
I have a custom user field with two options. I want to start off with one of them (radio button) chosen

e.g. Do you wear a hat? Yes / No

I want No to be the default for all so I'm assuming I can do this with a database query

I tried this but to no avail

Code:
INSERT INTO xf_user_field_value ('field_id', 'field_value')
VALUES ('wearHat', 'No')

Does anyone know what I need to do? Thanks
 
Solution
I have a custom user field with two options. I want to start off with one of them (radio button) chosen

e.g. Do you wear a hat? Yes / No

I want No to be the default for all so I'm assuming I can do this with a database query

I tried this but to no avail

Code:
INSERT INTO xf_user_field_value ('field_id', 'field_value')
VALUES ('wearHat', 'No')

Does anyone know what I need to do? Thanks
Hi. I am not a proponent of working with Xenforo via direct database queries, but since I don't remember any other way to accomplish this task and don't remember any plugins that allow you to do it, I will help you. It's not possible to do it with a simple query.
1) Execute SQL query:
SQL:
INSERT INTO xf_user_field_value SELECT user.user_id...
I have a custom user field with two options. I want to start off with one of them (radio button) chosen

e.g. Do you wear a hat? Yes / No

I want No to be the default for all so I'm assuming I can do this with a database query

I tried this but to no avail

Code:
INSERT INTO xf_user_field_value ('field_id', 'field_value')
VALUES ('wearHat', 'No')

Does anyone know what I need to do? Thanks
Hi. I am not a proponent of working with Xenforo via direct database queries, but since I don't remember any other way to accomplish this task and don't remember any plugins that allow you to do it, I will help you. It's not possible to do it with a simple query.
1) Execute SQL query:
SQL:
INSERT INTO xf_user_field_value SELECT user.user_id, 'wearHat' AS field_id, 'No' AS field_value FROM xf_user AS user WHERE NOT EXISTS (SELECT 1 FROM xf_user_field_value AS field WHERE user.user_id = field.user_id AND field.field_id = 'wearHat')
This query will select all users who have not yet configured a custom field with the id “wearHat” and set them to “No”.
2) Rebuild the user cache /admin.php?tools/rebuild
 
Solution
I have a custom user field with two options. I want to start off with one of them (radio button) chosen
The continual problem that you'll have is that the default for a new user will be "No selection" (null), so you'll need to query it all the time (unless you clutter your user registration page and make it a required input).

It should be that XF creates a radio button for yes/no only and allows for you to set the default to either or, but unfortunately, that's not the case.
 
The continual problem that you'll have is that the default for a new user will be "No selection" (null), so you'll need to query it all the time (unless you clutter your user registration page and make it a required input).
That's a good point but it will be a required input so going forward there will always be a selection.
 
  • Like
Reactions: frm
Back
Top Bottom