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 louisstephens
      So I ran into a very strange issue today. I have a template with a pagetable and I went to add an item to it, when I went to select an image (for an image field) the page instantly threw up an error
      "ProcessPageSearchLive: No search specified"
      The page's content also switched to the image attached. This all worked perfectly last week (local mamp box). Has anyone experienced this before, and how did you solve it?
       

    • By usualCommission
      Hey all. I'm having an issue with the new custom page classes feature which is a fantastic tool.
      I am running into a recursion issue when attempting to call a custom page class. I have a class called BlogPostPage.php (for blog-post.php) which contains a method called getSummary(). That method gets a summary field or truncates the body in it's absence. I've attached photos of the template code, custom page class code (which I've simplified for testing), and the PW output error.
      There is no recursion in the getSummary() method. This error occurs whether I output multiple blog posts in a loop or if I output one blog post with no looping in my template code.

      In use:
      ProcessWire 3.0.164 dev
      PHP 7.3.13

      I am also using the Template Engine Twig module which has not caused any errors or issues thus far.
      Many thanks!



    • By Pip
      Hi everyone, 
      Is there a way for us to replicate the "http://mydomain.com/processwire/page/edit/?id=xxx" and change the template to match my site template? 
      I'm terribly happy with the admin / backend page edit. It covers literaly everything I want to empower my non super admin user in updating pages such as validation, repeater management, file upload. 
      I wish not to allow the users to see the backend for both security and aesthetic reasons. 
      Thanks and hope to hear from you soon. 
       
    • By neonwired
      I'm hoping someone has seen this before. There doesn't appear to be an issue with the user info.

       

    • By EyeDentify
      Hello dear PW gurus.

      I have stumbled over a strange error that i all of sudden got when trying to upload an image to a images field on a page.
      There where images allready stored in the field that i wanted to keep, but during the upload the error apear and after that all images are gone from the field and i can´t upload any, i just get the error every time.
      I am running ProcessWire 3.0.153 dev.
      Update:
      After looking in the assets folder i find the folder for the page and the image files seems to be there including the ones i tried to upload when the error occured.
      But they don´t show up in the images field in the page editor.
       
      The error reported:
       
      SQLSTATE[01000]: Warning: 1265 Data truncated for column 'ratio' at row 1 And here is a screenshot of the event:

      The TracyDebugger Error reporting:

      I hope you fine folks could point me in a direction.
      But it seems our old pal set_time_limit() is back.

      Regards, EyeDentify
×
×
  • Create New...