SHOW:
|
|
- or go back to the newest paste.
| 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()); |
| 9 | + | try {
|
| 10 | $that->handleSuccess($form->getValues()); | |
| 11 | ||
| 12 | } catch (ValidationError $e) {
| |
| 13 | foreach ($e->errors as $error) {
| |
| 14 | $control = $error['control'] ? $form->getComponent($error['control']) : $form; | |
| 15 | $control->addError($error['message']); | |
| 16 | } | |
| 17 | } | |
| 18 | }; | |
| 19 | } | |
| 20 | if (method_exists($this, 'handleError')) {
| |
| 21 | $form->onError[] = function (Form $form) use ($that) {
| |
| 22 | $that->handleError($form->getValues()); | |
| 23 | }; | |
| 24 | - | $that->handleClick($form->getValues()); |
| 24 | + | |
| 25 | } | |
| 26 | ||
| 27 | public function attachButton(SubmitButton $button) | |
| 28 | {
| |
| 29 | $that = $this; | |
| 30 | if (method_exists($this, 'handleClick')) {
| |
| 31 | $button->onClick[] = function (SubmitButton $button) use ($that) {
| |
| 32 | $form = $button->form; | |
| 33 | try {
| |
| 34 | $that->handleClick($form->getValues()); | |
| 35 | ||
| 36 | } catch (ValidationError $e) {
| |
| 37 | - | public $onFoo = array(); |
| 37 | + | foreach ($e->errors as $error) {
|
| 38 | - | public $onBar = array(); |
| 38 | + | $control = $error['control'] ? $form->getComponent($error['control']) : $form; |
| 39 | $control->addError($error['message']); | |
| 40 | - | public function handleSuccess($values) { ... }
|
| 40 | + | } |
| 41 | } | |
| 42 | }; | |
| 43 | } | |
| 44 | if (method_exists($this, 'handleError')) {
| |
| 45 | $button->onInvalidClick[] = function (SubmitButton $button) use ($that) {
| |
| 46 | $that->handleError($form->getValues()); | |
| 47 | }; | |
| 48 | } | |
| 49 | } | |
| 50 | } | |
| 51 | ||
| 52 | class RegistrationHandler extends FormHandler | |
| 53 | {
| |
| 54 | public $onFoo = array(); | |
| 55 | public $onBar = array(); | |
| 56 | ||
| 57 | public function handleSuccess($values) | |
| 58 | {
| |
| 59 | $error = new ValidationError; | |
| 60 | ||
| 61 | if ($values->name != "Valid") {
| |
| 62 | $error->addError("Name is invalid", 'name');
| |
| 63 | } | |
| 64 | ||
| 65 | $error->assertValid(); | |
| 66 | $this->model->save($values); | |
| 67 | } | |
| 68 | } | |
| 69 | ||
| 70 | ||
| 71 | class ValidationError extends \Exception | |
| 72 | {
| |
| 73 | public $errors = array(); | |
| 74 | ||
| 75 | public function addError($error, $controlName = NULL) | |
| 76 | {
| |
| 77 | $this->errors[] = array('message' => $error, 'control' => $controlName);
| |
| 78 | return $this; | |
| 79 | } | |
| 80 | ||
| 81 | public function assertValid() | |
| 82 | {
| |
| 83 | if ($this->errors) { throw $this; }
| |
| 84 | } | |
| 85 | } | |
| 86 | ||
| 87 | $form = new Form; | |
| 88 | $handler = new RegistrationHandler(); | |
| 89 | $handler->attach($form); | |
| 90 | $handler->attachButton($form->addSubmit('send', "Odeslat")); |