0

I have a table states and fields are id,country_id,state_code,state_name.Now I want to validate that same country doesn't have a same state_code and same state_name in a table.

I tried but it's not working.

In my Controller :

    $validator = State::validator($request->all(), $id); 
    if ($validator->fails()) {
        return redirect()->back()
            ->withErrors($validator->getMessageBag())
            ->withInput($request->all());
    } 

Here is my validation function in model :

protected function validator(array $data, $id)
{
    return Validator::make($data, [
        'country_id'               => 'required',
        'state_code'               => 'required',
        'state_name'               => 'required',
    ]);
}

How can I solved this without custom validation ?

4
  • For a little clarification; do you need a unique validation rule that checks the uniqueness of a combination of state_code and state_name? I would think that state_code would be unique enough on its own, and you shouldn't have to worry about state_name. Commented Sep 18, 2019 at 14:33
  • country Romania,state Gorj has same code GJ for Gujarat state Commented Sep 18, 2019 at 14:36
  • Ah, I see; good example. In that case, consider something like a slug column, which is a lower-cased, - separated string of "unique" parts, like ro-gj-gorj and ro-gj-gurarat; constructed from country_code, state_code, state_name, and check uniqueness of that. Commented Sep 18, 2019 at 14:40
  • sure ll try and let u know if it helps. Commented Sep 18, 2019 at 14:43

2 Answers 2

1

First of all, your validation rules are only checking if these fields are present in the request. There is no validation happening for a value exists in database. First of all, you might wanna go through the documentation for the rule_exists. Secondly, you may have to update the query for this rule as per the documentation


use Illuminate\Validation\Rule;

Validator::make($data, [
    'email' => [
        'required',
        Rule::exists('staff')->where(function ($query) {
            $query->where('account_id', 1);
        }),
    ],
]);

Here you can pass additional query parameters.

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

2 Comments

of-course i know that it is checking only the required field, As i don't want to confuse with my code.
so did you try rule_exists with your custom query?
0

You do not need a custom validation for that one, laravel has a unique column validation

$this->validate($request, [
    'country_id' => 'required',
    'state_code' => 'required|unique:states,state_name',
    'state_name' => 'required|unique:states,state_code',
]);

This way you know for sure that column values in state_code will be unique compare to state_name for more check the docs

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.