Presenter lifecycle after form is submitted

Nette documentation doesn’t provide details about methods execution order in case of a submitted form. So hear hear, it is this:

  1. action<Action>
  2. createComponent<FormName>
  3. <FormName>Submitted
  4. render<Action>

Don’t be confused. It is different from when you call presenter’s action directly, without submitting a form. That would be:

  1. action<Action>
  2. render<Action>
  3. createComponent<FormName>

One situation when you need to know this, is forms with a variable number of containers. For example you edit all products on a single page, each product having its own container (a sub form). In one category, you have 5 products, in some other category only 3. Although you define your form in createComponent<FormName>, you need to add the containers in action<Action> because that’s where you know what category you edit. You can’t do it in render<Action> because <FormName>Submitted already needs to have the structure defined in order for $form->getValues to give you the product values.

Another case when you need to be aware of this order is if you evaluate user’s authorization based on the Action context. If you can’t do this in presenter’s startup then make sure you handle it in action<Action> and not in render<Action>. You want to be sure that the user is allowed to submit the form before you process the submission. To be on the safe side, you could also check for permissions inside of <FormName>Submitted but that would be repeating business logic that you need to have in action<Action> (or render<Action>) anyway.


Leave a Reply