Skip to content

Conversation

@crlcu
Copy link
Contributor

@crlcu crlcu commented Dec 3, 2018

Make the following methods public:

  1. Illuminate\Validation\Concerns\ValidatesAttributes

    • parseTable
    • getQueryColumn
    • requireParameterCount
  2. Illuminate\Validation\DatabasePresenceVerifier

    • table
  3. Illuminate\Validation\Validator

    • getPresenceVerifierFor

Making these methods public will allow users write some nice custom validation methods that implies database checks, such as this one:

In this example the user is specifying the table(customers), the column to search(name), the percentage(85) and how many records to inspect(5).

$rules = ['name' => ['required', 'similarity:customers,name,85,5']];
Validator::extend('similarity', function ($attribute, $value, $parameters, $validator) {
    $validator->requireParameterCount(3, $parameters, 'similarity');

    [$connection, $table] = $validator->parseTable($parameters[0]);
    $percent = $parameters[2];
    $limit = $parameters[3] ?? 5;

    // The second parameter position holds the name of the column that needs to
    // be verified as unique. If this parameter isn't specified we will just
    // assume that this column to be verified shares the attribute's name.
    $column = $validator->getQueryColumn($parameters, $attribute);

    // The presence verifier is responsible for counting rows within this store
    // mechanism which might be a relational database or any other permanent
    // data store like Redis, etc. We will use it to determine uniqueness.
    $verifier = $validator->getPresenceVerifierFor($connection);

    $similarities = $verifier->table($table)->where($column, 'like', '%' . str_replace(' ', '%', $value) . '%')
        ->limit($limit)
        ->get()
        ->filter(function ($item) use($percent, $column, $value) {
            $found = 0;
            $similar = similar_text($item->$column, $value, $found);

            return $found >= $percent;
        });

    return count($similarities) == 0;
});

@mfn
Copy link
Contributor

mfn commented Dec 3, 2018

Can you please add a test making use of these newly public methods?

@driesvints driesvints changed the title Change some methods related to validation from being protected to being public [5.7] Change some methods related to validation from being protected to being public Dec 3, 2018
@GrahamCampbell GrahamCampbell changed the title [5.7] Change some methods related to validation from being protected to being public [5.8] Change some methods related to validation from being protected to being public Dec 3, 2018
@GrahamCampbell GrahamCampbell changed the base branch from 5.7 to master December 3, 2018 12:48
@taylorotwell taylorotwell merged commit 722d02f into laravel:master Dec 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants