3

In my fluid template I have:

<f:form.textfield
    id="{propertyName}"
    property="{propertyName}"
    value="{value}"
    placeholder=""
    class="form-control"
/>

I want to add condition for "value". Something like:

<f:form.textfield
    id="{propertyName}"
    property="{propertyName}"

    <f:if condition="{value}"> value="{value}" <f:if>

    placeholder=""
    class="form-control"
/>

For now I'm using workaround

<f:if condition="{value}">
    <f:then>
        <f:form.textfield id="{propertyName}" property="{propertyName}" value="{value}" placeholder="" class="form-control" />
    </f:then>
    <f:else>
        <f:form.textfield id="{propertyName}" property="{propertyName}" placeholder="" class="form-control" />
    </f:else>
</f:if>

But I'd like to avoid double code.

Respant
  • 198
  • 1
  • 11

3 Answers3

1

I think this is not possible in this way.

Possible solution is user defined ViewHelper (exclude original f:form.textfield).

Developing a custom ViewHelper

Robert G.
  • 317
  • 2
  • 8
  • I think this is often the "cleanest" solution and better maintainable then very convoluted and hard to read Fluid. (if you need to fetch an expert to understand your Fluid code, maybe better to do it in PHP). You can use the AbstractTagBasedViewHelper (which is explained in the documentated, there is a link in the answer) – Sybille Peters Feb 17 '23 at 07:48
  • It might be nice to have full solution though. Would it be ok if I add a code snippet? – Sybille Peters Feb 17 '23 at 08:07
0

Nesting ViewHelpers in their XML notation is not possible. However, you can use the inline notation, at least to a certain extent:

<f:form.textfield
    value="{f:if(condition: '<your-condition>', then: value)}" />

Yes, the value attribute will still be passed this way, however, with a null value. Note that the <f:form.textfield> view helper automatically omits the value attribute if the submitted value is empty (see the source code of TYPO3\CMS\Fluid\ViewHelpers\Form\TextfieldViewHelper, line 74ff.):

public function render($required = false, $type = 'text')
{
    // [...]

    $value = $this->getValueAttribute();

    if ($value !== null) {
        $this->tag->addAttribute('value', $value);
    }

In the simple case that you just want to omit the value attribute when it's empty, just always passing value="{value}" should suffice. When the value is null, the attribute will not be included in the rendered <input> tag.

helmbert
  • 35,797
  • 13
  • 82
  • 95
0

You can do it like this:

<f:form.textfield 
    name="{data.name -> f:format.urlencode()}"   
    type="{f:if(then: '{data.inputtyp}', else: 'text', condition: '{data.inputtyp}')}"
    class="{f:if(then: '{data.class}', else: '', condition: '{data.class}')}"
    value="{f:if(then: '{data.populate}', else: '', condition: '{data.populate}')}"
    placeholder="{f:if(then: '{data.placeholder}', else: '', condition: '{data.placeholder}')}" />
Daviz
  • 1,736
  • 1
  • 11
  • 12
  • in this case attribute will always exist (with empty value, or not). I need fully hide it by condition – Respant Jun 12 '17 at 11:42
  • @Respant ah sorry, i have overseen that you asked explicitly for the "value" tag. So there is Robert G right, that's not possible with inline notation. But for the value tag: it is valid to add an emtpy value here. – Daviz Jun 12 '17 at 12:09