Skip to content

Commit

Permalink
[TASK] move BE validation to external JS
Browse files Browse the repository at this point in the history
  • Loading branch information
t3brightside committed Feb 8, 2024
1 parent b6c18d5 commit 3b6bac8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 22 deletions.
28 changes: 6 additions & 22 deletions Classes/Evaluation/HoursMinutesSeconds.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Brightside\Youtubevideo\Evaluation;

use TYPO3\CMS\Core\Page\JavaScriptModuleInstruction;
/**
* Class for field value validation/evaluation to be used in 'eval' of TCA
*/
Expand All @@ -9,29 +10,12 @@ class HoursMinutesSeconds

/**
* JavaScript code for client side validation/evaluation
*
* @return string JavaScript code for client side validation/evaluation
*/
public function returnFieldJS()
public function returnFieldJS(): JavaScriptModuleInstruction
{
return "
var value = value.replace(/[-|_|,|.|–|']+/g,':');
var value = value.replace(/[^\d:]+/g,'');
var value = value.replace(/^:|:$/g, '');
var p = value.split(':'),
s = 0, m = 1;
while (p.length > 0) {
s += m * parseInt(p.pop(), 10);
m *= 60;
}
if (s > 0) {
var date = new Date(0);
date.setSeconds(s);
var value = date.toISOString().substr(11, 8);
return value;
} else {
return '';
}
";
return JavaScriptModuleInstruction::create(
'@t3brightside/youtubevideo/form-engine-evaluation.js',
'FormEngineEvaluation'
);
}
}
10 changes: 10 additions & 0 deletions Configuration/JavaScriptModules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

return [
'dependencies' => [
'backend',
],
'imports' => [
'@t3brightside/youtubevideo/' => 'EXT:youtubevideo/Resources/Public/JavaScript/BackEnd/',
],
];
31 changes: 31 additions & 0 deletions Resources/Public/JavaScript/BackEnd/form-engine-evaluation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import FormEngineValidation from "@typo3/backend/form-engine-validation.js";
export class FormEngineEvaluation {
static registerCustomEvaluation(t) {
FormEngineValidation.registerCustomEvaluation(t, FormEngineEvaluation.evaluateSourceHost)
}
static evaluateSourceHost(t) {
// Replace characters
t = t.replace(/[-|_|,|.||']+/g, ':');
t = t.replace(/[^\d:]+/g, '');
t = t.replace(/^:|:$/g, '');

// Calculate time
var p = t.split(':');
var s = 0,
m = 1;
while (p.length > 0) {
s += m * parseInt(p.pop(), 10);
m *= 60;
}

// Check if valid time
if (s > 0) {
var date = new Date(0);
date.setSeconds(s);
t = date.toISOString().substr(11, 8);
return t;
} else {
return '';
}
}
}

0 comments on commit 3b6bac8

Please sign in to comment.