Jump to content

Form validation in a process module: Stay on the same page if there are errors.


Recommended Posts

Sorry to flood the module dev forum lately with questions, but I thought I'd better create separate threads for each question (they're somewhat related, though not quite the same issues).

I built a simple module that allows users to create new pages. That all works all quite nicely, but I noticed some differences how PW handles validation.

I created a public function ___executeAddNewFooPage() in my module, which takes the form values from the main ___execute() function. What I would like to do though, is the same behavior like in a module configuration screen: Stay on the same page if there are errors (form not valid). I can detect invalid fields just fine in  ___executeAddNewFooPage() and link back, display red errors at the top, but then each field, which is not a text-based field, loses the user's values, i.e. selects are reset, text + textarea field values are intact. I tried setting the form action to itself, i.e. "./" instead of "./addnewfoopage", but that doesn't change anything.

What I would like to achieve is that the user stays on the page, and sees field errors like e.g. in the WireMail module config screen:


I've searched the forums far and wide, but didn't find any related infos. If someone can enlighten me, I'd appreciate it very much.

Link to comment
Share on other sites

Set the form action to the be the same URL that renders the form. Or just don't set the action for the form because the current URL is the default action. Look for the name of the submit button ("submit" is the default) in $input->post and process the form input when it is present.


public function ___execute() {
	$input = $this->wire('input');
	$modules = $this->wire('modules');

	/** @var InputfieldForm $form */
	$form = $modules->get('InputfieldForm');

	/** @var InputfieldText $f */
	$f = $modules->get('InputfieldText');
	$f->name = 'greeting';
	$f->label = 'Greeting';

	/** @var InputfieldText $f */
	$f = $modules->get('InputfieldText');
	$f->name = 'name';
	$f->label = 'Name';
	$f->required = true;

	/** @var InputfieldSubmit $f */
	$s = $modules->get('InputfieldSubmit');

	// Process input if form was submitted
	if($input->post('submit')) {
		// Core form processing (required, etc)

		// Custom processing
		$greeting = $form->getChildByName('greeting');
		if($greeting->value !== 'Hello') {
			$greeting->error('Please say "Hello"');

		// Redirect (Post/Redirect/Get pattern)

	return $form->render();


  • Like 2
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...