Created by @barglm in Q&As
@barglm
@barglm

One more question, I have a form, which results in following message when I do not select any option with price:

"There was an error processing your request. Please try again later."

In the code, there is $form->addError for MissingValueException, do I need to do anything extra after catch and addError?

public function formSucceeded(Form $form, ArrayHash $values)
    {
        try {
            ...

            // Check total price
            if (!$totalPrice = (float) $values->total_price ?? 0.0) {
                throw new MissingValueException('You did not select any services', 'total_price');
            }

            unset($values->total_price);

           ...
        } catch (MissingValueException | InvalidValueException $ex) {
            $form->addError($ex->getMessage());
        } catch (\Dibi\Exception $ex) {
            $form->addError('Something went wrong, please try again');

        }

        if (!$form->hasErrors()) {
            $this->finishHandler(); // handle redirect
        }
@jahudka
@jahudka

Hi, yeah - you need to redraw the snippet containing the form :-)

UI\Forms' submits are internally handled using signals in Nette - and the default logic of the Nittro base presenter is that default snippets only ever get redrawn for non-signal requests - the logic being that within a signal handler you'll most probably want to redraw something more specific or just send an empty response anyway. If you fire up DevTools before submitting your form and look at the response the server sent you you'll probably see the full layout rendered as HTML instead of a JSON payload.

Another thing is that a form's onError() callbacks are also processed during a submit, and hence actually within a signal handler - so if you expect your form to fail on a server-side validation even before the onSuccess() callback gets called you should add an onError() callback which also redraws the appropriate snippet.

Sign in to post a reply