Gossamer Forum
Home : Products : DBMan : Customization :

Where to place zero padding code

Quote Reply
Where to place zero padding code
I have a month and day field both of which are two digits in length. I want to keep any numbers that are less than 10 padded with a leading zero. I know how to write the code to check for and insert the leading zero but I don't know the beat place to put it in the dbman code. What I'm looking for is to allow users to enter a one digit month or date and have dbman insert the leading zero so they don't have to.

Let's say they enter "1" for January... I want the month field to be written as "01" without the user having to remember to add the leading zero. I'd probably have to add the "leading zero" code to the "add new record" and "modify record" subs at minimum (I think) Is there a batter place to perform this check where I'd only have to write it once?

I was originally thinking I could do it with valid_expr but then I realized that valid_expr only checks to see if your field is valid and it doesn't actually change it to make it valid.
Quote Reply
Re: [acravens] Where to place zero padding code In reply to
It would be easiest to just make those fields select fields so they will be entered correctly to have the database run as it should. DBMan is very picky about the format for the dates!

If you really want to keep things as they are I suggest you get the following mod which you will most likely need to allow users to enter anything in the date fields and have DBMan be able to process them correctly.

Universal Date Translator Mod
Written by JPDeni (Carol Hall)

http://www.jpdeni.com/dbman/Mods/date_translator.txt

Unoffical DBMan FAQ

http://creativecomputingweb.com/dbman/index.shtml/
Quote Reply
Re: [LoisC] Where to place zero padding code In reply to
I'm not defining the year month and day fields as "date" fields... just plain old numeric fields.

I searched through the posts on this site and on the unofficial faq looking for a date format that would sort properly and didn't find one. So... I just created separate year month and day fields. I have other applications that pull data from the dbman databases and they need to be able to sort records by date.

The only thing I use dbman for is as a front-end maintain the data.
Quote Reply
Re: [acravens] Where to place zero padding code In reply to
 
I'm not sure exactly what codes you are using to pad the fields but you would put the code in your db.cgi file

sub add_record

after the line:

# Set the userid to the logged in user.
($auth_user_field >= 0) and ($in{$db_cols[$auth_user_field]} = $db_userid);


This thread seems to provide the best solution:

Topic: how to do this in perl
long327 15-May-2002
http://gossamer-threads.com/p/196442

$var = sprintf(''%02d'',$var); # Will pad to 2 places with zero's.


Give this a try and see if it works in sub add_record:

$month = sprintf(''%02d'',$var);
$in{'month'} = $month;

then the same for day and year. I'm not a programmer, but give it a try and see if will work for you.

Unoffical DBMan FAQ

http://creativecomputingweb.com/dbman/index.shtml/
Quote Reply
Re: [LoisC] Where to place zero padding code In reply to
Works fine... thank you. Now I just have to add the same code snippet to modify_record and any other subs where data might be entered.
Quote Reply
Re: [acravens] Where to place zero padding code In reply to
Actually... I think I'm going to take the code out of add_record and modify_record and put it at the top of the validate_record routine. Basically, I need to add this code to every routine where data might be entered. It seems like every place data might be entered, the validate_record routine would be called. If I put the mod in validate_record, I'd only have to enter it once.

I tried it and it works.
Quote Reply
Re: [acravens] Where to place zero padding code In reply to
Yep. You are correct.
Quote Reply
Re: [acravens] Where to place zero padding code In reply to
Great I'm so glad you got it to work :)

Unoffical DBMan FAQ

http://creativecomputingweb.com/dbman/index.shtml/