Date field validation/hook

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
Yilmaz
Posts: 25
Joined: 2013-01-24 16:58

Date field validation/hook

Post by Yilmaz » 2013-02-16 16:43

Dear forum,

I hope I am here in the right topic. Following issue:

- In my table I have 2 additional date fields "start_date" and "end_date".
- Start_date has the default value "now/today".
- The users should enter in the end_date only date values which are greater than the value in the start_date.
- If this is not the case, a java script alert should appear accordingly.

How can I handle this issue on a easiest way?

--> Which hook?
--> Where to code the java script validation?

Thanks in advance
Yilmaz

User avatar
a.gneady
Site Admin
Posts: 1107
Joined: 2012-09-27 14:46
Contact:

Re: Date field validation/hook

Post by a.gneady » 2013-02-16 22:26

One of the generated files is named "common.js.php" ... In this file, you can find a function named "tablename_validateData()" (where tablename is the name of the concerned table) ... Usually, the code in this function would look like this:

Code: Select all

return true;
Returning true means that validation is OK and the form can be submitted ... You can replace the above and add a javascript validation rule similar to this:

Code: Select all

if($('FieldName').value==''){
    /* display the error message to the user in a non-obtrusive manner using a modalbox */
    Modalbox.show(
        '<div class="Error" style="width: 90%; margin: 0;">You can\'t leave FieldName empty!</div>',
        {
            title: "FieldName error!",
            afterHide: function(){ $('FieldName').focus(); }
        }
    );
    
    /* returning false stops form submission */
    return false;
};

/* In case of no error, return true to submit the form */
return true;
:idea: Calendar plugin: Organize your data into beautiful calendars.
:arrow: Mass Update plugin: Update multiple records at once and improve your workflow efficiency.
:arrow: Summary Reports plugin: Build powerful reports and charts for your AppGini apps.
:arrow: Search Page Maker: Compose user-friendly yet powerful search pages for your AppGini apps.

Yilmaz
Posts: 25
Joined: 2013-01-24 16:58

Re: Date field validation/hook

Post by Yilmaz » 2013-02-18 11:35

Dear Ahmad,

many thanks for you hint. It is the right way, but unfortunately I have the following general problem with calling the date field as datepicker:

- I defined 2 variables in common.js.php: "startdate" and "enddate" and assigned them to the values accordingly ($('startsp').value; and $('endsp').value;)
- At first my if condition is due to the equality of those variables
- In case of equal I wanted to see which value is for example assigned to the variable "enddate" (alert (enddate))
- It was only the value year "2013" and not all the other values which are stored in the database as date (see below):
- My idea is to compare the "values" both of the dates (like the php-function strtotime) and alert a message depending of the if condition
- How can I assign the date values (2013-01-16) to the javascript variables?

Thanks in advance
Yilmaz

mysql> select district_id, startsp, endsp from ap_main;
+-------------+------------+------------+
| district_id | startsp | endsp |
+-------------+------------+------------+
| mstahl | 2013-01-16 | 2013-01-30 |
| mreiners | 2013-01-26 | 2013-02-02 |


My code in common.js.php is:

function ap_main_validateData(){

var startdate = $('startsp').value;
var enddate = $('endsp').value;

if(enddate == startdate){alert(enddate); Modalbox.show('<div class="Error" style="width: 90%; margin: 0;"><?php echo addslashes($Translation['datum kleiner']); ?></div>', { title: "<?php echo addslashes($Translation['error:']); ?>Enddatum ", afterHide: function(){ $('customernr_id').focus(); } }); return false; };

Yilmaz
Posts: 25
Joined: 2013-01-24 16:58

Re: Date field validation/hook

Post by Yilmaz » 2013-02-19 10:07

Any ideas?

Johnk
AppGini Super Hero
AppGini Super Hero
Posts: 65
Joined: 2013-01-09 03:47
Location: Cairns, Australia

Re: Date field validation/hook

Post by Johnk » 2013-02-19 13:58

You may need to be a bit patient Yilmaz, it's a new board with only a few experienced members. Ahmad is busy writing a new update and like yourself I'm also waiting for several posts to be answered. I guess it will all come together sooner than later :) Well I'm hoping it will!

John

Yilmaz
Posts: 25
Joined: 2013-01-24 16:58

Re: Date field validation/hook

Post by Yilmaz » 2013-02-19 14:10

Yes, of course you are right John. Sorry for my hurry, certainly I will wait.

Yilmaz

Yilmaz
Posts: 25
Joined: 2013-01-24 16:58

Re: Date field validation/hook - SOLVED

Post by Yilmaz » 2013-02-19 16:55

