TYPO3-Documentation / TYPO3CMS-Reference-TCA

Complete reference to the Table Configuration Array ($GLOBALS['TCA']).
https://docs.typo3.org/m/typo3/reference-tca/master/en-us/
14 stars 137 forks source link

Custom form evaluation (eval) example seems incomplete #667

Open sypets opened 2 years ago

sypets commented 2 years ago

https://docs.typo3.org/m/typo3/reference-tca/main/en-us/ColumnsConfig/Type/Text/Properties/Eval.html#custom-form-evaluation

[
    'columns' => [
        'text_9' => [
            'label' => 'text_9',
            'description' => 'readOnly=1',
            'config' => [
                'type' => 'text',
                'readOnly' => 1,
            ],
        ],
    ],
]

eval is missing here, unclear how the class TypeText9Eval is to be attached.

Also, this does not exist in "styleguide", perhaps change it to reflect styleguide or remove the EXT:styleguide and change to a more generic extension key.


Compare core v12:

Example typo3/sysext/redirects/Classes/Evaluation/SourceHost.php

Configuration/TCA/sys_redirect.php:

'eval' => 'trim,' . \TYPO3\CMS\Redirects\Evaluation\SourceHost::class,

ext_localconf.php

./ext_localconf.php:use TYPO3\CMS\Redirects\Evaluation\SourceHost;
./ext_localconf.php:$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tce']['formevals'][SourceHost::class] = '';

Classes/Evaluation/SourceHost.php

class SourceHost
{
    /**
     * Returns JavaScript instruction for client side validation/evaluation
     * (invoked by FormEngine when editing redirect entities).
     *
     * Returned `JavaScriptModuleInstruction` delegates handling to corresponding
     * RequireJS module, having a method `evaluateSourceHost` that deals with that
     * evaluation request.
     *
     * @return JavaScriptModuleInstruction
     */
    public function returnFieldJS(): JavaScriptModuleInstruction
    {
        return JavaScriptModuleInstruction::create('@typo3/redirects/form-engine-evaluation.js', 'FormEngineEvaluation');
    }

    /**
     * Server-side removing of protocol on save
     *
     * @param string $value The field value to be evaluated
     * @return string Evaluated field value
     */
    public function evaluateFieldValue(string $value): string
    {
        // 1) Special case: * means any domain
        if ($value === '*') {
            return $value;
        }

        // 2) Check if value contains a protocol like http:// https:// etc...
        if (PathUtility::hasProtocolAndScheme($value)) {
            $tmp = $this->parseUrl($value);
            if (!empty($tmp)) {
                return $tmp;
            }
        }

     // 3) Check domain name
        // remove anything after the first "/"
        $checkValue = $value;
        if (str_contains($value, '/')) {
            $checkValue = substr($value, 0, (int)strpos($value, '/'));
        }
        $validHostnameRegex = '/^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/';
        if (preg_match_all($validHostnameRegex, $checkValue, $matches, PREG_SET_ORDER) !== false) {
            if (!empty($matches)) {
                return $checkValue;
            }
        }

        // 4) IPv4 or IPv6
        $isIP = filter_var($value, FILTER_VALIDATE_IP) === $value;
        if ($isIP) {
            return $value;
        }

        return '';
    }

    protected function parseUrl(string $value): string
    {
        $urlParts = parse_url($value);
        if (!empty($urlParts['host'])) {
            $value = $urlParts['host'];

            // Special case IPv6 with protocol: http://[2001:0db8:85a3:08d3::0370:7344]/
            // $urlParts['host'] will be [2001:0db8:85a3:08d3::0370:7344]
            $ipv6Pattern = '/\[([a-zA-Z0-9:]*)\]/';
            preg_match_all($ipv6Pattern, $urlParts['host'], $ipv6Matches, PREG_SET_ORDER);
            if (!empty($ipv6Matches[0][1])) {
                $value = $ipv6Matches[0][1];
            }
        }
        return $value;
    }
}
sypets commented 1 year ago

see styleguide Classes/UserFunctions/FormEngine/TypeInput21Eval.php

Configuration/TCA/tx_styleguide_elements_basic.php:

'eval' => 'TYPO3\\CMS\\Styleguide\\UserFunctions\\FormEngine\\TypeInput21Eval',

sypets commented 1 year ago

I don't know how to do that with the automatically generated code snippets. We need to generated different code snippets from styleguide.

ohader commented 1 year ago

And this seems to be different to TCA type inputhttps://docs.typo3.org/m/typo3/reference-tca/main/en-us/ColumnsConfig/Type/Input/Properties/Eval.html# But actually the eval processing is the same among all TCA types.