HosipLan

Untitled

Jan 4th, 2013
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.21 KB | None | 0 0
  1. abstract class FormHandler extends Nette\Object
  2. {
  3.     public function attach(Form $form)
  4.     {
  5.         $that = $this;
  6.         if (method_exists($this, 'handleSuccess')) {
  7.             $form->onSuccess[] = function (Form $form) use ($that) {
  8.                 if (!$form->isValid()) return;
  9.                 $that->handleSuccess($form->getValues());
  10.             };
  11.         }
  12.         if (method_exists($this, 'handleError')) {
  13.             $form->onError[] = function (Form $form) use ($that) {
  14.                 $that->handleError($form->getValues());
  15.             };
  16.         }
  17.     }
  18.  
  19.     public function attachButton(SubmitButton $button)
  20.     {
  21.         $that = $this;
  22.         if (method_exists($this, 'handleClick')) {
  23.             $button->onClick[] = function (SubmitButton $button) use ($that) {
  24.                 $that->handleClick($form->getValues());
  25.             };
  26.         }
  27.         if (method_exists($this, 'handleError')) {
  28.             $button->onInvalidClick[] = function (SubmitButton $button) use ($that) {
  29.                 $that->handleError($form->getValues());
  30.             };
  31.         }
  32.     }
  33. }
  34.  
  35. class RegistrationHandler extends FormHandler
  36. {
  37.     public $onFoo = array();
  38.     public $onBar = array();
  39.  
  40.     public function handleSuccess($values) { ... }
  41. }
  42.  
  43. $form = new Form;
  44. $handler = new RegistrationHandler();
  45. $handler->attach($form);
  46. $handler->attachButton($form->addSubmit('send', "Odeslat"));
Advertisement
Add Comment
Please, Sign In to add comment