A Joomla 5 site has around 50 custom fields, like text, integer, or list. None of them are set as required, so some articles might have certain fields, while others have different ones.
When you're adding an article, you get a <select> field for list-type fields, showing the options you've got. Also, integer fields default to 1.
Now, the tricky part is when you want to leave these fields empty for an article that doesn't need them. For list-type, you can put a space as the first field, not the prettiest, but it works. But what about integer fields? Setting a default of 1 doesn't really fit when you want to leave it blank.
So how to NOT set a field value to an article?
Any ideas?
Using non-required custom fields
Moderator: General Support Moderators
Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10
-
- Joomla! Intern
- Posts: 54
- Joined: Mon Jun 19, 2017 10:57 am
Using non-required custom fields
Last edited by imanickam on Tue Nov 14, 2023 3:28 pm, edited 1 time in total.
Reason: Moved topic » from Administration Joomla! 4.x to Administration Joomla! 5.x
Reason: Moved topic » from Administration Joomla! 4.x to Administration Joomla! 5.x
- ceford
- Joomla! Hero
- Posts: 2314
- Joined: Mon Feb 24, 2014 10:38 pm
- Location: Edinburgh, Scotland
- Contact:
Re: Using non-required custom fields
Start the integer list at 0. Select 0 in the field in the article.
For the list you can set the first label to - Select - and then leave the value empty.
For the list you can set the first label to - Select - and then leave the value empty.
-
- Joomla! Intern
- Posts: 54
- Joined: Mon Jun 19, 2017 10:57 am
Re: Using non-required custom fields
Thanks, but 0 is a valid value for me, I cannot set it as "empty".
Also this is about the basics, how is this matching with Required=No setting in the field settings? I set the field not required, because I don't always need it.
- ceford
- Joomla! Hero
- Posts: 2314
- Joined: Mon Feb 24, 2014 10:38 pm
- Location: Edinburgh, Scotland
- Contact:
Re: Using non-required custom fields
A value of 0 or an empty string (depends on the type of field) is treated as not set and the field is not displayed in the output.
Lists use the first item in the list as the default. So you could select an integer from -10 to +10 with 0 in the middle. The default value would be -10 unless you set 0 as the Default Value. It is not possible to set an empty value for an integer because you have to choose a value from the list and the default is pre-selected.
Setting Required is most appropriate for text and text area fields. You cannot submit the form unless text is entered.
For a radio field you can set one choice to be empty (so not displayed in the output) but you can't then make it required. The form will submit but the data will not be saved.
Not being able to display an integer value of 0 does seem to be a problem. You may be able to solve that with a layout override for components/com_fields/layouts/fields/render.php by testing for rawvalue rather than value.
Lists use the first item in the list as the default. So you could select an integer from -10 to +10 with 0 in the middle. The default value would be -10 unless you set 0 as the Default Value. It is not possible to set an empty value for an integer because you have to choose a value from the list and the default is pre-selected.
Setting Required is most appropriate for text and text area fields. You cannot submit the form unless text is entered.
For a radio field you can set one choice to be empty (so not displayed in the output) but you can't then make it required. The form will submit but the data will not be saved.
Not being able to display an integer value of 0 does seem to be a problem. You may be able to solve that with a layout override for components/com_fields/layouts/fields/render.php by testing for rawvalue rather than value.
-
- Joomla! Intern
- Posts: 54
- Joined: Mon Jun 19, 2017 10:57 am
Re: Using non-required custom fields
Thanks! I get it, and I can deal with it. But it's kinda weird that even when I mark a field as not required, it still shows up as kind of 'required' and behaves like it needs special treatment.ceford wrote: ↑Thu Nov 16, 2023 8:58 amA value of 0 or an empty string (depends on the type of field) is treated as not set and the field is not displayed in the output.
Lists use the first item in the list as the default. So you could select an integer from -10 to +10 with 0 in the middle. The default value would be -10 unless you set 0 as the Default Value. It is not possible to set an empty value for an integer because you have to choose a value from the list and the default is pre-selected.
Setting Required is most appropriate for text and text area fields. You cannot submit the form unless text is entered.
For a radio field you can set one choice to be empty (so not displayed in the output) but you can't then make it required. The form will submit but the data will not be saved.
Not being able to display an integer value of 0 does seem to be a problem. You may be able to solve that with a layout override for components/com_fields/layouts/fields/render.php by testing for rawvalue rather than value.
A suggestion:
How about adding a button or something like 'I don't want to use this field' for the non-required ones? In that case, it could be greyed out or look different, and it wouldn't save anything to #__fields_values for that field/article combo.
I won't say VirtueMart is a good example, but with VM, if you don't need a field, you simply don't assign it to the product.
Less entries in the database, more precise results.
What do you think?