Waavi / Sanitizer

Data sanitizer and form request input sanitation for Laravel 5.
MIT License
428 stars 88 forks source link

Add a filter for sometimes rules #32

Open Trixpua opened 5 years ago

Trixpua commented 5 years ago

I think is interesting add a filter to check "sometimes" laravel rule and just apply the filter if the field isset, because this way if field is not set raise the error that the field is required.

norbybaru commented 5 years ago

Is there any progress with regards to this issue? I strongly believe that filters should only be applied to inputs that have been passed in the request instead of retuning null for inputs that haven't been passed through.

Trixpua commented 5 years ago

I think is not difficult to implement this, just need a check if is set the field in request before apply the sanitize.

thomas-obernberger commented 5 years ago

i got it working by filtering the filters array before it is returned

    public function filters()
    {
        return collect([
            'first_name'  => 'trim|escape|capitalize',
            'last_name'  => 'trim|escape|capitalize',
            'email' => 'trim',
        ])->filter(function ($value, $key) {
            return $this->input($key) !== null;
        })->toArray();
    }