No Code Attached Yet
avatar samuelagus
samuelagus
26 Dec 2022

Steps to reproduce the issue

I have form field "radio" in my custom component (just upgraded from J3! to J4!).
In J3!, if the radio button not selected, the user cannot save the form (showing the error: "Invalid field: Status" in the same page or same form).
In J4!, if the radio button not selected, the user still can save the form (showing the error: "Field required: Status" in the list view/list page after Saving successfully [normally after save form then back to the list page view], but no value from the radio field that saved in the DB because no radio value selected).

I try to change (for testing) from type="radio" to type="list", then add <option value="">~ Select ~</option> in XML form, then save the form without select any option. It showing the error: "One of the options must be selected" above the radio field, as expected.
But I need "radio" field instead of "list" field.

Expected result

If the radio button not selected, show an error "Field required: Status" in the same page (form page), and the form cannot be saved

Actual result

The form can still be saved even if the radio button is not selected, even though required="true" set in XML form field

System information (as much as possible)

Joomla! 4.2.6 (newest)

XML Form File

<field name="status" id="status" type="radio" class="btn-group"
			label="HISTORY_STATUS" 
			description="HISTORY_STATUS" 
		        required="true"
			default="0"
		>
			<option value="1">In Process</option>
  			<option value="2">Return</option>
			<option value="3">Sent</option>
		</field>

Additional comments

I've try to change default="0" to default="" or remove it from XML, still cannot as expected.

Is it a bug or there is something I missed?

avatar samuelagus samuelagus - open - 26 Dec 2022
avatar joomla-cms-bot joomla-cms-bot - change - 26 Dec 2022
Labels Added: No Code Attached Yet
avatar joomla-cms-bot joomla-cms-bot - labeled - 26 Dec 2022
avatar samuelagus samuelagus - change - 26 Dec 2022
The description was changed
avatar samuelagus samuelagus - edited - 26 Dec 2022
avatar samuelagus samuelagus - change - 26 Dec 2022
The description was changed
avatar samuelagus samuelagus - edited - 26 Dec 2022
avatar richard67
richard67 - comment - 26 Dec 2022

@samuelagus What happens if you set the default to one of the existing options, e.g. “1”?

avatar samuelagus
samuelagus - comment - 26 Dec 2022

@richard67 if I set the default to "1", it will automatically choose In Process, it's normal as expected

avatar richard67
richard67 - comment - 26 Dec 2022

@samuelagus Well, I would assume that the default should be one of the available options.

avatar samuelagus
samuelagus - comment - 26 Dec 2022

oh... I see...
My questions are:

  1. My old custom component still work normally in J3.9.23. Why in J3! it could use default="" or default="0" even though there is no "0" in the options value?
  2. I design the radio options as a three big button: In Process Return Sent. So the user can choose the radio option by only (once) click the button easily. Would you give me the tips/tricks how to force the user to choose an (radio) option before the form can be saved or submitted?
    Thank you :)
avatar brianteeman
brianteeman - comment - 26 Dec 2022
  1. You should not the setting a default in the xml - just delete that line (tested and works)
  2. Be careful with the field name/id of status to ensure that it is unique on the form
avatar samuelagus
samuelagus - comment - 26 Dec 2022

@brianteeman Now I remove the default line from XML, then try to save the form without select the radio button. The form save successfully then automatically go to list page, then showing the error notification in top of list page: Field required: Status. All fields values saved successfully in DB except the radio field value because there is no radio value selected before.

In my expectation, the error notification should be shown above the radio field (like the list field I describe before), then prevent to save the form.

avatar brianteeman
brianteeman - comment - 26 Dec 2022

Then you have a problem elsewhere.

Screenshot of my test
image

avatar ReLater
ReLater - comment - 26 Dec 2022

I design the radio options as a three big button: In Process Return Sent. So the user can choose the radio option by only (once) click the button easily. Would you give me the tips/tricks how to force the user to choose an (radio) option before the form can be saved or submitted?

Not tested, but that should be possible by adding

layout="joomla.form.field.radio.buttons"

