Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

form or field validation #53

Open
anthonymanzo opened this issue Apr 13, 2017 · 7 comments
Open

form or field validation #53

anthonymanzo opened this issue Apr 13, 2017 · 7 comments

Comments

@anthonymanzo
Copy link

Hi,
I'm wondering how to implement a custom validation (unique value for a particular field) with the form or collection component.
I'm guessing you have done this before, so I don't want to reinvent this process.
Thanks!

@anthonymanzo
Copy link
Author

Looking at the client/lib/form_utils.js I see that you have a placeholder for a custom validation callback:
this.validateForm = function(formObject, validationCallback, errorCallback, submitCallback) {
However, I don't see how to pass this through any of the dataview or form options.
Any help?

@perak
Copy link
Owner

perak commented Apr 14, 2017

Hi Anthony, unfortunatelly no way to pass custom validation to the kitchen. But You can do something server-side: in collection.before_insert_code do your validation and throw new Meteor.Error(...) on error. On submit, error message will be shown in form header (form field will not be marked red and focused).

P.S. all issues you reported are usefull issues - "must have" features, and when I finish this marathon (visual UI designer) and started improving quality of generated code, first thing I will do is to fix issues you reported. Thank you!

@anthonymanzo
Copy link
Author

anthonymanzo commented Apr 14, 2017 via email

@anthonymanzo
Copy link
Author

anthonymanzo commented Apr 17, 2017

Hi Perak,
You're suggestion to use the before insert (and consequently for my use case 'update') collection hooks worked.
I just put the following code in my kitchen file within the collection object:
"before_insert_code":" var orgExists = Orgs.findOne({org:doc.org_short_name}); if(typeof orgExists !== 'undefined'){ throw new Meteor.Error('Error','Org Short Name: '+doc.org_short_name+' already in use.'); } else { return doc; }", "before_update_code":" var postedOrg = modifier.$set.org_short_name; console.log('Posted Org Short Name:'+postedOrg); var orgExists = Orgs.findOne( {$and:[{org_short_name:postedOrg},{_id:{$ne:doc._id}}]} ); if(typeof orgExists !== 'undefined'){ throw new Meteor.Error('Error','Org Short Name: '+postedOrg+' already in use!'); } ",

@perak
Copy link
Owner

perak commented Apr 17, 2017

Glad you solved this 👍

I added two cards to my Trello board:

  • add custom validation mechanism

  • add "unique" property to field and auto generate code (check unique).

@anthonymanzo
Copy link
Author

Hi,
Something else related to the forms - it would be helpful to automatically add a 'required' class to the input groups having a required field. That way, you could also display a '*' at the end of the label for this required field by putting in css code like this:
div.required > label:after{content:'*';}

@anthonymanzo
Copy link
Author

Selecting required fields and styling those labels only without having the input and label directly adjacent is not possible with current CSS. I had to create the selector at the div level instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants