我阅读https://docs.typo3.org/typo3cms/TCAReference/ColumnsConfig/Type/Input.html#eval并尝试自己的评估。
它应该用这个逻辑评估三个字段的组合: 需要start_date AND end_date(非空)或者需要date_on_request。
加载类并且函数evaluateFieldValue()有效,但我错过了表单中的反馈。
<?php
namespace Vendor\Extension\Evaluation;
class StartDateAndEndDateOrDateOnRequestEvaluation {
/**
* JavaScript code for client side validation/evaluation
*
* @return string JavaScript code for client side validation/evaluation
*/
public function returnFieldJS() {
return 'return value;';
}
/**
* Server-side validation/evaluation on saving the record
*
* @param string $value The field value to be evaluated
* @param string $is_in The "is_in" value of the field configuration from TCA
* @param bool $set Boolean defining if the value is written to the database or not.
* @return string Evaluated field value
*/
public function evaluateFieldValue($value, $is_in, &$set) {
foreach($_POST['data']['tx_extension_domain_model_course'] as $id => $course) {
if ( (!empty($course['start_date']) && !empty($course['start_date'])) || !empty($course['date_on_request']) ) {
$set = true;
} else {
$set = false;
}
}
return $value;
}
/**
* Server-side validation/evaluation on opening the record
*
* @param array $parameters Array with key 'value' containing the field value from the database
* @return string Evaluated field value
*/
public function deevaluateFieldValue(array $parameters) {
return $parameters['value'];
}
}
我正在寻找示例,我如何在JavaScript中进行验证(returnFieldJS): - 我如何获得三个领域? - 如何设置错误类?
我应该在evaluateFieldValue()中查看什么?
答案 0 :(得分:0)
returnFieldJS()
字段未调用方法deevaluateFieldValue(array $parameters)
和datetime
。这就是为什么我认为没有干净的方法可以向datetime
字段添加JavaScript验证。
要在后端获取错误消息,您可以使用FlashMessageService
。我实现了一个示例,以检查course_end
日期是否晚于course_start
日期:
<?php
namespace Vendor\Extension\Evaluation;
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Core\Messaging\FlashMessageService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
class StartDateAndEndDateOrDateOnRequestEvaluation {
/**
* JavaScript code for client side validation/evaluation
*
* @return string JavaScript code for client side validation/evaluation
*/
public function returnFieldJS()
{
return 'return value;';
}
/**
* Server-side validation/evaluation on saving the record
*
* @param string $value The field value to be evaluated
* @param string $is_in The "is_in" value of the field configuration from TCA
* @param bool $set Boolean defining if the value is written to the database or not.
* @return string Evaluated field value
*/
public function evaluateFieldValue($value, $is_in, &$set)
{
$formData = GeneralUtility::_GP('data');
$courseId = key($formData['tx_extension_domain_model_course']);
$course = $formData['tx_extension_domain_model_course'][$courseId];
$courseStart = new \DateTime($course['course_start']);
$courseEnd = new \DateTime($course['course_end']);
if ($courseStart > $courseEnd) {
$this->flashMessage('Invalid field value', 'Course end date can not be before course start date!', FlashMessage::ERROR);
$set = false; //do not save value
}
return $value;
}
/**
* Server-side validation/evaluation on opening the record
*
* @param array $parameters Array with key 'value' containing the field value from the database
* @return string Evaluated field value
*/
public function deevaluateFieldValue(array $parameters)
{
return $parameters['value'];
}
/**
* @param string $messageTitle
* @param string $messageText
* @param int $severity
*/
protected function flashMessage($messageTitle, $messageText, $severity = FlashMessage::ERROR)
{
$message = GeneralUtility::makeInstance(
FlashMessage::class,
$messageText,
$messageTitle,
$severity,
true
);
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
$flashMessageService = $objectManager->get(FlashMessageService::class);
$messageQueue = $flashMessageService->getMessageQueueByIdentifier();
$messageQueue->addMessage($message);
}
}