to your field and remove your own design.

avatar chmst
chmst - comment - 27 Dec 2022

A radio field is defined that it must have one of several alternatives. Otherwise it is a checkbox or checkboxes.
You cold add an option 0 = "waiting" to your list of options, and set "0" as default.

avatar brianteeman
brianteeman - comment - 27 Dec 2022

I thought that as well but I was wrong see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/radio

and anyway I have shown above that the users code does work correctly as they intend when used with a core component

avatar chmst
chmst - comment - 27 Dec 2022

Yes, this is possible - but from UX it looks like a bug (maybe only for me). I expect always an option like "none".

In the following link we see some examples with big buttons - they use checkboxes which seems a better solution and radio also with buttons. Both examples look good for me.
https://ux.stackexchange.com/questions/76181/radio-buttons-with-none-selected

avatar brianteeman
brianteeman - comment - 27 Dec 2022

the reason to use radio groups is so that you can only check 1/3 options. checkboxes dont restrict you in that way

avatar samuelagus
samuelagus - comment - 7 Jan 2023

@samuelagus Well, I would assume that the default should be one of the available options.

A radio field is defined that it must have one of several alternatives. Otherwise it is a checkbox or checkboxes. You cold add an option 0 = "waiting" to your list of options, and set "0" as default.

@richard67 @chmst If I add an option "0" then set "0" as default, when the user forget to choose the "status" field ( "1" | "2" | "3"), then the status field will be save as "0". It's not what I want :)
I just want to force the user to choose one of the radio options, otherwise the form won't be saved.

How to force a "required" field or have a validation, so the status field cannot be "0" or "" (blank)?

avatar samuelagus
samuelagus - comment - 7 Jan 2023

I design the radio options as a three big button: In Process Return Sent. So the user can choose the radio option by only (once) click the button easily. Would you give me the tips/tricks how to force the user to choose an (radio) option before the form can be saved or submitted?

Not tested, but that should be possible by adding

layout="joomla.form.field.radio.buttons"

to your field and remove your own design.

@ReLater I've remove the css so now it looks like plain radio options, then add layout="joomla.form.field.radio.buttons" still get the same result: the form saved without "status" radio field chosen, even though I have add required="true" in the XML form

avatar samuelagus
samuelagus - comment - 7 Jan 2023

I thought that as well but I was wrong see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/radio

and anyway I have shown above that the users code does work correctly as they intend when used with a core component

@brianteeman I'm confused, what's wrong with my codes. It worked normally in J3! until now I'm still using it in J3!

avatar ReLater
ReLater - comment - 8 Jan 2023

@samuelagus

@ReLater I've remove the css so now it looks like plain radio options, then add layout="joomla.form.field.radio.buttons" still get the same result: the form saved without "status" radio field chosen, even though I have add required="true" in the XML form

Yes, you're right. I relied on the PHP code of that JLayout. Later I tried a lot of variations with 3 buttons and never had a CLIENT-SIDE(!!!) validation that told me that the field is required. Only after save & page reload a message like in your opening comment (SERVER-SIDE validation). But I was very tired and didn't check if something was saved in DB.

avatar joomdonation joomdonation - change - 8 Jan 2023
Status New Closed
Closed_Date 0000-00-00 00:00:00 2023-01-08 16:09:21
Closed_By joomdonation
avatar joomdonation joomdonation - close - 8 Jan 2023
avatar joomdonation
joomdonation - comment - 8 Jan 2023

PR #39578 should address this issue. Please help testing. Thanks !

avatar samuelagus
samuelagus - comment - 9 Jan 2023

PR #39578 should address this issue. Please help testing. Thanks !

I have tested #39578

The result as expected, there is an error message above the radio field: One of the options must be selected when no radio option selected :)

avatar joomdonation
joomdonation - comment - 9 Jan 2023

@samuelagus That mean the PR solves your issue? If so, please go to https://issues.joomla.org/tracker/joomla-cms/39578 , click on Test this to report your test result as Success so that the PR could be merged.

Add a Comment

Login with GitHub to post a comment