0

It's the first time I am using fluid.

The challenge: In a form I am trying to mark those fields (highlighting or indicator) which are mandatory. The mandatory fields are provided as a string (within an array {settings.registration.requiredFields})

The approach I thought a can create a partial to which I pass the current field-name and compare this fieldname against the list of requiredFields.

The issue

1) I found out that partial strings can not be compared.

2) So I thought to explode the string with an v:interator.explode into an array which than can be compared with an f:for and the marking done there, as described here

After some hours of earching an googling I have now found out that I need to add the vhs and flux extensions. With no success.

In the output the statement are shown instead of the results.

<v:iterator.explode content="Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam" glue=" " as="bar">
                <div class="event-registration-value event-title">

                </div>
            </v:iterator.explode>

from the (test)input

          <v:iterator.explode content="Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam" glue=" " as="bar">
            <div class="event-registration-value event-title">
                {bar}
            </div>

        </v:iterator.explode>

What do I need to do to get this running?

PS: I am using typo3 7.6.2

2 Answers 2

0

As you do not mention any extension for the form I assume, that you just creating your own, in such case you can easily add your own ViewHelper as well which will do exactly what you need i.e.:

somewhere in your TypoScript:

plugin.tx_myext.settings.registration.requiredFields = foo bar baz

typo3conf/ext/myext/Classes/ViewHelpers/IsFieldRequiredViewHelper.php:

<?php
namespace Vendor\Myext\ViewHelpers;

use TYPO3\CMS\Core\Utility\GeneralUtility;

class IsFieldRequiredViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractConditionViewHelper {

    /**
     * @param string $fieldName Current field name
     * @param string $requiredFields List of required names separated by spaces
     *
     * @return string the rendered string
     */
    public function render($fieldName, $requiredFields) {
        $requiredArray = GeneralUtility::trimExplode(' ', $requiredFields, true);

        return (in_array($fieldName, $requiredArray))
            ? $this->renderThenChild()
            : $this->renderElseChild();
    }
}

So in your Fluid template you can use it in several ways, several samples:

{namespace myvhs=Vendor\Myext\ViewHelpers}


<!-- 1: Use your condition with then/else blocks -->
<myvhs:isFieldRequired
        fieldName="foo"
        requiredFields="{settings.registration.requiredFields}">

    <f:then>This field IS required</f:then>
    <f:else>It's optional...</f:else>

</myvhs:isFieldRequired>

<!-- 2: or just with `then` implicit block: -->
<myvhs:isFieldRequired
        fieldName="bar"
        requiredFields="{settings.registration.requiredFields}">Fill the bellow input...</myvhs:isFieldRequired>

<!-- 3: Add `required-field` class inline -->
<input type="text" class="{myvhs:isFieldRequired(fieldName: 'baz', requiredFields: settings.registration.requiredFields, then: 'required-field')}">

(note that you of course need to change the Vendor and extension name to fit your current situation)

3
  • Sorry, I'm trying to adopt the Event Registration The settings is already there. Jan 11, 2016 at 12:59
  • In such case you can create a simple extension exactly for your needs with Extension Builder: typo3.org/extensions/repository/view/extension_builder and add the mentioned ViewHelper there (note: you can use custom ViewHelpers within templates of other extensions the same way as in own exts)
    – biesior
    Jan 11, 2016 at 13:03
  • BTW, shouldn't the fields be separated by comma in this ext? (just shooting as watching it's code)
    – biesior
    Jan 11, 2016 at 15:28
0

To get the v: functions running there are two important things to do:

  1. Include the vhs extension from the TER
  2. define the namespace

The namespace is defined by adding

 {namespace v=FluidTYPO3\Vhs\ViewHelpers}

on top of the file.

Hint:

If the template breaks after this addition, it might not be the namespace line, but instead a typo in the <v:...> statements.

In my case it was a missing ending " and missing {} around same variables.

As a newbie took me three days to solve.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.