zhorton34 / vuejs-form

Vue Form with Laravel Inspired Validation and Simply Enjoyable Error Messages Api. (Form Api, Validator Api, Rules Api, Error Messages Api)
https://cleancode.studio
MIT License
41 stars 6 forks source link

Question: dot notation for rules? #17

Open shopapps opened 4 years ago

shopapps commented 4 years ago

Hi,

Does the validation rules support objects for the fields and dot notation for the rules?

Example:


        createForm(){
              this.bank_details = {
                      'account_name': '',
                      'account_number': '',
                      'sort_code': '',
              };

              this.form = form({
                    bank_details: this.bank_details,
                })
                .rules({
// this causes "cannot get length of undefined" errors 
                    'bank_details.account_name': 'string|min:5|required',
                    'bank_details.account_number': 'number|min:7|max:8|required',
                    'bank_details.sort_code': 'number|min:6|max:6|required',
// this causes "cannot call split" errors 
//                    bank_details: {
//                        account_name: 'string|min:5|required',
//                        account_number: 'number|min:7|max:8|required',
//                        sort_code: 'number|min:6|max:6|required',
//                    }
                });
    }
}

error when trying to use the object method of rules is: Error in callback for watcher "form.data": "TypeError: rules.split is not a function"

zhorton34 commented 4 years ago

@shopapps Not as of yet. We have an issue for it here -- https://github.com/zhorton34/vuejs-form/issues/1.

Right now I'm over loaded at work and with the youtube channel, but plan on getting back to this issue hopefully sooner than later.

That being said, if you're in a hurry and need this solved ASAP I've already created the tested logic for dot notation in this repository (https://github.com/zhorton34/laravel-js-helpers) and just haven't had the time to implement the logic over to this repository for nested rules.

If you're interested, I'm more than willing to set up an outlined issue for implementing this feature using the data_get, data_set, and data_fill tested functions from thelaravel js helpers repository.

It'd be awesome to get some help copying some of that logic over into this repo and implementing this feature you're talking about. I really want it but just haven't had the time.

Here's a quick over view of each method that you'd be interested in from that repo that would be important for implementing dot notation rules. After you see the overview of functions already created below that'll help with dot notation for rules, let me know if you'd be interested and I'll set up the outline and make sure to be more active on this repo for any questions you come across.

No worries either way, if not -- I'll eventually be getting around to this feature, my schedule's just hectic right now so it may be a couple months before I personally have the time.

data_get: Get's nested data data_set: Sets nested data (Over riding preexisting values) data_fill: Sets nested data (Unless data is already set)

Examples


Data Get


Data Get Simple

data_get(family, 'family.members.brother.name');

Data Get Output

"drew"

Data Get Wildcard("*")

data_get(family, 'family.members.*.age'); 

Data Get Wildcard Output

[17, 25, 'thats not polite', 50];

Data Set


Data Set Simple

data_set(family, 'members.brother.age', 21);
data_set(family, 'members.brother.name', 'Everett');

Data Set Wildcard("*")

data_set(family, 'members.*.last_name', 'Smith');

Data Set Output

{
    members: {
        brother: {
            age: 21,
            name: 'Everett',
            last_name: 'Smith',
            hobbies: ['football', '"chillin"', 'video games']
        },
        sister: {
            age: 25,
            name: 'ash',
            last_name: 'Smith',
            hobbies: ['mothering babies', 'fitness', 'nutrition', '']
        },
        mother: {
            name: 'amy',
            last_name: 'Smith',
            age: 'thats not polite',
            hobbies: ['cleaning', 'nursing', 'hiking']
        },
        father: {
            age: 50,
            name: 'jon',
            last_name: 'Smith',
            hobbies: ['reading', 'sports', 'talking for seemingly ever', 'providing useful insights']
        },
    }
}

Data Fill


Data Fill Simple

data_fill(family, 'members.brother.age', 23); // wont do anything
data_fill(family, 'members.brother.name', 'Zak'); // wont do anything
data_fill(family, 'members.brother.birthday', 'April 22'); // will do something

Data Fill Wildcard("*")

data_fill(family, 'members.*.last_name', 'Horton'); // wont do anything
data_fill(family, 'members.*.nickname', 'Hortonion'); // will do something

Data Fill Output

{
    members: {
        brother: {
            age: 21,
            name: 'Everett',
            last_name: 'Smith',
            birthday: 'April 22',
            nickname: 'Hortonion',
            hobbies: ['football', '"chillin"', 'video games']
        },
        sister: {
            age: 25,
            name: 'ash',
            last_name: 'Smith',
            nickname: 'Hortonion',
            hobbies: ['mothering babies', 'fitness', 'nutrition', '']
        },
        mother: {
            name: 'amy',
            last_name: 'Smith',
            nickname: 'Hortonion',
            age: 'thats not polite',
            hobbies: ['cleaning', 'nursing', 'hiking']
        },
        father: {
            age: 50,
            name: 'jon',
            last_name: 'Smith',
            nickname: 'Hortonion',
            hobbies: ['reading', 'sports', 'talking for seemingly ever', 'providing useful insights']
        },
    }
}
luis-gmonkey commented 2 years ago

Is there any solution/workaround for this issue without changing the vuejs-form rep code?

zhorton34 commented 2 years ago

@luis-gmonkey Not at the moment, no