Advanced search  

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]

Pages: [1]   Go Down

Author Topic: How can I force a title to be entered when uploading?  (Read 11520 times)

0 Members and 1 Guest are viewing this topic.

AndyK

  • Coppermine newbie
  • Offline Offline
  • Posts: 3
How can I force a title to be entered when uploading?
« 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.
« Last Edit: January 22, 2006, 03:16:36 pm by GauGau »
Logged

Paver

  • Dev Team member
  • Coppermine addict
  • ****
  • Country: us
  • Offline Offline
  • Gender: Male
  • Posts: 1609
  • Paul V.
Re: How can I force a title to be entered when uploading?
« Reply #1 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.
« Last Edit: January 22, 2006, 01:35:27 am by Paver »
Logged

AndyK

  • Coppermine newbie
  • Offline Offline
  • Posts: 3
Re: How can I force a title to be entered when uploading?
« Reply #2 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! :)
Logged

Paver

  • Dev Team member
  • Coppermine addict
  • ****
  • Country: us
  • Offline Offline
  • Gender: Male
  • Posts: 1609
  • Paul V.
Re: How can I force a title to be entered when uploading?
« Reply #3 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!
Logged

AndyK

  • Coppermine newbie
  • Offline Offline
  • Posts: 3
Re: How can I force a title to be entered when uploading?
« Reply #4 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!
Logged
Pages: [1]   Go Up
 

Page created in 0.02 seconds with 20 queries.