Jump to content

Check for errors in hook


renobird
 Share

Recommended Posts

How do I check for errors (required fields not filled in, etc..)?

I need to redirect after a successful page save, but only if there are no errors.

public function init() {
    $this->pages->addHookAfter('save', $this, 'redirectMe');
}

public function redirectMe($event){
    if ($event->arguments(0) != "news") return;
    $this->session->redirect($this->config->url->admin . "/processwire/news/");
}

The code above redirects even if there are errors on the page.

Link to comment
Share on other sites

Thanks Martijn,

That got me thinking, maybe I can just check the notices?

$errors = false;
foreach(wire('notices') as $notice) {
    $notice instanceof NoticeError ? $errors = true : "";

}

Haven't tried that yet, just riffing.

Will report back.

  • Like 1
Link to comment
Share on other sites

This seems to work. At least through some simple tests.

public function redirectMe($event){
    $page = $event->arguments(0);
    $errors = false;

    foreach(wire('notices') as $notice){
        if($notice instanceof NoticeError) $errors = true;
    }

    if (!$errors) $this->session->redirect($this->config->url->admin . "/processwire/news/");    
}
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...