Jump to content

Run php code in Hook function only if there are no errors after submitting the form

Recommended Posts

Here is a tipp for you if you want to check for validation errors in inputfields before processing php code inside a Hook function.

My goal was to run the code inside the Hook function only if there are no errors after processing the form in the backend.

$pages->addHookBefore('saveReady', function($event)
    $page = $event->arguments[0];
    //count errors during input process
    $errors = $page->errors('all');
    $errors = count($errors);
    if ($errors == 0) {
    ......//run your code


In this case I run a Hook before "saveReady" inside my ready.php. But I only want to run my php.code inside this Hook function if there are no errors in the form submission.

So I need to check for errors in the form submission first. These 3 simple lines of code are necessary:

    $page = $event->arguments[0];
    //count errors during input process
    $errors = $page->errors('all');
    $errors = count($errors);
  1. The first step is to define the page variable $page;
  2. Second step is to get the error array
  3. Last step is to count the entries in the array to get the number of the errors

Thats all! Afterwards you can use the number of errors in an if-statement to run the code only if there are no errors.

Hope this will be helpful for someone.


  • Like 6

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...