miguelalarcos / autoform-extension

An extension for AutoForm that permit you to use typeahead and formated dates.
4 stars 0 forks source link

ForeignKey Validation #4

Closed ccorcos closed 10 years ago

ccorcos commented 10 years ago

Looking at the code, it doesn't seem that the ForeignKey is validated is it?

miguelalarcos commented 10 years ago

You are right, it doesn't. I have been thinking about that before, and I'm not sure how to do it. Imagine the next scenario: a webapp multi-user. If you want to be sure the user inserts a reference that belongs to him, you maybe have to check for the userId. What do you think?

ccorcos commented 10 years ago

Well, the custom validation is the simplest way of ensuring proper validation. But I was just thinking something as simple as making sure it is a valid object in another collection:

if (Authors.findOne()) { return true}

Another field could be something like noRepeats which would ensure that in an array of authors, there are no repeats. This logic could also be used with selectize to make sure the correct options are displayed...

I'd love to help by the way, I'm just having trouble digging into the auto form, collection2, and simple-schema code...

miguelalarcos commented 10 years ago

I've coded the next:

window = @

SimpleSchema.addValidator ->
    if Meteor.isServer
        if @definition.references?           
            references = @definition.references
            a = references.split('.')
            collection = a[0]
            collection = window[collection]
            if collection.findOne @value
                return true
            else
                return 'Error Foreign Key.'
        else
            return true
    else
        true

Is it more or less what you expect? You can see it in the selectize branch.

ccorcos commented 10 years ago

exactly