Jump to content

Suppress required field errors in ProcessPageEdit?


schwarzdesign
 Share

Recommended Posts

Short question: I'm looking for a way to suppress the error messages that appear on the edit page (ProcessPageEdit) after saving a page while required fields are missing. I also want to remove the flagged status for those pages as well.

Longer context: I'm building a very large template with many fields that are filled through a couple of custom forms in the frontend. We generate the frontend forms mostly based on the field settings from ProcessWire, so there are a bunch of required fields. However, we still allow site admins to manually edit pages in the backend. The problem is that if half the data is still missing, saving the page will generate a bunch of error messages because of the required fields. I would like to be able to suppress those warning, as well as the "flagged" status the page gets after saving it with invalid data, so that site admins can edit those pages without having to worry about the required fields.

Is there a hook I can use? I had a quick look at ProcessPageEdit, but couldn't figure out yet where I need to hook in order to clear out the error messages after they are registered.

I'm grateful for every advice. Thanks!

Link to comment
Share on other sites

Well, frankly speaking, I don't quite see the issue here. Required is required, so some warning messages make sense. Why do you define certain fields as required in the first place?

You can try to be creative with other kinds of setups / workflow, e.g. nothing is required initially (on save). But you could define some fields as required only once the page gets published. There are probably methods and hooks that let you do that, e.g. https://processwire.com/api/ref/pages/publish-ready/

You could also "fake" a required field in the backend, e.g. use labels like "Phone*" and / or use a yellow background (UIKit field settings). In other words - make them look super-important for the users visually.

Here's a rather old thread that is somewhat related: https://processwire.com/talk/topic/4476-validating-field-before-page-save/

 

Link to comment
Share on other sites

5 minutes ago, dragan said:

Well, frankly speaking, I don't quite see the issue here. Required is required, so some warning messages make sense. Why do you define certain fields as required in the first place?

We use the field settings (required, min / max length, description, notes) mostly to display our custom-built front-end forms. So by marking a field as required, it will be rendered as such in the front-end form (frontend validation) and validated accordingly upon submission (backend validation). We do it this way because we have a lot of fields for this template – to make it easier to manage those, we just use the built-in field settings.

Of course, the obvious solution would be to just not have those fields as 'required' fields. But that would mean we'd need another layer of settings to select required fields for the frontend-forms, or hard-code that into the template, both of which aren't good solutions. In fact, the normal page editor is much less important than the front-end forms in this case. The users won't see it, only site admins – and it's annoying for the latter. We could live with that, but it would be nice to suppress those warnings, because you need to scroll down two screens to see the actual content because of all the warnings and they have no meaning for us in this case.

Link to comment
Share on other sites

Well, I just found processInputErrorAction() in ProcessPageEdit.module, but that is a non-hookable method. A related function seems to be

     * // Disable change tracking
     * $page->setTrackChanges(false);

which is in wire/core/Wire.php

I would maybe try to work with "Required only if custom PHP returns true" and add some logic like "required only if data is edited in the frontend", but I'm not even sure that's possible.

A quick test with

$isAdmin = $this->page->rootParent->id == 2;
return !$isAdmin;

seems to work... (thanks to @LostKobrakai's post here)

  • Like 1
Link to comment
Share on other sites

This seems to do the job:

$wire->addHookAfter('ProcessPageEdit::buildFormContent', function(HookEvent $event) {
	/* @var InputfieldWrapper $wrapper */
	$wrapper = $event->return;
	// Do some check on user role
	if($event->wire('user')->isSuperuser()) {
		$inputfields = $wrapper->getAll();
		foreach($inputfields as $inputfield) {
			$inputfield->required = 0;
		}
	}
});

 

  • Thanks 1
Link to comment
Share on other sites

Thanks @Robin S, that did the trick! @dragan Thank you for your suggestions as well!

I adjusted the code a bit to only trigger during POST requests; this way, the form will still display the * required markers, but the form will validate successfully despite the missing fields. Here's my final code, if someone needs it:

// site/template/admin.php

$wire->addHookAfter('ProcessPageEdit::buildFormContent', function (HookEvent $e) {
    $formwrapper = $e->return;
    $page = $e->object->getPage();
    $userRoles = wire('user')->roles;
    $isPrivilegedEditor = $userRoles->has('superuser') || $userRoles->has('editor');
    $isPost = wire('input')->requestMethod() === 'POST';
    if ($isPost && $page->template->name === 'client' && $isPrivilegedEditor) {
        foreach ($formwrapper->getAll() as $inputfield) {
            $inputfield->required = false;
        }
    }
});

require($config->paths->adminTemplates . 'controller.php'); 

One thing I got stuck on is that the hook needs to come BEFORE the require statement at the bottom. Otherwise, the hook would be added to late and wouldn't fire at all.

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