auraphp/Aura.Filter

custom rules not applying when field is blank

Closed this issue · 10 comments

php- commented

Hello @pmjones
I was trying to make 'required-if' rule, where the field would be required only in some conditions, but because of the fact that rules are not called to blank fields there is no workaround about this issue.

Thanks for help
George

Hi,

Could you show some code what you have tried to implement or can you send a unit test to prove that there is a failure ?

Thank you

php- commented

Ill explain in short, add new validation rule and just return true, next create subject filter class, and do test the result on blank valued input. the result will be false. that means that validation is not called and returns false by default because of having blank value

php- commented

@harikt any updates here?

Finally I invested sometime to understand what you were saying. ( An example would have helped though, but as you failed to provide :( ) .

First I thought this was simple when you don't apply the blank rule and rule will be applied on it. But further testing with code I understood it is not as what I expected it to be.

Tested code below .

<?php
require __DIR__ . '/vendor/autoload.php';

class ValidateHex
{
    public function __invoke($subject, $field)
    {
        echo "\n Subject : " . $subject->$field;
        // done!
        return true;
    }
}

use Aura\Filter\FilterFactory;

$validate_factories = array(
    'test' => function () { return new ValidateHex(); },
);

$filter_factory = new FilterFactory(
    $validate_factories
);

$filter = $filter_factory->newSubjectFilter();

// the 'color' field must be a hex value of no more than 6 digits
$filter->validate('color')->is('test')->asSoftRule();

$subject = [
	'color' => ''
];

$success = $filter->apply($subject);

$failures = $filter->getFailures();
var_dump($failures->getMessages());

var_dump($success);

The underlying problem lies at

if ($this->subjectFieldIsBlank($subject)) {
return $this->allow_blank;
}
.

I don't have a solution for this right now. But that is a nice bug 👍 .

Thank you for the report.

UPDATE : I will try to invest some time tomorrow to write a test and see if I can fix this. I have some ideas / work around for this. Just wanted to make sure other tests run 👍 .

Oh oh and this seems related to #119 the one @jakejohns created.

php- commented

@harikt ya u got it, thanks, and it will not work when that field is not present at all.
This is important because what I'm trying to achieve is for example: there are required fields on insert, but those fields can be omitted from user when updating data.
Hope we get solution soon.
Best Regards

I am sorry that I can't make a solution for your problem.

I have created a failed test #137 so in case someone find it interesting can play around the same.

I actually modified the PR #137 , so if you pass something as

->skipBlankRule();

It will skip checking whether the field is blank or not.

I am not sure if that is a right way or not. I wished if we could move the checking blank as a rule itself. But that will break some API I believe.

I am open to discussion about the same. Not very much convinced with the names. ie also open to discussion.

Will wait for others before we merge.

In the mean time you can use the branch via composer.

Hope that helps! .

Fixed via #137