Dear all,

I found in the meanwhile an other way to validate the date fields instead of hooking in the common.js.php:

- Edit tablename_dml.php
- Find the section for tablename_dml.php and replace the condition if empty through the date field comparision as below

Code: Select all

function ap_main_insert(){
...

        //if($data['endsp']== ''){
        if($data['endsp'] < $data['startsp']){
                echo StyleSheet() . "\n\n<div class=\"Error\">" . $Translation['error:'] . "<br />" . $Translation['datum kleiner'] . '<br /><br />';
                echo '<a href="" onclick="history.go(-1); return false;">'.$Translation['< back'].'</a></div>';
                exit;
        }

...

        //Comparision of a date field with "today"
        $heute = date("Y-n-d");
        //if($data['date_latest']== ''){
        if($data['date_latest'] < $heute ){
                echo StyleSheet() . "\n\n<div class=\"Error\">" . $Translation['error:'] . "<br />" . $Translation['Wunschdatum kleiner heute'] . '<br /><br />';
                echo '<a href="" onclick="history.go(-1); return false;">'.$Translation['< back'].'</a></div>';
                exit;
        }
- Do the same for update function in the same _dml.php right down.

The only "cosmetic" disadvantage is that you don't have an animated error message field like the output from common.js.php.

Regards,

Yilmaz

KSan
AppGini Super Hero
AppGini Super Hero
Posts: 252
Joined: 2013-01-08 20:17

Re: Date field validation/hook

Post by KSan » 2013-02-19 22:43

Thanks much for sharing your solution. This is most useful.

MikeB
Posts: 18
Joined: 2013-01-08 19:18

Re: Date field validation/hook

Post by MikeB » 2013-02-20 18:41

Going back to the common.js.php solution, is there a reason why this code doesn't prevent saving a record with the EndDate earlier than the StartDate (both fields in the ParkingPass table)? I'm kind of new to PHP coding, so please excuse if I've made a stupid mistake.

Code: Select all

function ParkingPass_validateData(){
	if($('ResidentID').value==''){ Modalbox.show('<div class="Error" style="width: 90%; margin: 0;"><?php echo addslashes($Translation['field not null']); ?></div>', { title: "<?php echo addslashes($Translation['error:']); ?> Resident ID", afterHide: function(){ $('ResidentID').focus(); } }); return false; };
	if($('ReasonForPass').value==''){ Modalbox.show('<div class="Error" style="width: 90%; margin: 0;"><?php echo addslashes($Translation['field not null']); ?></div>', { title: "<?php echo addslashes($Translation['error:']); ?> Reason For Pass", afterHide: function(){ $('ReasonForPass').focus(); } }); return false; };

/* New Date Validation Code */

$TestDate = $('StartDate').value;

if($('EndDate').value < '$TestDate'){
    /* display the error message to the user in a non-obtrusive manner using a modalbox */
    Modalbox.show(
        '<div class="Error" style="width: 90%; margin: 0;">End Date can\'t be earlier than Start Date.</div>',
        {
            title: "End Date error!",
            afterHide: function(){ $('EndDate').focus(); }
        }
    );

    /* returning false stops form submission */
    return false;
};

/* In case of no error, return true to submit the form */
	return true;
}

pgkounelas
Posts: 10
Joined: 2013-01-30 14:55
Location: Larissa, Hellas

Re: Date field validation/hook

Post by pgkounelas » 2013-02-21 17:22

Another way to validate dates and text is with magic files.
I think in this should help us a little Ahmad.
to validate numbers successfully use this code.

Code: Select all

document.observe('dom:loaded', function() {
    $('score').observe('change', function() {
        if(isNaN($F('score')) || $F('score') > 100 || $F('score') < 0){
            alert('Score must be between 0 and 100!');
            $('score').focus();
        }
    });
});
Ahmad, how can I do the same with dates or text?
Α little help is needed, we stuck.

User avatar
a.gneady
Site Admin
Posts: 1107
Joined: 2012-09-27 14:46
Contact:

Re: Date field validation/hook

Post by a.gneady » 2013-02-23 07:06

When validating dates in javascript, you need to apply the getTime() function first. Please refer to the sample code at http://stackoverflow.com/questions/6948 ... comparison
:idea: Calendar plugin: Organize your data into beautiful calendars.
:arrow: Mass Update plugin: Update multiple records at once and improve your workflow efficiency.
:arrow: Summary Reports plugin: Build powerful reports and charts for your AppGini apps.
:arrow: Search Page Maker: Compose user-friendly yet powerful search pages for your AppGini apps.

Post Reply