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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Mithlesh
      Changing it to null
    • By EyeDentify
      I have been experimenting with the new $page->meta() method and find it useful.

      Once i figured out that the data i "save" with it is tied to the page where i called the method from.

      So this is not obvious at least not for me in the documentation:
      https://processwire.com/api/ref/page/meta/
       
      So i just wanted to share that revelation with the community so you don´t get as confused as i was.

      Happy Coding Everyone.
    • By stanoliver
      In the new page-meta-method (https://processwire.com/blog/posts/pw-3.0.133/#comments) there is an example how to ouput "world". 
      Could may someone give me a snippet of code so that the output is one of the colors (red, green, blue).
    • By Tyssen
      I have a page that contains a single ProFields table field and I want to display the contents of the table on the front end and then for logged in users, they can edit certain columns in the table.
      What I have at the moment is
      $out = '<form action="'.$page->url.'" method="post" > <table class="table"> <tbody>'; $count = 1; foreach($page->fieldName as $row) : $out .= ' <tr> <td><input type="checkbox" name="fieldName_'.$count.'_columnName"></td> </tr>'; if($input->post->submit) : $page->of(false); $page->set('fieldName_'.$count.'_columnName', $sanitizer->text($input->post->{fieldName_'.$count.'_columnName})); $page->save(); endif; $count++; endforeach; $out .= ' </tbody> </table> <button class="button" type="submit">Save</button> </form>'; The two problems I have are:
      I get an error trying from $sanitizer->text($input->post->{fieldName_'.$count.'_columnName}), not sure how to make that dynamic.  If I change the above to just a static value, e.g. $page->set('fieldName_1_columnName', 'Testing') and save the form, it's not saving the values to the database. Where am I going wrong?
    • By VeiJari
      Hello forum!
      I've yet again stumbled on a head-scratching situation. We have enabled the option on our articles template and events template that it skips the title adding part and goes straight to the form. This is what our customer wants. So when you add a new article or event it automatically names it temporary to "article-0000000" and same with event. Now the problem is that obviously after saving the form we want to change to page url or "name" to the title, like it's normally. 
      Now here's the code for the hook:
      wire()->addHookBefore("Pages::saved(template=tapahtuma|artikkeli)", function($hook) {
        $page = $hook->arguments(0);
        $newUrl = wire()->sanitizer->pageName($page->title); // give it a name used in the url for the page
        wire()->log->message($page->name);
        $page->setAndSave('name', $newUrl);
      });
      I get the correct page and the name and path changes when I log them, but when I try to save it. It just loads and then I get: 
      Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 262144 bytes) This happens in sanitizer.php
      and then another error: Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 262144 bytes) in Unknown on line 0
       
      What is happening? Am I not suppose to use sanitizer in this way? When we made a temporary page object in out other hook, the sanitizer worked perfectly.
      Thanks for the help!
×
×
  • Create New...