Jump to content

How to detect if a template form has errors in it?


VeiJari
 Share

Recommended Posts

Hello forum. I'm trying to figure out how to detect if current form has errors after saving it in pw admin page. This is so that our event location isn't added in database if we detect errors. But I can't seem to find a correct way to detect errors? I only found the wire()->errors() and it always triggers even if I don't have any errors. 

How can I detect errors in a form?

By error I mean $page->error('this is the red error');

Link to comment
Share on other sites

  • 2 months later...
On 5/31/2019 at 11:15 AM, VeiJari said:

current form

Where? In the frontend or backend?

If you're doing forms in the frontend, you have to write your own validation logic.

Or do you refer to field dependencies in the backend ("required only if...") ?

Link to comment
Share on other sites

On 8/8/2019 at 4:59 PM, dragan said:

Where? In the frontend or backend?

If you're doing forms in the frontend, you have to write your own validation logic.

Or do you refer to field dependencies in the backend ("required only if...") ?

Sorry for the confusing question. Let me clarify.

I'm talking about how to detect if I get a session error on backend in hooks.

Also is there a way for us to give an error considering only a inputfield in backend, instead of just getting a session error?

 

Thanks

Link to comment
Share on other sites

On 8/8/2019 at 4:59 PM, dragan said:

Where? In the frontend or backend?

If you're doing forms in the frontend, you have to write your own validation logic.

Or do you refer to field dependencies in the backend ("required only if...") ?

And another clarification whan I mean by an error, if you make a field required in the backend, it prompts an error above the field ('this field is required"), this is the error I want to make from the api.

Thank you

Link to comment
Share on other sites

There are a couple of different questions here.

On 5/31/2019 at 9:15 PM, VeiJari said:

I'm trying to figure out how to detect if current form has errors after saving it in pw admin page.

Check if saved page has errors:

$wire->addHookAfter('Pages::saveReady', function(HookEvent $event) {
	/* @var Page $page */
	$page = $event->arguments(0);
	// Find out if the page being saved has any errors
	$has_errors = $page->hasStatus(Page::statusFlagged);
	// Do something accordingly...
});

 

45 minutes ago, VeiJari said:

if you make a field required in the backend, it prompts an error above the field ('this field is required"), this is the error I want to make from the api.

You can set custom error messages as follows. This is for a text inputfield - adjust to suit the type of inputfield in question:

$wire->addHookAfter('InputfieldText::processInput', function(HookEvent $event) {
	/* @var InputfieldText $inputfield */
	$inputfield = $event->object;
	$field = $inputfield->hasField;
	// Return early if the field name doesn't match
	if(!$field || $field->name != 'text_1') return;
	// If some condition is or isn't met
	if($inputfield->value && $inputfield->value !== 'foo') {
		// Show an error for the inputfield
		$inputfield->error('The value of this field must be empty or foo');
		// Maybe clear the inputfield value
		$inputfield->value = '';
	}
});

 

Link to comment
Share on other sites

On 8/10/2019 at 2:20 PM, Robin S said:

Check if saved page has errors:


$wire->addHookAfter('Pages::saveReady', function(HookEvent $event) {
	/* @var Page $page */
	$page = $event->arguments(0);
	// Find out if the page being saved has any errors
	$has_errors = $page->hasStatus(Page::statusFlagged);
	// Do something accordingly...
});

 

Hi @Robin S! I'm a bit confused with your solution. I tried to use your code, it threw me "Uncaught Error: Undefined class constant 'statusFlagged'".

I looked at wire/Page.php, and it doesn't seem to have statusFlagged: 

static protected $statuses = array(
  'locked' => self::statusLocked,
  'systemID' => self::statusSystemID,
  'system' => self::statusSystem,
  'draft' => self::statusDraft,
  'versions' => self::statusVersions,
  'temp' => self::statusTemp,
  'hidden' => self::statusHidden,
  'unpublished' => self::statusUnpublished,
  'trash' => self::statusTrash,
  'deleted' => self::statusDeleted,
  'systemOverride' => self::statusSystemOverride, 
  'corrupted' => self::statusCorrupted, 
);

Am I looking at the wrong place, or has it been renamed?

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

×
×
  • Create New...