Jump to content
VeiJari

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

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

Share this post


Link to post
Share on other sites

I believe what you are looking for might be answered here....

If not, please elaborate and someone will jump in and help.

Share this post


Link to post
Share on other sites
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...") ?

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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 = '';
	}
});

 

Share this post


Link to post
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?

Share this post


Link to post
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.

  • Similar Content

    • By benbyf
      Hi, Looking to create form elements on a page–some input with a colection of form inputs and the appropriate labels and variables for that input. I've used ProForms in the past and rolled out my own when creating simply one off forms, but I wonder if anyone has found a good way of allowing form creation on page editing so that clients can adhocly make and edit forms?
      Thanks
    • By EyeDentify
      Hello Dear PW Gurus.

      Hope you fair well in these Corona Crisis times.

      Anyhow, i have a problem with a Cryptic error message that shows up when i am trying to Delete images out of a Images field.
      The Error message does say a little but it does not make sense to me why i can´t delete the images because of it.

      Is it some permission issue perhaps?

      I will attach screenshots of it and what i did prior to it.
      I am from sweden so ignore the funny words here and there.

      Hope you all can send me on the right track.
      i am running PW 3.0.139 on this install.

      Step1:

      Step 2: Select the images with the trashcan symbol

       
      Step 3: Cryptic Error message

      I am scratching my head on this one, have not seen this before.

      Thankfull for all help.
      /EyeDentify
    • By Mithlesh
      Changing it to null
×
×
  • Create New...