Es gibt ein paar Möglichkeiten, dies nähern konnte, aber das war das erste, was ich versuchen würde, (Code nicht getestet).
Angenommen, dies ist CodeIgniter 3
1) Erstellen der folgenden Konfigurationsdatei auf /application/config/validation/validate.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
// CI not normally available in config files,
// but we need it to load and use the model
$CI =& get_instance();
// Load the external model for validation of dates.
// You will create a model at /application/models/validation/time_logic.php
$CI->load->model('validation/time_logic');
$config['validation_rules'] = [
[
'field' => 'event_start_time',
'label' => 'Starttid',
'rules' => 'trim|required|strip_tags'
],
[
'field' => 'event_end_time',
'label' => 'Sluttid',
'rules' => [
'trim',
'required',
'strip_tags'
[
'_ensure_start_time_end_time_logic',
function($str) use ($CI) {
return $CI->time_logic->_ensure_start_time_end_time_logic($str);
}
]
]
]
];
2) Erstellen des Validierungsmodell an/application/Modelle/Validierung/time_logic.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Time_logic extends CI_Model {
public function __construct()
{
parent::__construct();
}
public function _ensure_start_time_end_time_logic($str)
{
// Making an assumption that your posted dates are a format that can be read by DateTime
$startTime = new DateTime($this->input->post('event_start_time'));
$endTime = new DateTime($str);
// Start time must be before end time
if($startTime >= $endTime)
{
$this->form_validation->set_message(
'_ensure_start_time_end_time_logic',
'Start time must occur before end time'
);
return FALSE;
}
return $str;
}
}
3) In Ihrem Controller, Modell, oder wo auch immer es ist, dass Sie die Post, Last validieren und die Validierungsregeln anwenden, statt spec wenn ich ihnen sagte, wie du es gemacht hast.
$this->config->load('validation/validate');
$this->form_validation->set_rules(config_item('validation_rules'));