lunar icon indicating copy to clipboard operation
lunar copied to clipboard

extend validation rules

Open wychoong opened this issue 2 years ago • 8 comments

this is a proof of concept on customising validation rules in hub. Current only applied for Product's collection.

now you can define more rules in config

# in config/getcandy-hub/product.php
<?php

use GetCandy\Models\CollectionGroup;

return [
    # ... 

    'validation' => [
        'variant.sku' => 'required|unique|min:8',
        'collections.*.group_id' => [
            'required',
            'distinct',
        ],
        'collections' => ['required' => true, 'array', function ($attribute, $value, $fail) {
            $missing = [];

            $collections = collect($value)->pluck('id', 'group_id');

            $groups = CollectionGroup::whereIn('id', [1, 2])
                ->select(['id', 'name'])
                ->get()
                ->pluck('name', 'id');

            $missing = $groups->diffKeys($collections)->all();

            if (filled($missing)) $fail("Require Collection Group from: " . implode(', ', $missing));
        }],
    ]
];

result image

image

issue the main problem with the current implementation is unable pass in Model instance to those additional rules. it can be done but I don't think is good approach.

wychoong avatar Jul 29 '22 20:07 wychoong

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
getcandy-2-docs ✅ Ready (Inspect) Visit Preview Dec 14, 2022 at 2:49PM (UTC)
lunar-docs ✅ Ready (Inspect) Visit Preview Dec 14, 2022 at 2:49PM (UTC)

vercel[bot] avatar Jul 29 '22 20:07 vercel[bot]

@glennjacobs need your input on this

this is referring to this https://discord.com/channels/497326121441034240/916819091841486869/1001891823146520646

wychoong avatar Jul 29 '22 20:07 wychoong

@alecritson thoughts on this?

glennjacobs avatar Aug 01 '22 08:08 glennjacobs

@glennjacobs I'm not against this but don't feel like the config is the correct place. Due to some validation using closures (like in the example above) this means that Laravel wouldn't be able to serialise the config for optimisation.

Would it not be better to provide a method on the component that dev's can tap into at the component level? Something like:

ProductEdit::extendValidation([
    'name' => 'required|min:50',
]);

alecritson avatar Aug 03 '22 06:08 alecritson

@glennjacobs I'm not against this but don't feel like the config is the correct place. Due to some validation using closures (like in the example above) this means that Laravel wouldn't be able to serialise the config for optimisation.

Would it not be better to provide a method on the component that dev's can tap into at the component level? Something like:

ProductEdit::extendValidation([
    'name' => 'required|min:50',
]);

Fair point. Will look into this approach

wychoong avatar Aug 03 '22 06:08 wychoong

now can extend validation by using component method

ProductCreate::extendValidation(
            [
                'variant.sku' => ['required', 'min:8'],
                'collections' => ['required', 'array', function ($product) {
                    return function ($attribute, $value, $fail) use ($product) {
                        $missing = [];

                        $collections = collect($value)->pluck('id', 'group_id');

                        $groups = CollectionGroup::whereIn('id', [1, 2])
                            ->select(['id', 'name'])
                            ->get()
                            ->pluck('name', 'id');

                        $missing = $groups->diffKeys($collections)->all();

                        if (filled($missing)) $fail($product->translateAttribute('name') . " Require Collection Group from: " . implode(', ', $missing));
                    };
                }],
            ]
        );

$product is able to pass from component by using app()->call(...) refer: getExtendValidation method

wychoong avatar Aug 03 '22 17:08 wychoong

@wychoong It's looking promising, I can see it's still in draft, is there still work you want to do on it?

alecritson avatar Aug 10 '22 11:08 alecritson

@wychoong It's looking promising, I can see it's still in draft, is there still work you want to do on it?

hey. I think in terms of implementation it is done. I kept it as draft is to get feedbacks and since this opens up a lot more validation, we also need to add few more changes to display the error message. in

  • getSideMenuProperty hasError
  • @error in blade

wychoong avatar Aug 10 '22 12:08 wychoong

@wychoong should we have documentation for this?

glennjacobs avatar Sep 05 '22 08:09 glennjacobs

@wychoong should we have documentation for this?

added doc

wychoong avatar Sep 05 '22 09:09 wychoong

@wychoong should we have documentation for this?

added doc

Awesome. Just Alec's question and then I think this can be merged?

glennjacobs avatar Sep 05 '22 10:09 glennjacobs

@wychoong should we have documentation for this?

added doc

Awesome. Just Alec's question and then I think this can be merged?

hrmmm... you referring to this? image

not sure why its pending or should i click the resolve converstation?

wychoong avatar Sep 05 '22 10:09 wychoong

figured it out that i need to submit my "review" comment 😓

wychoong avatar Sep 05 '22 14:09 wychoong

@wychoong the VuePress config file has conflicts now. You have changed the indentation from 2 spaces to 4 spaces, any reason for this?

glennjacobs avatar Sep 13 '22 07:09 glennjacobs

@wychoong the VuePress config file has conflicts now. You have changed the indentation from 2 spaces to 4 spaces, any reason for this?

I think that was because of my VSCode does that, can you help on this as I’m away from computer till the weekend

wychoong avatar Sep 13 '22 10:09 wychoong

@glennjacobs it's ready for review

wychoong avatar Sep 19 '22 08:09 wychoong