0

I have to following customized validation

Validator::extend('uniqueFirstAndLastName', function ($attribute, $value, $parameters, $validator) {
        $count = DB::table('people')->where('firstName', $value)
            ->where('lastName', $parameters[0])
            ->where('id', '<>', $parameters[1])
            ->count();

        return $count === 0;
    });

How do I call this rule with the 2nd parameter since $parameters expects an array?

This does not work:

return Validator::make($data, [
            'firstName' => "uniqueFirstAndLastName:{$data['lastName]},{$data['id']}"
]
6
  • Your validation, even if it would work, will produce false positives. Why? Because you're delegating uniqueness check to PHP and PHP (or any other language) cannot guarantee uniqueness. Only the underlying database can. That means - you need to place unique constraints on desired column(s) and perform an insert. If you get exception with code 23000 then the record exists and you notify the user that those credentials have been taken. Create a binary column that contains a hash of first and last name, make it unique, simply insert and catch the exception. Everything else will fail. Commented Feb 8, 2017 at 17:00
  • I'm using laravel validation and I prefer to make the checking on the php side rather with an index on these 2 fields. This validation rule works great. the only missing part is that it needs to exclude the current record being checked with the rest of the table. stackoverflow.com/questions/42115930/… Commented Feb 8, 2017 at 17:05
  • 2
    Your preference doesn't make something what you need it to be. You'll end up with duplicated names in the end, even on a low-traffic website - I've seen it happen so many times and the solution is always to constrain the records on database side. If you don't want to do it, that's another pair of sleeves. I wrote the comment to warn you, but since you think it's wise to proceed - let's wait for some nice soul to give you a gun to shoot your foot :) good luck with this! Commented Feb 8, 2017 at 17:12
  • Looks like you are trying to make a bicycle out of a car It should be mentioned as well that there is no need to pass $data params into validator rule directly - they are already there in $validator->getData() Commented Feb 8, 2017 at 20:28
  • I don't want tu be rude, but if someone shows a direction where to go, you would look at his finger. My question isn't about unique, which you've been focusing since the beginning. My question is about : how to call a custom validation function with an array for the argument 'parameters'. Commented Feb 9, 2017 at 19:11

2 Answers 2

1

As Mjh said, it's always a got practice to add a constrain on the database.

however, you might try this approach,

Validator::make($data, [
    'lastName' => [
        'required',
        "unique:people,lastName,{$ignoredUserId},firstName,{$data['lastName']}'"
    ],
]);

Reference: https://laravel.com/docs/5.3/validation#rule-unique

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer, but after trying your code, it shows the following error: ErrorException in ValidatesAttributes.php line 721: Undefined offset: 1 And as other people, my question isn't about unique rule, please read again the original question.
0

After trying a var_dump inside the custome validator function, I was able to see the content of its parameters.

The answer to this question is simply separating values by comma like so:

return Validator::make($data, [
            'firstName' => "uniqueFirstAndLastName:{$data['lastName]},{$data['id']}"
]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.