<![CDATA[ Validating a TV against another TV before resource is saved. - My Forums]]> https://forums.modx.com/thread/?thread=104899 <![CDATA[Validating a TV against another TV before resource is saved.]]> https://forums.modx.com/thread/104899/validating-a-tv-against-another-tv-before-resource-is-saved#dis-post-564046 sean69 Feb 11, 2019, 08:58 PM https://forums.modx.com/thread/104899/validating-a-tv-against-another-tv-before-resource-is-saved#dis-post-564046 <![CDATA[Re: Validating a TV against another TV before resource is saved.]]> https://forums.modx.com/thread/104899/validating-a-tv-against-another-tv-before-resource-is-saved#dis-post-564061
]]>
BobRay Feb 12, 2019, 10:55 PM https://forums.modx.com/thread/104899/validating-a-tv-against-another-tv-before-resource-is-saved#dis-post-564061
<![CDATA[Re: Validating a TV against another TV before resource is saved.]]> https://forums.modx.com/thread/104899/validating-a-tv-against-another-tv-before-resource-is-saved#dis-post-564059 Quote from: BobRay at Feb 12, 2019, 07:08 PM
I think it might be simple as something like this, connected to OnBeforeDocFormSave:

$start = strtotime($resource->getTVValue('start_date'));
$end = strtotime($resource->getTVValue('end_date'));

if ((!empty($start)) && (!empty($end)))
    if ($end < $start) {
        return (array('End date must be later than start date'));
    }
}

return;


It should display the error message, but I've never tested it.

This will check both new and existing resources on save. If you don't want that, you can put something like this at the top:

if ($mode === modSystemEvent::MODE_NEW) {
    return '';
}


You may also want to add a test of $resource->get('parent') or $resource->get('id') at the top to limit it to specific resources.




thanks Bob - ya - it looks good.]]>
sean69 Feb 12, 2019, 07:56 PM https://forums.modx.com/thread/104899/validating-a-tv-against-another-tv-before-resource-is-saved#dis-post-564059
<![CDATA[Re: Validating a TV against another TV before resource is saved.]]> https://forums.modx.com/thread/104899/validating-a-tv-against-another-tv-before-resource-is-saved#dis-post-564056
$start = strtotime($resource->getTVValue('start_date'));
$end = strtotime($resource->getTVValue('end_date'));

if ((!empty($start)) && (!empty($end)))
    if ($end < $start) {
        return (array('End date must be later than start date'));
    }
}

return;


It should display the error message, but I've never tested it.

This will check both new and existing resources on save. If you don't want that, you can put something like this at the top:

if ($mode === modSystemEvent::MODE_NEW) {
    return '';
}


You may also want to add a test of $resource->get('parent') or $resource->get('id') at the top to limit it to specific resources.



]]>
BobRay Feb 12, 2019, 07:08 PM https://forums.modx.com/thread/104899/validating-a-tv-against-another-tv-before-resource-is-saved#dis-post-564056
<![CDATA[Re: Validating a TV against another TV before resource is saved.]]> https://forums.modx.com/thread/104899/validating-a-tv-against-another-tv-before-resource-is-saved#dis-post-564047 muzzstick Feb 12, 2019, 05:44 AM https://forums.modx.com/thread/104899/validating-a-tv-against-another-tv-before-resource-is-saved#dis-post-564047