renobird Posted August 22, 2014 Posted August 22, 2014 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.
Martijn Geerts Posted August 22, 2014 Posted August 22, 2014 I do think field errors are cleaned in the InputfieldWrapper render() methode. So maybe you need an additional hook before InutfieldWrapper render and loop through the fields and count the errors. 1
renobird Posted August 22, 2014 Author Posted August 22, 2014 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. 1
renobird Posted August 22, 2014 Author Posted August 22, 2014 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/"); } 1
Martijn Geerts Posted August 22, 2014 Posted August 22, 2014 I'm not sure about that notices loop, if you didn't delete the install.php for example does that prevent your $session->redirect ? 1
renobird Posted August 22, 2014 Author Posted August 22, 2014 if you didn't delete the install.php for example does that prevent your $session->redirect? Not sure, but in that case it's OK for it to fail, because the admin (me) has some cleanup to do.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now