mirror of
https://github.com/moodle/moodle.git
synced 2025-04-12 20:12:15 +02:00
MDL-44393 tasks: Validate cron fields
This commit is contained in:
parent
eb1dc9fab9
commit
c241edb5cf
@ -53,22 +53,27 @@ class tool_task_edit_scheduled_task_form extends moodleform {
|
||||
$mform->addElement('text', 'minute', get_string('taskscheduleminute', 'tool_task'));
|
||||
$mform->setType('minute', PARAM_RAW);
|
||||
$mform->addHelpButton('minute', 'taskscheduleminute', 'tool_task');
|
||||
$mform->addRule('minute', get_string('required'), 'required');
|
||||
|
||||
$mform->addElement('text', 'hour', get_string('taskschedulehour', 'tool_task'));
|
||||
$mform->setType('hour', PARAM_RAW);
|
||||
$mform->addHelpButton('hour', 'taskschedulehour', 'tool_task');
|
||||
$mform->addRule('hour', get_string('required'), 'required');
|
||||
|
||||
$mform->addElement('text', 'day', get_string('taskscheduleday', 'tool_task'));
|
||||
$mform->setType('day', PARAM_RAW);
|
||||
$mform->addHelpButton('day', 'taskscheduleday', 'tool_task');
|
||||
$mform->addRule('day', get_string('required'), 'required');
|
||||
|
||||
$mform->addElement('text', 'month', get_string('taskschedulemonth', 'tool_task'));
|
||||
$mform->setType('month', PARAM_RAW);
|
||||
$mform->addHelpButton('month', 'taskschedulemonth', 'tool_task');
|
||||
$mform->addRule('month', get_string('required'), 'required');
|
||||
|
||||
$mform->addElement('text', 'dayofweek', get_string('taskscheduledayofweek', 'tool_task'));
|
||||
$mform->setType('dayofweek', PARAM_RAW);
|
||||
$mform->addHelpButton('dayofweek', 'taskscheduledayofweek', 'tool_task');
|
||||
$mform->addRule('dayofweek', get_string('required'), 'required');
|
||||
|
||||
$mform->addElement('advcheckbox', 'disabled', get_string('disabled', 'tool_task'));
|
||||
$mform->addHelpButton('disabled', 'disabled', 'tool_task');
|
||||
@ -92,5 +97,63 @@ class tool_task_edit_scheduled_task_form extends moodleform {
|
||||
// Do not use defaults for existing values, the set_data() is the correct way.
|
||||
$this->set_data(\core\task\manager::record_from_scheduled_task($task));
|
||||
}
|
||||
|
||||
/**
|
||||
* Custom validations.
|
||||
*
|
||||
* @param array $data
|
||||
* @param array $files
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function validation($data, $files) {
|
||||
$error = parent::validation($data, $files);
|
||||
$fields = array('minute', 'hour', 'day', 'month', 'dayofweek');
|
||||
foreach ($fields as $field) {
|
||||
if (!self::validate_fields($field, $data[$field])) {
|
||||
$error[$field] = get_string('invaliddata', 'core_error');
|
||||
}
|
||||
}
|
||||
return $error;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function that validates the submitted data.
|
||||
*
|
||||
* Explanation of the regex:-
|
||||
*
|
||||
* \A\*\z - matches *
|
||||
* \A[0-5]?[0-9]\z - matches entries like 23
|
||||
* \A\*\/[0-5]?[0-9]\z - matches entries like * / 5
|
||||
* \A[0-5]?[0-9](,[0-5]?[0-9])*\z - matches entries like 1,2,3
|
||||
* \A[0-5]?[0-9]-[0-5]?[0-9]\z - matches entries like 2-10
|
||||
*
|
||||
* @param string $field field to validate
|
||||
* @param string $value value
|
||||
*
|
||||
* @return bool true if validation passes, false other wise.
|
||||
*/
|
||||
public static function validate_fields($field, $value) {
|
||||
switch ($field) {
|
||||
case 'minute' :
|
||||
case 'hour' :
|
||||
$regex = "/\A\*\z|\A[0-5]?[0-9]\z|\A\*\/[0-5]?[0-9]\z|\A[0-5]?[0-9](,[0-5]?[0-9])*\z|\A[0-5]?[0-9]-[0-5]?[0-9]\z/";
|
||||
break;
|
||||
case 'day':
|
||||
$regex = "/\A\*\z|\A([1-2]?[0-9]|3[0-1])\z|\A\*\/([1-2]?[0-9]|3[0-1])\z|";
|
||||
$regex .= "\A([1-2]?[0-9]|3[0-1])(,([1-2]?[0-9]|3[0-1]))*\z|\A([1-2]?[0-9]|3[0-1])-([1-2]?[0-9]|3[0-1])\z/";
|
||||
break;
|
||||
case 'month':
|
||||
$regex = "/\A\*\z|\A([0-9]|1[0-2])\z|\A\*\/([0-9]|1[0-2])\z|\A([0-9]|1[0-2])(,([0-9]|1[0-2]))*\z|";
|
||||
$regex .= "\A([0-9]|1[0-2])-([0-9]|1[0-2])\z/";
|
||||
break;
|
||||
case 'dayofweek':
|
||||
$regex = "/\A\*\z|\A[0-6]\z|\A\*\/[0-6]\z|\A[0-6](,[0-6])*\z|\A[0-6]-[0-6]\z/";
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return (bool)preg_match($regex, $value);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user