contributte / forms-multiplier

:repeat: Form multiplier & replicator for Nette Framework
https://contributte.org/packages/contributte/forms-multiplier.html
MIT License
26 stars 21 forks source link

Allow setDefaults after the component is attached #48

Closed Thoronir42 closed 4 years ago

Thoronir42 commented 4 years ago

Hi, In this PR, I'd like to introduce the possibility to use setDefaults later in the Component model life cycle than in the constructor. Currently it is possible to set values only before the component is attached, which makes the Multiplier a bit tough to use with a typical Nette application life cycle.

With this PR, it is possible to:

class RecipePresenter {

  public function actionDetail($recipeId) {
    $recipe = [
      'name' => 'hot-dog',
      'steps' => [
        ['description' => 'put sausage into boiling water'],
        ['description' => 'make hole into a bread roll'],
        ['description' => 'optional: add ketchup or mustard into bread roll'],
        ['description' => 'insert sausage'],
      ]
    ];
    $form = $this['recipeForm'];
    $form->setDefaults($recipe);
  }

  public function createComponentRecipeForm() {
    $form = new Form();
    $form->addText('title', 'Recipe name');
    $form->addMultiplier('steps', 'Steps', function($container) {
      $container->addTextarea('description');
    });

    return $form;
  }
}

Note: The first commit is not neccessary for this PR. As I was investigating, I found out that the removed functionality does not seem to bring any value and with re-initialization in mind, I wanted to reduce needles instances creation

MartkCz commented 4 years ago

Thanks!