J4 Custom FormField

121 views
Skip to first unread message

MonkeyT

unread,
Mar 2, 2022, 12:04:59 AM3/2/22
to Joomla! General Development
I'm getting very comfortable working on Joomla 4 components, but I haven't found a good example or tutorial on creating Joomla 4 custom FormFields.  I need to rate each of 18 statements in a questionnaire using a set of five radio buttons for each.  My J3 implementation refers to each of these sets as a single 'ranking' element with five options to choose from.

They were pretty well documented in Joomla 3. Even though Joomla 4 is reading my file, it's not instantiating the object or using its methods.

Any hints or suggestions?

Peter Tuson

unread,
Apr 13, 2022, 9:12:16 AM4/13/22
to Joomla! General Development
I have created custom form fields for the admin side of my component.

Firstly, the fields are within a Field folder with the src folder. Each Field is given a unique name with Field at the end, e.g. MyformField.

The field starts with a namespace definition, e.g. MyCompany\Component\MyComponent\Administrator\Field;

If it is a listfield, then add 'use Joomla\CMS\Form\Field\ListField;', and then add your new class, e.g. class MyformField extends ListField. Then mkae sure that the rest of the formfile is compliant with J4.

The actual form is help within the forms folder, e.g myform.xml. To make sure that the form knows where to find the custom form field then you add the namespace as a prefix to the fieldset, e.g.
<fieldset addfieldprefix="MyCompany\Component\MyComponent\Administrator\Field">

This works for me and I hope it does for you.

Regards,

Peter
Reply all
Reply to author
Forward
0 new messages