Jump to content

Recommended Posts

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;
  }
}

 

 

Share this post


Link to post
Share on other sites

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);
}

 

Share this post


Link to post
Share on other sites

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;
  }
}

 

Share this post


Link to post
Share on other sites

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;
}

 

  • Like 1

Share this post


Link to post
Share on other sites
$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/

  • Like 2

Share this post


Link to post
Share on other sites

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

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.

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