Jump to content

Using hooks to populate a field, how do you stop an error message


cb2004
 Share

Recommended Posts

I am using hooks to populate some required field values. The populating is done, but I get an error message if it is a required field regardless if it has been populated. Which hook should I use to save the values, then the page displays without an error?

Cheers.

$pages->addHookAfter("save", function($event) {
	$page = $event->arguments(0);

	// apply job title if a quote is selected, apply customer address if customer selected
	if ($page->template->id == 48) {
		if ($page->page_quote && empty($page->title)) {
			$page->setAndSave("title", $page->page_quote->title);
		}

		if ($page->page_customer && empty($page->textarea_1)) {
			$page->setAndSave("textarea_1", $page->page_customer->textarea_1);
		}
	}
});
Link to comment
Share on other sites

hi cb,

i think addhookafter "saveReady" should be fine.

and just set the values, the save will take place afterwards:

// change this
$page->setAndSave("title", $page->page_quote->title);

// to that
$page->title = $page->page_quote->title;
  • Like 2
Link to comment
Share on other sites

  • 1 month later...

Ben provided the correct hook here:

https://github.com/ryancramerdesign/ProcessWire/issues/1832

So we end up with:

$pages->addHookBefore("ProcessPageEdit::processInput", function($event) {
	$page = $event->object->getPage();

	// apply job title if a quote is selected, apply customer address if customer selected
	if ($page->template->id == 48) {
		if ($page->page_quote && empty($page->title)) {
			$page->setAndSave("title", $page->page_quote->title);
		}

		if ($page->page_customer && empty($page->textarea_1)) {
			$page->setAndSave("textarea_1", $page->page_customer->textarea_1);
		}
	}
});
  • Like 3
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...