Jump to content

Hook question: How to add a value to a field before field validation on save


Juergen
 Share

Recommended Posts

Hello @ all,

I have a required field inside a template and I want to add a value via a hook if the user enters nothing in this field. Therefore I have tried to hook before "processInput".

$wire->addHookBefore("InputfieldDatetime::processInput", function($event) {
$page= $this->process->getPage();
$page->of(false);  
  if(!in_array($page->template->name, ['single-event'])) return;
  if($page->parent->reservationdeadline != ''){
    $diff = ($page->parent->starteventdate) - ($page->parent->reservationdeadline);
    if(($page->starteventdate) && ($page->reservationdeadline != '')){
       $page->reservationdeadline = ($page->starteventdate)-$diff;
    }
  }
});

So if the user presses the save button, then it should be checked if a value was entered in date field called "reservationdeadline". If not then a value should be entered via the hook before the validation of the required field takes place.

At the moment I always get the error message that there is no value entered in the required field.

Could someone give me a hint how to achive this?

I know I could set the option to non required and enter the value always if nothing is there but it is possible to change it before the form will be validated?

Link to comment
Share on other sites

Keep in mind that the regular processInput method is still called afterwards, so any assignments to the page's fields will be overwritten by the submitted data. You need to fetch the $input argument from $event->arguments(0), read the submitted value from there and assign your computed value to $input["reservationdeadline"] (peek into the POST data in the developer console if you're unsure what date format to use). Don't modify $page, let the regular PW logic handle that part.

Though you'd probably find it easier to remove the "required" option and run your logic in a saveReady hook.

  • Like 1
Link to comment
Share on other sites

2 minutes ago, BitPoet said:

Keep in mind that the regular processInput method is still called afterwards

Ok, I thought that the form data could be changed before processing the form if I use "$wire->addHookBefore("InputfieldDatetime::processInput", function($event) {"

Thanks @BitPoet

Link to comment
Share on other sites

1 hour ago, Zeka said:

SchedulePages module has similar functionality and it's done via hook to after page save

Only to mention: after page save was not an opportunity for me, because the validation of the required field kicks in before this hook :(. I have tried it before, but thanks for your hints @Zeka

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