How can I force a title to be entered when uploading? How can I force a title to be entered when uploading?
 

News:

cpg1.5.48 Security release - upgrade mandatory!
The Coppermine development team is releasing a security update for Coppermine in order to counter a recently discovered vulnerability. It is important that all users who run version cpg1.5.46 or older update to this latest version as soon as possible.
[more]

Main Menu

How can I force a title to be entered when uploading?

Started by AndyK, January 21, 2006, 09:13:23 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

AndyK

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.

Paver

The simplest solution is a client-side one using Javascript.

In upload.php, change the open_form function to be as shown:
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:
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:
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.

AndyK

Thank you very much indeed Paver. Your Javascript solution is simple and elegant and works like a charm!
I really appreciate your help! :)

Paver

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!

AndyK

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!