Federico Posted August 23, 2019 Share Posted August 23, 2019 Hi, here is my attempt in short. I want to hook the submitted form in custom admin module and fire a function if conditional applies. Problem: I cannot find a working hook to check if a form has been submitted. I would like to fire the addButtonMethod function if the form "form_application" is submitted by the "hook_accept_application" button. I know it sounds basic, but I have tried all of the following without success. Thanks a lot for the help public function init() { parent::init(); $this->addHookBefore('InputfieldForm::processInput', $this, 'addButtonsMethods'); $this->addHookBefore('InputfieldWrapper::processInput', $this, 'addButtonsMethods'); $this->addHookBefore('InputfieldSubmit::processInput', $this, 'addButtonsMethods'); } public function ___execute() { $modules = $this->wire('modules'); $form = $this->modules->get("InputfieldForm"); $form->action = "./"; $form->method = "post"; $form->attr("id+name", "form_application"); $btn_application_create = $this->modules->get("InputfieldSubmit"); $btn_application_create->attr('id+name', 'hook_accept_application'); $btn_application_create->value = "Start Student"; $btn_application_create->addClass("ui-priority-primary button_submit"); $form->append($btn_application_create); return $form->render(); } public function addButtonsMethods($event) { if($this->input->post->hook_accept_application){ wire('session')->redirect(wire('pages')->get(123)->editUrl; } } Link to comment Share on other sites More sharing options...
Zeka Posted August 23, 2019 Share Posted August 23, 2019 Hi @Federico Why just not check for input post like $modules = $this->wire('modules'); $form = $this->modules->get("InputfieldForm"); $form->action = "./"; $form->method = "post"; $form->attr("id+name", "form_application"); if($this->wire('input')->post->your_submit_button) { $btn_application_create = $this->modules->get("InputfieldSubmit"); $btn_application_create->attr('id+name', 'hook_accept_application'); $btn_application_create->value = "Start Student"; $btn_application_create->addClass("ui-priority-primary button_submit"); $form->append($btn_application_create); } Link to comment Share on other sites More sharing options...
Federico Posted August 23, 2019 Author Share Posted August 23, 2019 Hi @Zeka In the code above I have the POST input check, as follow: if($this->input->post->hook_accept_application){ wire('session')->redirect(wire('pages')->get(123)->editUrl; } which works. However, for a more articulated module development I am looking for a hook method to fire a function when the form named "form-application" is submitted. In other words, I am looking to wrap it into a function like the following one. Any idea? public function addButtonsMethods($event) { if($this->input->post->hook_accept_application){ wire('session')->redirect(wire('pages')->get(123)->editUrl; } } Link to comment Share on other sites More sharing options...
Zeka Posted August 23, 2019 Share Posted August 23, 2019 Ok, then public function ___execute() { $out = ''; $input = $this->wire('input'); $modules = $this->wire('modules'); $form = $this->modules->get("InputfieldForm"); $form->action = "./"; $form->method = "post"; $form->attr("id+name", "form_application"); $btn_application_create = $this->modules->get("InputfieldSubmit"); $btn_application_create->attr('id+name', 'hook_accept_application'); $btn_application_create->value = "Start Student"; $btn_application_create->addClass("ui-priority-primary button_submit"); $form->append($btn_application_create); if ($input->post->hook_accept_application) { $form->processInput($input->post); if ($form->getErrors()) { $out .= $form->render(); } else { $this->addButtonsMethods(); } } else { $out = $form->render(); } return $out; } public function addButtonsMethods($event) { wire('session')->redirect(wire('pages')->get(123)->editUrl; } 1 Link to comment Share on other sites More sharing options...
bernhard Posted August 23, 2019 Share Posted August 23, 2019 $wire->addHookAfter("InputfieldForm(name=form-application)::processInput", function($event) { $form = $event->object; /** @var InputfieldForm $form */ if(count($form->getErrors())) return; // do your stuff $input = $event->arguments(0); }); Something like this? See https://processwire.com/api/ref/inputfield-form/process-input/ 2 Link to comment Share on other sites More sharing options...
Federico Posted August 25, 2019 Author Share Posted August 25, 2019 Thanks @Zeka and @bernhard for some reasons the code posted by Bernard gives an error, even if starts with $this->wire->addHookAfter instead of just $wire->addHookAfter maybe because the form name declaration in this method (I did not know, can you actually declare the form name in that?) InputfieldForm(name=form-application)::processInput Anyway, I have solved this minor issue with the approach as per the Zeka suggestion. Thank you both Link to comment Share on other sites More sharing options...
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