×
Please submit new Bug Reports on GitHub: github.com/Jensen-Technologies/component-creator-issues/issues
File field (upload)
Székely Dénes
New Member
Posts: 4
4 năm 6 tháng trước #9720
bởi Székely Dénes
File field (upload) was created by Székely Dénes
Setting the File type of field to Unique seems to not help when I try to prevent that subsequent uploads of files with the same name to be overwritten. Despite the fact that field is set to unique I am falling in one of these situation:
a. I leave the form XML as the Component Creator generates it. This has the result of a field like this:
<field name="file" type="FileMultiple" required="true" label="COM_FILES_FORM_LBL_FILE_FILE" description="COM_FILES_FORM_DESC_FILE_FILE" hint="COM_FILES_FORM_LBL_FILE_FILE"/>
This happening because the Unique parameter automatically sets the fields to Required. This ends up with a validation problem, as described here: 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.
b. I edit the form XML, and remove the required="true" part. This solves the validation error - but kills the Unique thing.
Any tips to solve the problem is greatly appreciated.
a. I leave the form XML as the Component Creator generates it. This has the result of a field like this:
<field name="file" type="FileMultiple" required="true" label="COM_FILES_FORM_LBL_FILE_FILE" description="COM_FILES_FORM_DESC_FILE_FILE" hint="COM_FILES_FORM_LBL_FILE_FILE"/>
This happening because the Unique parameter automatically sets the fields to Required. This ends up with a validation problem, as described here: 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.
b. I edit the form XML, and remove the required="true" part. This solves the validation error - but kills the Unique thing.
Any tips to solve the problem is greatly appreciated.
Vui lòng Đăng nhập hoặc Tạo tài khoản để tham gia cuộc hội thoại.
Székely Dénes
New Member
Posts: 4
4 năm 5 tháng trước #9723
bởi Székely Dénes
Replied by Székely Dénes on topic File field (upload)
I decided to try to solve it another way, to add a server side validation, which checks, if the filename already exist in the database.
Here what I did:
In the form's XML file I added this:And I also modified the line wich handles the file upload field:Then I created the /rules subdir in models (as above) and added a file named file.php with this function (don't paste there the non essential parts)And nothing changed, I can still upload the same file again and again, generating new entries in database and the first upload getting overwritten. What I am doing wrong??
Here what I did:
In the form's XML file I added this:
<fieldset addrulepath="/components/com_files/models/rules" >
<field name="file" type="FileMultiple" label="COM_FILES_FORM_LBL_FILE_FILE" validate="file" description="COM_FILES_FORM_DESC_FILE_FILE" hint="COM_FILES_FORM_LBL_FILE_FILE" message="YCOM_FILES_FORM_ERROR_FILE_EXIST" />
use \Joomla\CMS\Factory;
/**
* Form Rule class for the Joomla Framework.
*/
class JFormRuleFile extends JFormRule
{
public function test(& $element, $value, $group = null, & $input = null, & $form = null)
{
$db = Factory::getDbo();
$query = $db->getQuery(true);
$query
->select($db->quoteName('file'))
->from($db->quoteName('#__files_file'))
->where($db->quoteName('file') . ' LIKE ' . $db->quote($value));
$db->setQuery($query);
$db->execute();
return ($db->getNumRows() == 0) ? true : false;
}
}
Vui lòng Đăng nhập hoặc Tạo tài khoản để tham gia cuộc hội thoại.
Søren Beck Jensen
Administrator
Posts: 81
4 năm 5 tháng trước #9724
bởi Søren Beck Jensen
Søren Beck Jensen
Founder, Component-Creator.com
Replied by Søren Beck Jensen on topic File field (upload)
When and how are files written into that table? Why are they written into the table?
I would instead solve this by looking to see if the file actually exists. You can use file_exists($fullPath) or JFile::exists($fullPath) to see if the file actually exists and then skip the database stuff all together. That way it will also work if the file is deleted or moved.
But I think the problem in your code is that you use $db->execute(); which I don't believe is meant for select statements. I would instead try the following.
$match = $db->loadResult();
But that expects just one row and there is a slim chance that you may have more than one. So also make a limit on your query to 1 row.
$query->setLimit('1');
Then you can simply return (bool) $match;
I would instead solve this by looking to see if the file actually exists. You can use file_exists($fullPath) or JFile::exists($fullPath) to see if the file actually exists and then skip the database stuff all together. That way it will also work if the file is deleted or moved.
But I think the problem in your code is that you use $db->execute(); which I don't believe is meant for select statements. I would instead try the following.
$match = $db->loadResult();
But that expects just one row and there is a slim chance that you may have more than one. So also make a limit on your query to 1 row.
$query->setLimit('1');
Then you can simply return (bool) $match;
Søren Beck Jensen
Founder, Component-Creator.com
Những thành viên sau(s) đã Cảm ơn: Székely Dénes
Vui lòng Đăng nhập hoặc Tạo tài khoản để tham gia cuộc hội thoại.
Székely Dénes
New Member
Posts: 4
4 năm 5 tháng trước #9729
bởi Székely Dénes
Replied by Székely Dénes on topic File field (upload)
Filenames are written in the table. I added a FileMultiple type of field in the form, which handles the upload. The files are uploaded to server, in a given directory. Both are working, currently, the file is uploaded, the name is stored in the database.
I need to tweak that part to prevent upload of files with names already in use.
So, appreciate the responses, but WHERE I should add the code you are suggesting?
BTW, selecting in ComponentCreator the FileMultiple field as being Unique triggers a catch 22 situation: You cannot upload any files anymore, because selecting Unique means setting the field as mandatory, and as is stated in your docs, thie renders the field unusable. So, take it simple:
1. I need to upload a file. A single file.
2. I need to be sure, that a file with same name isn't exist already in the system.
Period. If that's working, reliably, I can sort out the rest.
I need to tweak that part to prevent upload of files with names already in use.
So, appreciate the responses, but WHERE I should add the code you are suggesting?
BTW, selecting in ComponentCreator the FileMultiple field as being Unique triggers a catch 22 situation: You cannot upload any files anymore, because selecting Unique means setting the field as mandatory, and as is stated in your docs, thie renders the field unusable. So, take it simple:
1. I need to upload a file. A single file.
2. I need to be sure, that a file with same name isn't exist already in the system.
Period. If that's working, reliably, I can sort out the rest.
Vui lòng Đăng nhập hoặc Tạo tài khoản để tham gia cuộc hội thoại.
Székely Dénes
New Member
Posts: 4
4 năm 5 tháng trước #9730
bởi Székely Dénes
Replied by Székely Dénes on topic File field (upload)
Thank you, based on your tip, I worked it out. But this should be easier in the future - take it as a feature request. If someone needs a file upload functionality, would not nbe happy if files get accidentally overwritten.Period. If that's working, reliably, I can sort out the rest.
Vui lòng Đăng nhập hoặc Tạo tài khoản để tham gia cuộc hội thoại.
Søren Beck Jensen
Administrator
Posts: 81
4 năm 5 tháng trước #9741
bởi Søren Beck Jensen
Søren Beck Jensen
Founder, Component-Creator.com
Replied by Søren Beck Jensen on topic File field (upload)
I completely agree, but this should be solved by Joomla's file field.
Søren Beck Jensen
Founder, Component-Creator.com
Vui lòng Đăng nhập hoặc Tạo tài khoản để tham gia cuộc hội thoại.
Thời gian tải trang: 0.055 giây