manamalkav
Založený: 11.11.2003
Príspevky: 8
Bydlisko: Florida, USA
Zaslal: 2003-11-11 17:12
Návrat hore
Odpovedať s citátom
|
This is how you can perform simple date validation using 'before' triggers:
1. Set the invocation to the trigger file
//set the trigger on this table
$opts['triggers']['update']['before']= 'tb.inc';
$opts['triggers']['insert']['before']= 'tb.inc';
2. Contents of trigger file (tb.inc)
<?php
include_once("date_validate.inc");
$field_name = 'my_date_field';
$valid_date = validate_date($newvals[$field_name]);
if ($valid_date)
return true;
return false;
}
?>
3. Contents of date_validate.inc:
<?php
function validate_date($input_date)
{
//this routine accepts a date in the YYYY/MM/DD format
//and checks if valid
$get_date = explode("/",$input_date);
$year = $get_date[0];
$month = $get_date[1];
$day = $get_date[2];
if(!checkdate($month, $day, $year))
{
echo "<h3> $input_date is not a valid date! </h3>";
return "false";
}
return true;
}
?>
Ofcourse, this idea can be extended to validate any field prior to updating the table (and it works like a charm!)
I hope this helps.
|
manamalkav
Založený: 11.11.2003
Príspevky: 8
Bydlisko: Florida, USA
Zaslal: 2003-11-12 16:35
Návrat hore
Odpovedať s citátom
|
Doug - I had a look at your work at http://phpvs.com/misc/forum1.php
- really AWESOME ! I have been using a similar validation scheme but this tool will simplify life so much...you are the man!
I am just thinking out loud here....
1. The front end to this 'validation script generator' can be made more functional by
accepting field details like whether email/ whether SSN/ whether telephone/whether ZIP
etc.
2. In response to Angel's original query of accepting user input in MM/DD/YYYY format, validating
it and storing it in the DB as YYYY-MM-DD. I thought changing $newvals[date field] in the
trigger after the validation would do the trick.. But, I was wrong !!
On closer insepction of the code, I realised that the insert/update query is fully built PRIOR
to the trigger invocation ... And, understandably so.. Because the 'before' query ($oldvals) may be required in the trigger.
Therefore, making a change to the $newvals array in the trigger does not alter what goes into the table!
I was working on a efficient solution to do this without repeatitive code.
Ideas ??
|
michal
Založený: 17.06.2003
Príspevky: 537
Bydlisko: Slovakia
Zaslal: 2003-11-12 19:10
Návrat hore
Odpovedať s citátom
|
to view errors and add form after an error encoutered:
something like this:
| Kód: |
if ($error) {
$this->operation = "Add";
$this->fdd[$k]['default'] = $_POST['col_name'];
$this->fdd[$k]['help'] = $error;
}
return false;
|
in the before trigger where col_name is columns name which happens to be the $k-th.
i haven't tested, but should work :)
Naposledy upravil michal dňa 2003-11-12 20:56, celkom upravené 2 krát
|
michal
Založený: 17.06.2003
Príspevky: 537
Bydlisko: Slovakia
Zaslal: 2003-11-12 19:13
Návrat hore
Odpovedať s citátom
|
also for date validation i found this script somewhere. it looks quite handy
| Kód: |
// This function is made to check date-input from an HTML-form
// and bring it to the YYYY-MM-DD format that it can be
// insertet in a Date-Field of a MySQL-Table.
//
// Input-Formats can be:
// "d.m.y" or "y.m.d" or "d.m" or "d"
// if year or month and year are missing, these values are
// taken from the current date.
//
// When the result-String is empty, the date-input was wrong.
//
// Usage: $date_ok = input2date($date_from_form_field);
//
function input2date($idate='')
{
$token="-,./ ";
$p1 = strtok($idate,$token);
$p2 = strtok($token);
$p3 = strtok($token);
$p4 = strtok($token);
//echo $p1." ".$p2." ".$p3;
$date="";
$y=""; $m=""; $d="";
// check 'd.m.y'
if (($p1>0 && $p1<32) &&
($p2>0 && $p2<13) &&
($p3>0))
{
$y=$p3;
$m=$p2;
$d=$p1;
//echo $y;
}
// check 'y.m.d'
if ($y == "" &&
($p1>32) &&
($p2>0 && $p2<13) &&
($p3>0 && $p3<32))
{
$y=$p1;
$m=$p2;
$d=$p3;
}
// check 'd.m'
if ($y == "" &&
($p3=="") &&
($p2>0 && $p2<13) &&
($p1>0 && $p1<32))
{
$y=date("Y");
$m=$p2;
$d=$p1;
}
// check 'd'
if ($y == "" &&
($p3=="") &&
($p2=="") &&
($p1>0 && $p1<32))
{
$y=date("Y");
$m=date("m");
$d=$p1;
}
//echo $y." ".$m." ".$d;
// add 1900 or 2000 to year
if ($y!="" && $y<=99)
{
if ($y>=70) $y = $y + 1900;
if ($y<70) $y = $y + 2000;
//echo "ceckujem year <br>";
}
if ($y!="")
{
if (checkdate($m, $d, $y))
$date="$y-$m-$d";
} else { // error mesage here, should be exported to your needs
$error = 'insert proper date';
}
return $date;
}
}
|
|
manamalkav
Založený: 11.11.2003
Príspevky: 8
Bydlisko: Florida, USA
Zaslal: 2003-11-12 20:15
Návrat hore
Odpovedať s citátom
|
The generated validation script needs a minor modification..
$newvals[$FIELD] to be replaced with
$newvals['FIELD']
| manamalkav Napísal: |
Doug - I had a look at your work at http://phpvs.com/misc/forum1.php
- really AWESOME ! I have been using a similar validation scheme but this tool will simplify life so much...you are the man!
I am just thinking out loud here....
1. The front end to this 'validation script generator' can be made more functional by
accepting field details like whether email/ whether SSN/ whether telephone/whether ZIP
etc.
2. In response to Angel's original query of accepting user input in MM/DD/YYYY format, validating
it and storing it in the DB as YYYY-MM-DD. I thought changing $newvals[date field] in the
trigger after the validation would do the trick.. But, I was wrong !!
On closer insepction of the code, I realised that the insert/update query is fully built PRIOR
to the trigger invocation ... And, understandably so.. Because the 'before' query ($oldvals) may be required in the trigger.
Therefore, making a change to the $newvals array in the trigger does not alter what goes into the table!
I was working on a efficient solution to do this without repeatitive code.
Ideas ?? |
|
manamalkav
Založený: 11.11.2003
Príspevky: 8
Bydlisko: Florida, USA
Zaslal: 2003-11-14 20:50
Návrat hore
Odpovedať s citátom
|
Could you explain this a little more (and include code if possible) ?
What I am trying to achieve is to redisplay the values keyed in by a user on the 'Add' form if 'Trigger Insert Before' returns errors.
| michal Napísal: |
to view errors and add form after an error encoutered:
something like this:
| Kód: |
if ($error) {
$this->operation = "Add";
$this->fdd[$k]['default'] = $_POST['col_name'];
$this->fdd[$k]['help'] = $error;
}
return false;
|
in the before trigger where col_name is columns name which happens to be the $k-th.
i haven't tested, but should work :) |
|
manamalkav
Založený: 11.11.2003
Príspevky: 8
Bydlisko: Florida, USA
Zaslal: 2003-11-14 21:38
Návrat hore
Odpovedať s citátom
|
Could you explain this a little more (and include code if possible) ?
What I am trying to achieve is to redisplay the values keyed in by a user on the 'Add' form if 'Trigger Insert Before' returns errors.
| michal Napísal: |
to view errors and add form after an error encoutered:
something like this:
| Kód: |
if ($error) {
$this->operation = "Add";
$this->fdd[$k]['default'] = $_POST['col_name'];
$this->fdd[$k]['help'] = $error;
}
return false;
|
in the before trigger where col_name is columns name which happens to be the $k-th.
i haven't tested, but should work :) |
|
hbernard
Založený: 23.03.2003
Príspevky: 159
Bydlisko: FRANCE
Zaslal: 2003-11-14 21:54
Návrat hore
Odpovedať s citátom
|
| manamalkav Napísal: |
Ideas ?? |
I currently make sql updating after insert, based on the mysql_insert_id (I dont care what initially is inserted in the table)
Dirty, but it just works....
Another way can be to direcly modify the query from the trigger file, using a regexp, no ?
|
michal
Založený: 17.06.2003
Príspevky: 537
Bydlisko: Slovakia
Zaslal: 2003-11-15 00:30
Návrat hore
Odpovedať s citátom
|
let us assume you have your error array in form
| Kód: |
error[] = array (
'col_name' => 'email',
'k' => 4,
'old_values' => 'bad@email',
'error_message' => 'error 555: you have bad email address'
);
|
this error array is somehow filled in you trigger.
where k is the order of col_name
col_name == $this->fds[k];
old_values is bad value of appropriate field
error_message is what you want to tell your client
something like this:
| Kód: |
foreach($error as $key => $er) {
$this->fdd[$er['k']]['help'] = $er['error_message']; // shows error message
$this->fdd[$er['k']]['default'] = $er['old_values']; // displays bad input
}
$this->operation = 'Add'; // langage specific
return false; // stops the insert proccess
|
at the end of the before trigger should do the job. i haven't tested as a whole, but all parts seem to work seperately.
|
michal
Založený: 17.06.2003
Príspevky: 537
Bydlisko: Slovakia
Zaslal: 2003-11-15 14:03
Návrat hore
Odpovedať s citátom
|
after some testing, the code that somehow works for me is this in case an error encoutered:
| Kód: |
// filling user input in all columns first
foreach ($newvals as $key => $value) {
$this->fdd[$key]['default'] = $value;
}
// error handling
foreach($error as $key => $er) {
$this->fdd[$er['col_name']]['help'] = $er['error_message']; // shows error message
$this->fdd[$er['col_name']]['default'] = $er['old_values']; // displays bad input
}
// emulating moreadd/more button
$this->operation = $this->labels['Add']; // language unspecific
$this->recreate_fdd();
$this->recreate_displayed();
// stopping the insert proccess
return false;
|
hope this helps
|
|
Odoslať novú tému
Odpovedať na tému
|