Jump to content

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


Juergen
 Share

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
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...