forum.coppermine-gallery.net

No Support => Modifications/Add-Ons/Hacks => Mods: Uploading => Topic started by: AndyK on January 21, 2006, 09:13:23 am

Title: How can I force a title to be entered when uploading?
Post by: AndyK on January 21, 2006, 09:13:23 am
My apologies if this has been answered previously ... I tried an extensive search but couldn't find anything.

I need to be able to ensure that uploaded pictures have a title. That is, I need to mod upload.php (I think) to reject an upload if the title field is blank. My users only have single file upload. I'm not an expert with PHP and am hoping that someone could point me in the right direction.
Title: Re: How can I force a title to be entered when uploading?
Post by: Paver on January 21, 2006, 09:28:54 pm
The simplest solution is a client-side one using Javascript.

In upload.php, change the open_form function to be as shown:
Code: [Select]
function open_form($path) {

    echo <<<EOT
    <script language="javascript" type="text/javascript">
    function textCounter(field, maxlimit) {
            if (field.value.length > maxlimit) // if too long...trim it!
            field.value = field.value.substring(0, maxlimit);
    }
    function ValidateForm(form) {
            title = form.title.value;
            title = title.replace(/^\s*/, '').replace(/\s*$/, '');
            if (title.length == 0) {
alert("You must enter a title."); // not language-compatible
form.title.focus();
return false;
            }
            return true;
    }
    </script>
    <form method="post" action="$path" enctype="multipart/form-data" name="upload_form" onSubmit="javascript:return ValidateForm(this)">
EOT;
}
The changes are adding the ValidateForm function into the Javascript block and adding the name & onSubmit properties to the form tag.  The form name is not actually necessary for this mod but I figured while we were changing things, it's a good idea to name the form.  You might use it for something else.

If you want a language-compatible solution, you need to replace the text in the alert call to something language-compatible.  The closest I could find was this:
Code: [Select]
alert('{$lang_thumb_view['submit']}'+' '+'{$lang_upload_php['pic_title']}');and you have to add this line at the beginning of the open_form function:
Code: [Select]
global $lang_thumb_view, $lang_upload_php;(before the echo <<<EOT line).  There is a 'missing' string used in another script's $lang array, but it would require pretending to be that script to use that string so it's best to stick to the global & upload arrays.
Title: Re: How can I force a title to be entered when uploading?
Post by: AndyK on January 22, 2006, 12:54:25 am
Thank you very much indeed Paver. Your Javascript solution is simple and elegant and works like a charm!
I really appreciate your help! :)
Title: Re: How can I force a title to be entered when uploading?
Post by: Paver on January 22, 2006, 01:38:26 am
You're welcome.  I just realized that I did use the form name upload_form in the code I posted, so I modified it above to use the passed 'form' which is even *more* elegant, if that's possible.

The alert box is just one way to alert the user to enter a title.  For one field, I think it's a good way to do so.  For lots of fields, I've used an image or text next to each field saying what's missing; I'm sure you've seen this on other forms.  You don't want to use a message box for 6 fields and then have the user click OK and not remember which ones they were!
Title: Re: How can I force a title to be entered when uploading?
Post by: AndyK on January 22, 2006, 02:09:09 am
I agree, the alert box is an ideal solution for a single field.
I've made the small change that you mentioned. Thanks once again!