Reputation: 538
In ZF2, I have a form that has two fieldsets. The 2 fieldsets are basically for party-related info (party = person or company) and phone-related info. The fieldsets are called using the init method of the form like this
class PhoneRegistrationForm extends RegistrationForm implements InputFilterAwareInterface
{
public function __construct()
{
parent::__construct();
$this
->setAttribute('method', 'post')
->setHydrator(new ClassMethods(false))
->setObject(new Phone());
$this->add([
'type' => 'Zend\Form\Element\Button',
'name' => 'submitButton',
'attributes' => [
'id' => 'submit-phone-button',
'class' => 'btn2 submit-button',
'type' => 'submit',
],
'options' => [
'label' => 'Сохранить',
],
]);
}
public function init()
{
$this->add([
'type' => 'Parties\Forms\Fieldsets\PhoneFieldset',
'name' => 'phoneFieldset'
]);
$this->add([
'type' => 'Parties\Forms\Fieldsets\PartyIdAndRolesFieldset',
'name' => 'partyFieldset'
]);
}
}
In the controller's edit action, I create and hydrate the PhoneObject and PartyObject with the values from the database (PhoneObject and PartyObjects are just collection of getters and setters). I then set the hydrated objects to the fieldsets:
$this->form->get('phoneFieldset')->setObject($phone);
$this->form->get('partyFieldset')->setObject($party);
And here is my problem, if I return the instance of form to the controller, the form inputs have no values that come from the attached hydrated objects.
How to get the form inputs filled with the values from the hydrated objects attached to fieldsets?
I tried binding $this->form->bind($phone)
, but to no avail. The fieldsets have ClassMethods()
hydrator set too.
What could the problem be? Any clues?
EDIT: after answer by @hooli, this is the code for the fieldset that I use.
class PhoneFieldsetFactory extends Fieldset implements InputFilterProviderInterface
{
public function __invoke(FormElementManager $formElementManager)
{
$this
->setHydrator(new ClassMethods())
->setObject(new Phone());
$this->add([
'type' => 'hidden',
'name' => 'id',
]);
$this->add(
$formElementManager
->get('Parties\Forms\Elements\ContactMechanismTypeSelect',
['mechanismType' => 'телефон'])
);
$this->add(
$formElementManager
->get('Parties\Forms\Elements\CountrySelect')
);
$this->add(
$formElementManager
->get('Parties\Forms\Elements\ContactMechanismPurposeTypeMultiCheckbox',
['mechanismType' => 'телефон'])
);
$this->add([
'type' => 'text',
'name' => 'areaCode',
'attributes' => [
'id' => 'area-code',
'placeholder' => '1234',
],
'options' => [
'label' => 'Код области'
],
]);
$this->add([
'type' => 'text',
'name' => 'phoneNbr',
'attributes' => [
'id' => 'phone-nbr',
'placeholder' => '1234567890',
],
'options' => [
'label' => 'Номер телефона',
],
]);
$this->add([
'type' => 'text',
'name' => 'extension',
'attributes' => [
'id' => 'extension',
'placeholder' => '1234567890',
],
'options' => [
'label' => 'Добавочный',
],
]);
$this->add([
'type' => 'text',
'name' => 'comment',
'attributes' => [
'id' => 'comment',
'placeholder' => 'общий телефон организации',
],
'options' => [
'label' => 'Примечание',
],
]);
return $this;
}
}
Upvotes: 0
Views: 171
Reputation: 2544
Here is what I understand for your problem :
when you declare this :
$this
->setAttribute('method', 'post')
->setHydrator(new ClassMethods(false))
->setObject(new Phone());
The PhoneRegistrationForm object when you'll use bind
method will populate your Phone Object, but you didn't have any fields of your Phone Object in that direct PhoneRegistrationForm because you use a fieldset.
Then it's your fieldset you should update to set the correct hydrator strategy.
Plus, it's the same solution for you other fieldset.
I usually use doctrine and entities, but it's the same logic.
Upvotes: 1
Reputation: 538
THIS IS NOT A REALLY GOOD SOLUTION, PLEASE FEEL FREE TO SUGGEST BETTER VARIANTS! I WILL MAKE YOUR ANSWER ACCEPTED IF IT'S BETTER.
After a day of trials and errors still couldn't figure out why the form inputs don't get filled based on the objects attached to the fieldsets. I found, though, a workaround for this.
A ZF2 Fieldset
implements the method populateValues()
. If you use something like this
$this->form->get('neededFieldset')->populateValues(
$this->form->getHydrator()->extract($objectWhoseValuesWillBeExtracted));
you'll have the form's inputs filled with the corresponding object's data.
Upvotes: 0