Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to display an error message thrown from an Autoform MeteorMethod Call

Tags:

meteor

I have a quick form like so:

{{> quickForm schema=competitorSchema id="newCompetitorForm" type="method" buttonContent="Save Competitor" meteormethod="insertCompetitor" tid=tournament._id }}

This is in a modal popup BTW.

Now in the method code I had a defect and did not specify one of the required fields in the mongo insert operation so what happened was:

  1. Client side validation passed (cause I had populated all the required field in the form)
  2. When the actual insert occurred collection2 threw the proper error saying field X was missing.

This was thrown back to me and I catch this in my onError: auto form hook like so:

onError: function(operation, error, template) {
    if(error){
        alert(error);
    }
}

This could any general error - for any reason BTW.

Alert is not what I want to use here... cause it's not very nice. Collection2 throws "Error: X is required" but I don't really want to parse out the string field name since that's sorta brittle trying to match that with a form key since it seems like X is actually the label.

I can come up with a bunch of work arounds like populating a special div or popping a modal etc but what I really want is for auto form to handle this for me with a generic form level validation error (not a specific key).

Does anyone know if there a nice way built into auto form to display a form level (not field specific error) to the user? I did not see this in the docs.

I see how I can use addInvalidKeys etc for a specific field but what I want to do is use something like addInvalidKeys with no key so it displays a form level error message etc.

like image 426
DaveR Avatar asked Oct 20 '22 18:10

DaveR


1 Answers

You can return whatever you want from the method call return function, so you can return human readable messages as a string or object.

If Alert doesn't work for you, you can use a Modal to put up a nice message.

It's really not much code. You can populate a Session variable when there is an error and add a banner div of some sort.

If you want VALIDATION, then use a SimpleSchema with your autoform and it will work automatically, but that only validates the input matches the Schema, not backend insertion errors etc...

like image 62
MrE Avatar answered Nov 24 '22 03:11

MrE