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.
If the radio button not selected, show an error "Field required: Status" in the same page (form page), and the form cannot be saved
The form can still be saved even if the radio button is not selected, even though required="true"
set in XML form field
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>
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?
Labels |
Added:
No Code Attached Yet
|
@richard67 if I set the default to "1", it will automatically choose In Process
, it's normal as expected
@samuelagus Well, I would assume that the default should be one of the available options.
oh... I see...
My questions are:
default=""
or default="0"
even though there is no "0" in the options value?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?@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.
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.
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.
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
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
the reason to use radio groups is so that you can only check 1/3 options. checkboxes dont restrict you in that way
@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)?
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
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!
@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.
Status | New | ⇒ | Closed |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2023-01-08 16:09:21 |
Closed_By | ⇒ | joomdonation |
@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.
@samuelagus What happens if you set the default to one of the existing options, e.g. “1”?