The problem I had was that each new record that was being added, would
then go to a step to add several related records in a different table.
So even though I could check the datestamps against each other, the
flash messages were getting lost because the application immediately
redirects and starts using another model. In my case these are lab
specimens that are divided into aliquots.

The user begins by creating a new record for a specimen. There are
three date fields. The draw date, the receive date and the storage
date. The record is added and the user is prompted for how many
aliquots will be created from the specimen. They enter a number and a
corresponding number of aliquot records are created. In leaving the
specimen model and going to the aliquot model, I was losing flash
messages before anyone saw the specimen record again.

I decided (for now) to check the dates in the view, so I created a
simple helper to format any dates that are before the 'created' date
in red text. (I should add that all of my dates are being stored as
Unix timestamps.) I created this helper:

<?php
/* /app/views/helpers/date.php */

class DateHelper extends AppHelper {

    function future($datestamp, $created, $format) {

        if ($datestamp > 0) {

                if ($datestamp > $created) {

                        $thedate = '<span class="redtext">' . date($format,
$datestamp) . ' This record was created on ' . date('m/d/Y',
$created) . ' at ' . date('G:i', $created) . '. Specimens cannot be
future dated. </span>';

                } else {

                        $thedate = date($format, $datestamp);

                }

                return $this->output($thedate);

        } else {

                $thedate = '';
                return $this->output($thedate);

        }

    }
}

?>

Since my helper is called 'DateHelper', add 'Date' to the helper var
in the AppController.

Then I can just send any date as:

$date->future($date, $created, $format)

and the date is sent back formatted like $format (the same as the php
date() function). If the date in question is later than the date the
record was created, it's formatted with the class 'redtext' so it
stands out.

There's probably a better way to do all of this utilizing AJAX, but
I'm not there yet.

For now, my helper is handy and I can reuse it in the other views that
use dates.

One big reason I did it this was as that I wanted the record to be
saved, but send also send a message to the user that something is
wrong with the date. I'll refine this as I develop the application
further, but for now this works.

On Sep 16, 12:36 pm, LunarDraco <[EMAIL PROTECTED]> wrote:
> Here is a sample that I use, Its not exactly what your looking for,
> but a little tweaking will get you where you need to be:
>
> var $validate = array(
>    'expires' =>
> array('rule'=>array('date','mdy'),'rule'=>array('validateFutureDate'),'on'=>'create')
> );
>
>         function validateFutureDate($value) {
>         $valid = false;
>
>         $timestamp = strtotime(str_replace('-','/',
> $value['expires']));
>                 $t = time()+(86400*34);
>                 if($timestamp >= $t)
>                         $valid = true;
>
>         return $valid;
>     }
>
> On Sep 16, 3:08 am, RichardAtHome <[EMAIL PROTECTED]> wrote:
>
> > > I was afraid that might be the case
>
> > It's really not difficult :-)
>
> > And its a very powerful solution once you understand how it works.
>
> > On Sep 16, 1:46 am, Tony Thomas <[EMAIL PROTECTED]> wrote:
>
> > > Thanks. I was afraid that might be the case.
>
> > > On Sep 15, 7:42 pm, "Dardo Sordi Bogado" <[EMAIL PROTECTED]> wrote:
>
> > > > You can't use functions i.e date() at class definitions, create a
> > > > custom method in the model and use that as a validation rule.
>
> > > > On Mon, Sep 15, 2008 at 9:34 PM, Tony Thomas <[EMAIL PROTECTED]> wrote:
>
> > > > > According to the documentation that's the proper way to define the
> > > > > array.
>
> > > > >http://book.cakephp.org/view/139/comparison
>
> > > > > It just amounts to defining an array without defining the keys if I'm
> > > > > not mistaken. I'll take a closer look at the syntax in the morning
> > > > > when my mind is a little fresher. I'll post something here if
> > > > > something jumps out at me.
>
> > > > > On Sep 15, 4:20 pm, 703designs <[EMAIL PROTECTED]> wrote:
> > > > >> I noticed that the way you coded your array would definitely throw a
> > > > >> syntax error.
>
> > > > >> How about
> > > > >> 'rule' => array(
> > > > >>     'comparison' => date()
> > > > >> ),
>
> > > > >> I'll try this on my Cake installation later to check, as I haven't
> > > > >> used the 'comparison' validator. I just know that your array looks
> > > > >> botched.
>
> > > > >> On Sep 15, 5:14 pm, 703designs <[EMAIL PROTECTED]> wrote:
>
> > > > >> > date() should just return the formatted date string. What sort of
> > > > >> > errors are you seeing?
>
> > > > >> > On Sep 15, 3:51 pm, Tony Thomas <[EMAIL PROTECTED]> wrote:
>
> > > > >> > > Anyone have a proven method for validating submitted dates to 
> > > > >> > > make
> > > > >> > > sure they're not in the future?
>
> > > > >> > > The closest I can get is:
>
> > > > >> > > 'date_field' => array('rule' => array('comparison',
> > > > >> > >                                                         '>=',
> > > > >> > >                                                         date()),
> > > > >> > >                                                         
> > > > >> > > 'message' => 'Dates cannot be in the future.')
>
> > > > >> > > But date() is throwing a syntax error.
>
> > > > >> > > None of the core time testing helpers do this.
>
> > > > >> > > I should note that in the controller, before save, the date 
> > > > >> > > string is
> > > > >> > > converted into a Unix timestamp.
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to