here some code:
Form Class
<?php
namespace Form\Model\Entity;
use Laminas\Form\Annotation; //annotation is for fun
/**
* @Annotation\Name("form")
*/
class Form {
//[...]
/**
* @Annotation\Name("child")
* @Annotation\Type("Form\Model\Entity\Privato\Coll")
*/
private $child_coll;
}
?>
Collection Class
<?php
namespace Form\Model\Entity\Privato;
use Laminas\Form\Element;
class Coll extends \Laminas\Form\Fieldset implements \Laminas\InputFilter\InputFilterProviderInterface{
public function __construct() {
parent::__construct();
$this->add([
'type' => Element\Collection::class,
'options' => [
'count' => 1, //test_c
'allow_add' => true,
'should_create_template' => true,
'template_placeholder' => '__placeholder__',
'target_element' => [
'type' => CollElemFieldset::class,
],
],
]);
}
public function getInputFilterSpecification(): array {
return [];
}
}
Collection Element Class
<?php
namespace Form\Model\Entity\Privato;
use Laminas\Form\Element;
class CollElemFieldset extends \Laminas\Form\Fieldset implements \Laminas\InputFilter\InputFilterProviderInterface {
public function __construct() {
parent::__construct('child_elem');
$this->add([
'name' => 'surname',
'type' => Element\Text::class,
'options' => [
'label' => 'Cognome',
],
'attributes' => [
'required' => 'required',
],
]);
$this->add([
'name' => 'name',
'type' => Element\Text::class,
'options' => [
'label' => 'Nome',
],
'attributes' => [
'required' => 'required',
],
]);
$this->add([
'name' => 'gender',
'type' => \Form\Element\GenderSelect::class,
'options' => [
'label' => 'Sesso',
],
'attributes' => [
'required' => 'required',
],
]);
$this->add([
'name' => 'maritial_status',
'type' => \Form\Element\MaritialStatus::class,
'options' => [
'label' => 'Stato civile',
],
'attributes' => [
'required' => 'required',
],
]);
$this->add([
'name' => 'birthday',
'type' => Element\Date::class,
'options' => [
'label' => 'Data di nascita',
],
'attributes' => [
'required' => 'required',
],
]);
$this->add([
'name' => 'npwp',
'type' => Element\Text::class,
'options' => [
'label' => 'Codice fiscale',
],
]);
}
public function getInputFilterSpecification(): array {
return [];
}
}
child_elem.phtml
<?php
$reg = $this->reg;
$formLabel = $this->plugin('formLabel');
$surname = $reg->get('surname');
$surname->setAttribute('class', 'form-control custom_surname');
$name = $reg->get('name');
$name->setAttribute('class', 'form-control');
$gender = $reg->get('gender');
$gender->setAttribute('class', 'form-control');
$maritial_status = $reg->get('maritial_status');
$maritial_status->setAttribute('class', 'form-control');
$birthday = $reg->get('birthday');
$birthday->setAttribute('class', 'form-control');
$npwp = $reg->get('npwp');
$npwp->setAttribute('class', 'form-control npwp');
?>
<div class="box_person">
<div class="form-row">
<div class="col-md-5">
<?php
echo $formLabel->openTag() . $surname->getOption('label');
echo $formLabel->closeTag();
echo $this->formInput($surname);
echo $this->formElementErrors($surname);
?>
</div>
<div class="col-md-4">
<?php
echo $formLabel->openTag() . $name->getOption('label');
echo $formLabel->closeTag();
echo $this->formInput($name);
echo $this->formElementErrors($name);
?>
</div>
<div class="col-md-3">
<?php
echo $formLabel->openTag() . $gender->getOption('label');
echo $formLabel->closeTag();
echo $this->formSelect($gender);
echo $this->formElementErrors($gender);
?>
</div>
</div>
<div class="form-row">
<div class="col-md-4">
<?php
echo $formLabel->openTag() . $maritial_status->getOption('label');
echo $formLabel->closeTag();
echo $this->formSelect($maritial_status);
echo $this->formElementErrors($maritial_status);
?>
</div>
<div class="col-md-4">
<?php
echo $formLabel->openTag() . $birthday->getOption('label');
echo $formLabel->closeTag();
echo $this->formInput($birthday);
echo $this->formElementErrors($birthday);
?>
</div>
<div class="col-md-4">
<?php
echo $formLabel->openTag() . $npwp->getOption('label');
echo $formLabel->closeTag();
echo $this->formInput($npwp);
echo $this->formElementErrors($npwp);
?>
</div>
</div>
</div>
index.phtml
<?php
//...
// custom render but no dynamic template
$collection = $form->get('child')->get('collection');
$this->partialLoop()->setObjectKey('reg');
echo $this->partialLoop('child_elem.phtml', $collection);
//not custom render but dynamic template
echo $this->formCollection($collection);
//...
?>
Should I create a custom formRow view helper and call the child_elem.phtml ?