In Yii2, verify the implementation of the value of the time format 15:00
1. The code is implemented as follows
use yii\base\InvalidArgumentException;
use yii\validators\DateValidator;
$validator = new DateValidator(['type' => DateValidator::TYPE_TIME, 'format' => 'H:i']);
if (!$validator->validate($this->value, $error)) {
throw new InvalidArgumentException($surveyFormField['name'] . '验证失败:' . $error);
}
2. But the verification failed, as shown in Figure 1
{
"code": 10500,
"message": "时间验证失败:属性 该输入 的格式无效。"
}
3. Since the format of H:I is the format in PHP. Set the string starting with php: to represent the date-time format that PHP can recognize. Verification passed. as shown in Figure 2
use yii\base\InvalidArgumentException;
use yii\validators\DateValidator;
$validator = new DateValidator(['type' => DateValidator::TYPE_TIME, 'format' => 'php:H:i']);
if (!$validator->validate($this->value, $error)) {
throw new InvalidArgumentException($surveyFormField['name'] . '验证失败:' . $error);
}

