Macbernie Macbernie - 3 months ago 6
PHP Question

Second formbuilder include fields of first formbuilder

Related to my previous post Call to a member function addPaiementType() on null, if I have two distinct form:

$form = $app['manager.form']
->addDuree()
->addPaiementType()
->addTermsAccepted()
->getForm();

$formcb = $app['manager.form']
->addFirstname()
->addLastname()
->addNumero()
->addMonth()
->addYear()
->addCryptogramme()
->getForm();


The render of the second form include the fields of the form declared before it... It should not, because I have declared two distinct object Form.

Don't understand why the second form include the fields of the first one.

But this is working:

//$form = $app['manager.form']
$f1 = new Form($app);
$form = $f1
->addDuree()
->addPaiementType()
->addTermsAccepted()
->getForm();

//$formcb = $app['manager.form']
$f2 = new Form($app);
$formcb = $f2
->addFirstname()
->addLastname()
->addNumero()
->addMonth()
->addYear()
->addCryptogramme()
->getForm();


For me, it is exactly the same thing, but it seems not...

Thanks for help

Answer

You change $app['manager.form'] on calling addXXX methods, not copy.

If want to keep $app['manager.form'] as template for other forms protect callback that returns object to get callback itself on getting manager.form from application.

$app['manager.form'] = $app->protect(function() use ($app) {
    return new Form($app);
});

$form = $app['manager.form']();
$form = $form
    ->addDuree()
    ->addPaiementType()
    ->addTermsAccepted()
    ->getForm();

$formcb = $app['manager.form']();
$formcb = $formcb
    ->addFirstname()
    ->addLastname()
    ->addNumero()
    ->addMonth()
    ->addYear()
    ->addCryptogramme()
    ->getForm();
Comments