How to add date validation when creating new record

A place for users and developers of the Xataface to discuss and receive support.

How to add date validation when creating new record

Postby rleyba » Tue Nov 20, 2012 5:54 am

Hi Steve,

Just a quick one. I would need to do some field validation in my tables and I was looking here --> http://xataface.com/documentation/tutor ... validation. My request is a bit different

I have two DATE_TIME fields in one of my tables:START_DATE_TIME,END_DATE_TIME which the user types in, when keying in the form.

However, I need more control over the date they can type, such that:
START_DATE_TIME --> Cannot be earlier than current system date/time and will popup "Change cannot be earlier than today" if they try to type in early date and will not save the record.
Also,
END_DATE_TIME =--> Cannot be earlier than the START_DATE_TIME field and will popup "End Date/Time cannot be earlier than start time" and will not save the record if this is the case.

*I figured it will be a combination of delegate classes and before save and after save techniques, since in the case of the END_DATE_TIME validation, it won't know the value of START_DATE_TIME until AFTER this fields has been evaluated first.

* Would appreciate if you could just direct me to a working example or some code snippets to get this to work.

thanks Steve
rleyba
 
Posts: 53
Joined: Sat Dec 04, 2010 3:50 pm

Re: How to add date validation when creating new record

Postby rleyba » Tue Nov 20, 2012 5:59 am

I checked this also --> http://xataface.com/documentation/how-t ... validation.

Haven't found a date validation forum post yet. It is working with the hooks to get the system date/time and the php part that I am struggling with.

Thanks.
rleyba
 
Posts: 53
Joined: Sat Dec 04, 2010 3:50 pm

Re: How to add date validation when creating new record

Postby shannah » Tue Nov 20, 2012 9:17 am

Date validation shouldn't be any different than any other validation. You can either use custom validators or you can place validation inside a beforeSave() trigger (e.g. at http://xataface.com/documentation/tutor ... d/triggers).

e.g.
Code: Select all
function start_date__validate(&$record, $value, &$params){
   if ( strtotime($record->strval('start_date')) < time() ){
      $params['message'] = 'Start time too soon.';
      return false;
   }
   return true;
}

function end_date__validate(&$record, $value, &$params){
    $startTime = strtotime($record->strval('start_date'));
    $endTime = strtotime($record->strval('end_date'));
    if ( $endTime < $startTime ){
      $params['message'] = 'End time before start time.';
      return false;
   }
   return true;
}
--
Steve Hannah
@shannah78 (on twitter)
sjhannah.com blog
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: How to add date validation when creating new record

Postby rleyba » Thu Nov 22, 2012 5:23 am

Thanks Steve! I'll try this out.
rleyba
 
Posts: 53
Joined: Sat Dec 04, 2010 3:50 pm

Re: How to add date validation when creating new record

Postby rleyba » Thu Nov 22, 2012 9:20 pm

Hi Steve, I am getting some errors and it could be related to the field type in my mysql database.

It shows two lines of errors with the same message:
Notice: Array to string conversion in /var/www/html/xataface-1.2.6/Dataface/QueryBuilder.php on line 502
Notice: Array to string conversion in /var/www/html/xataface-1.2.6/Dataface/QueryBuilder.php on line 502

Notice my field name below.....a bit different from the one above, but it shouldn't matter. In my setup, ImplementationStartDate is datetime format type in my mysql database. It is not a string. So what I did was modify your function above to remove the string conversion. But it still keeps complaining about the string conversion. In other words, whethere I used your example above or my modified conversion below, it kept showing the conversion error. I wonder if I am modifying the wrong thing.

My syntax is as below:


Code: Select all
    function ImplementationStartDate__validate(&$record, $value, &$params){
       if ( $record->val('ImplementationStartDate') < time() ){
          $params['message'] = 'Implementation start date cannot be in the past. Ensure format is YYYY-MM-DD HH:MM:SS.';
          return false;
       }
       return true;
    }

}

Thanks Steve.
rleyba
 
Posts: 53
Joined: Sat Dec 04, 2010 3:50 pm

Re: How to add date validation when creating new record

Postby shannah » Fri Nov 30, 2012 4:10 am

$record->val('ImplementationStartDate') returns an array.

Use strtotime( $record->strval('ImplementationStartDate')) instead to get the timestamp.

-Steve
--
Steve Hannah
@shannah78 (on twitter)
sjhannah.com blog
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: How to add date validation when creating new record

Postby rleyba » Sun Jan 06, 2013 1:33 am

Hi Steve,

I'm still getting some exactly the same error in my corrected function below:

Code: Select all
function ImplementationStartDate__validate(&$record, $value, &$params){
       if (strtotime ( $record->strval('ImplementationStartDate')) < time() ){
          $params['message'] = 'Implementation start date cannot be in the past. Ensure format is YYYY-MM-DD HH:MM:SS.';
          return false;
       }
       return true;
    }


The new syntax didn't seem to have any effect. It still shows two lines of errors with the same message:
Notice: Array to string conversion in /var/www/html/xataface-1.2.6/Dataface/QueryBuilder.php on line 502
Notice: Array to string conversion in /var/www/html/xataface-1.2.6/Dataface/QueryBuilder.php on line 502

Anything else you think I should check?

Thanks very much.
rleyba
 
Posts: 53
Joined: Sat Dec 04, 2010 3:50 pm

Re: How to add date validation when creating new record

Postby shannah » Sun Jan 06, 2013 10:46 am

Do you still get this error if you remove the validation function entirely?
--
Steve Hannah
@shannah78 (on twitter)
sjhannah.com blog
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: How to add date validation when creating new record

Postby shannah » Sun Jan 06, 2013 10:48 am

Also, you should update to at least Xataface 1.2.8, as it has some critical security patches.
--
Steve Hannah
@shannah78 (on twitter)
sjhannah.com blog
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: How to add date validation when creating new record

Postby rleyba » Mon Jan 07, 2013 4:48 pm

Hi Steve,

Once I remove the validation function, everything works perfectly fine.

*Regarding the upgrade, I will do some planning soon to have it scheduled.

Thanks and regards.
rleyba
 
Posts: 53
Joined: Sat Dec 04, 2010 3:50 pm


Return to Xataface Users

Who is online

Users browsing this forum: No registered users and 2 guests

Powered by Dataface
© 2005-2007 Steve Hannah All rights reserved