Andy
August 23, 2020, 12:23pm
1
Hello Experts.
@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
To use laminas-form or only laminas-inputfilter?
Andy
August 24, 2020, 6:22pm
3
Good Evening my best Expert.
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[0]" value="">
<input type="text" name="answers[1]" value="">
<span data-template="<input type="text" name="answers&#x5B;__index__&#x5D;" value="">"></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.
Andy
September 2, 2020, 5:56pm
6
Hello
I will look. It sounds very well.
thx.