RecursiveValidator or ListValidator?

Hello Experts. :slight_smile:

@FIRST: I am so sorry for my terrible english. I hope you can understand what I mean.

I get from Form[POST] a list (`$this->request->getPost()->toArray()``) :

$formData = [
        'values[]' => [ // dynamic length (controlled by javascript) & values
            'abcd',
            'Ab123',
            'jkjkj#-'
        ]
    ];

All input[text] fields of ‘values’ have a 'data-toggle=> ‘tooltip’ DIV.

I want to filter/validate all ‘values’ elements with/like:

[
        'name' => 'values[]',
        'filters' => [
            [ 'name' => \Laminas\Filter\StringTrim::class ],
            [ 'name' => \Laminas\Filter\StripTags::class ],
            [ 'name' => \Laminas\I18n\Filter\Alnum::class ],
        ],
        'validators' =>[
            [
                'name' => \Laminas\Validator\StringLength::class,
                'options' => [
                        'min' => 2,
                        'max' => 24,
                        'messsages' => [
                                \Laminas\Validator\StringLength::TOO_SHORT => 'too short',
                                \Laminas\Validator\StringLength::TOO_LONG => 'too long',
                        ],
                 ],
        ],
        [
             'name' => \Laminas\I18n\Validator\Alnum::class,
            'option' => [
                'messages' => [
                    \Laminas\I18n\Validator\Alnum::NOT_ALNUM => 'not alnum',
                ],
            ],
        ],
    ],
]

and send validation messages to the my $formData[‘values[2]’]??? (wrong input in this example)

best greetings and be health :slight_smile:

To use laminas-form or only laminas-inputfilter?

Good Evening my best Expert. :slight_smile:

Inputfilter in Form\Form.

.........
$this->add([
            'type' => Element\Text::class,
            'name' => 'answers[]',
            'options' => [
                'label' => 'Answers'
            ],
            'attributes' => [
                'required' => true,
                'size' => 40,
                'maxlength' => 100,
                'class' => 'form-control',
                'placeholder' => 'Enter a possible answer',
                'data-toggle' => 'tooltip',
                'title' => 'Provide a possible answer'
            ]
        ]);

in phtml i have two rows for ‘answers[]’ and i can configure to max. 5 rows with an onclick event.
best greetings

The topic is “Collections” which means to use a special element that contains an form element or a set of form elements. These sub-elements can be repeated multiple times.
The special element with name “Collection” can also create a template for use with JavaScript.

Example

Example form class:

class ExampleForm extends Laminas\Form\Form
{
    public function init() : void
    {
        $this->add(
            [
                'type'    => Laminas\Form\Element\Collection::class,
                'name'    => 'answers',
                'options' => [
                    'label'                  => 'Answers',
                    'count'                  => 2,
                    'should_create_template' => true,
                    'allow_add'              => true,
                    'target_element'         => [
                        'type' => Laminas\Form\Element\Text::class,
                        // …
                    ],
                ],
            ]
        );
    }
}

Usage:

// Init form
$form = new ExampleForm();
$form->init();

// Validate form
$form->setData(
    [
        'answers' => [
            'Foo',
            'Bar',
            'Baz',
        ],
    ]
);
var_dump($form->isValid()); // true

// Render form
/** @var Laminas\View\Renderer\PhpRenderer|Laminas\Form\View\HelperTrait $renderer */
$renderer = new Laminas\View\Renderer\PhpRenderer();
$renderer->getHelperPluginManager()->configure(
    (new Laminas\Form\ConfigProvider())->getViewHelperConfig()
);
echo $renderer->form($form);

The rendered form:

<form action="" method="POST">
    <fieldset>
        <legend>Answers</legend>
        <input type="text" name="answers&#x5B;0&#x5D;" value="">
        <input type="text" name="answers&#x5B;1&#x5D;" value="">
        
        <span data-template="&lt;input&#x20;type&#x3D;&quot;text&quot;&#x20;name&#x3D;&quot;answers&amp;&#x23;x5B&#x3B;__index__&amp;&#x23;x5D&#x3B;&quot;&#x20;value&#x3D;&quot;&quot;&gt;"></span>
    </fieldset>
</form>

Learn more

More informations can be found in the documentation of laminas-form: https://docs.laminas.dev/laminas-form/collections/

Try the “Collection” element and if you need more help with the related input-filter, please write here.

Hello :slight_smile:

I will look. It sounds very well.

thx.