This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

Snirkum
Snirkum

Hi,

How do i bypass the default handling of errors on the front-end? Example: `if ($validation->fails())

  throw new ValidationException($validation);`

As far as i can see they will go through ValidationException and somehow end up in a json string to the framework.js. This will create a popup with that string. But i would like to have more control over how its presented and what it does. I don't like the alert type of error reporting.
how can i change this behavour? Im looking at data-request-error, but that just adds another alert. What im trying to get is somekind of response that doesnt use the alert but gives something back to the frontend that i can catch up on. Think of highlighting the textfield with a error ala bootstrap.

I hope you get what i mean.

Last updated

Daniel81
Daniel81

Yeah, I've looked at this in the past, but I couldn't stop the script from processing, so if you didn't throw an exception, even if there was an error, the script would still run & the form would still get submitted.

It's easy enough to pass the validation messages to TWIG, I just don't know how to halt the script if there is an error.

Snirkum
Snirkum

atm im trying to flash the errors to the seesion and dont throw in a error. But testing this now :) Seams silly as i think of it and wont work as expected. You have to trow in an error. Now figuring out how to stop the alert and load a partial. Wich can loop trough the errors.

Last updated

Daniel81
Daniel81

You could pass the errors to the same partial as the form etc, that's easy enough, but like I said, I don't know how to stop the form from processing once it hits an error other than throwing an exception?

Last updated

mesinrusak
mesinrusak

try this:

/components/blabla.php

public function onCheck(){
...
$validation = Validator::make($data, $rules);
if ($validation->fails()) {
    //throw new ValidationException($validation);
    $this->page['message'] = $validation->errors()->toArray();
    return false;
}
...
}

/components/blabla/alert.htm

{% if message %}
 <div class="alert alert-primary alert-dismissible" role="alert">
  <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
  <ul>
  {% for item in message %}
  <li>{{item.0}}</li>
  {% endfor %}
  </ul>
</div>
 {% endif %}

page.htm

<div id="message"></div>
<form data-request="onCheck" data-request-update="'blabla::alert': '#message'">
...
</form>

Last updated

paltakacs
paltakacs

Hey, I was searching about the same issue but I didn't find any useful stuff. However I solved it myself but I see I was late a "little".

So the solution (without mod the original plugin) is the following:

You need to create a custom plugin, and you need to add a custom method.

Inside that custom method (in your custom component class) you should get an instance of the target(original) component and you should add a try-catch solution, for example:


use ValidationException;
class QuickForm...
...public function onAccountSignin() {
$account = $this->findComponentByName('account');
        try {
            $account->onSignin();
        } catch( ValidationException $exception ) {
            $this->page['partialResponse'] = $exception->getMessage());
        }
}

At the end you should use your own custom method at ajax call. (For example: form data-request="quickform::onAccountSignin" )

I hope It helps those who encounter this problem.

Best, Pal

Last updated

1-6 of 6

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.