Submitting your form to another presenter

You probably know that you can define your form as a component in a class of its own and handle its submission in the same class. This is handy if the submission handling logic can be isolated and is independent of any presenter. But what if it needs to be a part of some particular presenter lifecycle? Then you need to force the form to submit to this particular presenter.

Define your form as a component (e.g. app/forms/SignInForm.php):

  1.  
  2. class SignInForm extends NAppForm {
  3.   function __construct($parent = NULL, $name = NULL) {
  4.     parent::__construct($parent, $name);
  5.     $this->addText(‘name’, ‘Name:’);
  6.     // …
  7.     $this->addSubmit(‘login’, ‘Sign In’);
  8.   }
  9. }
  10.  

Handle the submission in the target presenter (app/presenters/MainPresenter.php):

  1.  
  2. function createComponentSignInForm($name) {
  3.   $form = new SignInForm($this, $name);
  4.  
  5.   $form->onSuccess[] = callback($this, ‘signInFormSubmitted’);
  6.   return $form;
  7. }
  8.  
  9. function signInFormSubmitted($form) {
  10.     $values = $form->getValues();
  11.     // …
  12. }
  13.  

For the other presenters, you’ll need to use a bit of a hacky solution (app/presenters/OtherPresenter.php):

  1.  
  2. function createComponentSignInForm($name) {
  3.   $form = new SignInForm($this, $name);
  4.  
  5.   // it’s okay to change your form here
  6.   $form[‘login’]->caption = ‘Continue’;
  7.  
  8.   $url = $this->link(‘Main:myAction’, array(
  9.     NPresenter::SIGNAL_KEY =>
  10.       $name.self::NAME_SEPARATOR.‘submit’
  11.   ));
  12.   $form->setAction($url);
  13.  
  14.   return $form;
  15. }
  16.  

Tags:

Leave a Reply