setRegisterInAr" /> setRegisterInAr" /> setRegisterInAr"/>

How to add an error message based on two field values using Zend_Validate_Db_NoRecordExists

39 Views Asked by At

I am having a page with two fields "Start Time" and "End Time".

$start_time = new Zend_Form_Element_Select("start_time");
$start_time->setRegisterInArrayValidator(false);  
$start_time->addValidator(
    new Zend_Validate_Db_NoRecordExists(
        array(
            'table'=>'main_times',
            'field'=>'start_time',
            'exclude'=> 'id != "' . Zend_Controller_Front::getInstance()->getRequest()->getParam('id') . '" and isactive = 1',    
        )
    )
);
$start_time->getValidator('Db_NoRecordExists')->setMessage('Start Time already exists.'); 
$end_time = new Zend_Form_Element_Select("end_time");
$end_time->addMultiOptions(array(''=>'Select End Time'));
$end_time->setRegisterInArrayValidator(false);
$end_time->addValidator(
    new Zend_Validate_Db_NoRecordExists(
        array(
            'table'=>'main_times',
            'field'=>'end_time',
            'exclude'=>'id != "' . Zend_Controller_Front::getInstance()->getRequest()->getParam('id') . '" and isactive = 1',    
        )
    )
);
$end_time->getValidator('Db_NoRecordExists')->setMessage('End Time already exists.'); 

Here, you can see, I am validating "Start Time" and "End Time" field separately.

Now, I want to show an error message like "Time Slot already exists", if I try to create a record with same Start Time and End Time.

Eg:

First Time: I created a record with "Start Time" as "02:00PM" and "End Time" as "03:00PM". Now the Time Slot is 02:00PM - 03:00PM

Second Time: I created a record with "Start Time" as "02:00PM" and "End Time" as "05:00PM". Now the Time Slot is 02:00PM - 05:00PM

Third Time: I created a record with "Start Time" as "02:00PM" and "End Time" as "03:00PM". Now the Time Slot is 02:00PM - 03:00PM. Now, I should get the error message like "Time Slot already exists". And the form should not get submitted.

How can I implement this in Zend using Zend_Validate_Db_NoRecordExists (or) there is any other way to do this?

0

There are 0 best solutions below