No Code Attached Yet
avatar pioada
pioada
3 Apr 2025

File form field marked as required always returns validation error.
It has been reported earlier #12832 but the problem was not solved.
This field type does not contain any value. Problem is in: libraries > src > Form > FormField in line 1201 which checks for value if field is required:
if ($required && (($value === '') || ($value === null)))

Possible solution is to check if this is not file field type eg:
if ($this->element['type'] != 'file' && $required && (($value === '') || ($value === null)))

This solution restores proper functionality. We have front-end verification and can do server-side veryfication with rules.

Steps to reproduce the issue

Create a form with a file field marked as required. Attach a file. Save form.

Expected result

Success message

Actual result

Error message saying that the field is not filled in.

System information (as much as possible)

Joomla 4.4.9, PHP 7.4.33
Same on 5.2.4

Additional comments

avatar pioada pioada - open - 3 Apr 2025
avatar joomla-cms-bot joomla-cms-bot - change - 3 Apr 2025
Labels Added: No Code Attached Yet
avatar joomla-cms-bot joomla-cms-bot - labeled - 3 Apr 2025
avatar brianteeman
brianteeman - comment - 3 Apr 2025

https://docs.joomla.org/File_form_field_type

required Cannot be used with this field type. If the field is marked as required it will always fail validation regardless of whether a file has been uploaded or not. The suggested workaround is to add a filerequired attribute which can be tested in your own file handling code.

avatar pioada
pioada - comment - 3 Apr 2025

It is irracional to check the value for element that does not return any value. It always fails.
With my suggestion it wont be necessary to create any workarounds, hacks, etc. It will work based on core client-side validation functionality and will skip value check on server-side. It's just a change of one line of code and file field will work fine without any extra code.

avatar ayeshabarlas
ayeshabarlas - comment - 3 Apr 2025

I think there might be location error,Locate the File to libraries/src/Form/FormField.php in your Joomla installation or Find line 1201, which checks if the field is required. Change it to:

if ($this->element['type'] != 'file' && $required && (($value === '') || ($value === null)))
Save and then try!

avatar pioada
pioada - comment - 4 Apr 2025

@ayeshabarlas I have already done this and everything works fine after implementing this change. Now it is good to push this into J! code.

Add a Comment

Login with GitHub to post a comment