Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom form evaluation (eval) example seems incomplete #667

Open
sypets opened this issue Nov 18, 2022 · 3 comments
Open

Custom form evaluation (eval) example seems incomplete #667

sypets opened this issue Nov 18, 2022 · 3 comments
Labels
help wanted Request help for solving this issue / PR by others

Comments

@sypets
Copy link
Contributor

sypets commented Nov 18, 2022

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 sypets self-assigned this Dec 8, 2022
@sypets
Copy link
Contributor Author

sypets commented Dec 8, 2022

see styleguide Classes/UserFunctions/FormEngine/TypeInput21Eval.php

Configuration/TCA/tx_styleguide_elements_basic.php:

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

@sypets
Copy link
Contributor Author

sypets commented Dec 8, 2022

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

@sypets sypets removed their assignment Dec 8, 2022
@sypets sypets added the help wanted Request help for solving this issue / PR by others label Dec 8, 2022
@ohader
Copy link
Contributor

ohader commented Nov 6, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Request help for solving this issue / PR by others
Projects
None yet
Development

No branches or pull requests

2 participants