How to add date validation when creating new record

Archived from the Xataface Users forum.

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 — 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.


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;
}`


rleyba — Thu Nov 22, 2012 5:23 am

Thanks Steve! I’ll try this out.


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.


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


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.


shannah — Sun Jan 06, 2013 10:46 am

Do you still get this error if you remove the validation function entirely?


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.


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.