Volka Dimitrev
Volka Dimitrev

Reputation: 387

Unique field validation issue in laravel

I'm trying to validate a unique entry in my laravel app

following is my validation array,

$website = $websiteModel->find($id);

        $this->validate($request, [
            'subDomainName' => ['required','regex:/^[A-Za-z0-9 ]+$/'],
            'subDomainSuffix' => ['required'], 
            'packageType' => ['required'],
            'themeid' => ['required'],
            'lang' => ['required'],
            'user' => ['required'],
            'domain' => [
                'required',
                'string',
                'min:2',
                'max:255',
                Rule::unique('apps')->ignore($website)
            ],
        ], $request->all());

My validation working properly BUT,

When i tried to enter a duplicate value for my domain field, It get validated properly but not showing the error message, saying sorry the name is already exists...

<input type="text" id="domain" class="form-control" name="domain" >
{!! $errors->first('domain', '<span class="help-block" role="alert">:message</span>') !!}

Here in this span it shows nothing but in the common error message area it shows sorry the form cannot be updated... So how can I validate the field properly and display the relevant error message

Upvotes: 1

Views: 800

Answers (4)

Adam Pery
Adam Pery

Reputation: 2102

Do something like this:

On insert request use

'domain' => [
   ...
   'unique:websites,domain'
]

On update request use

'domain' => [
   ...
   "unique:websites,domain,{$this->website->id}"
]

Or

'domain' => [
   ...
   Rule::unique('websites', 'domain')->ignore($this->website)
]

Upvotes: 1

PHP Geek
PHP Geek

Reputation: 4033

please try this one . it helps to solve your problem

use exception and validator in top of the file

use Exception;
use Validator;

$rules = [
                'subDomainName' => 'required|unique:sub_domain_name',
            ];

            $validator = Validator::make($request->all(), $rules, $message);
            if ($validator->fails()) {
                throw new Exception(implode('\n', $validator->errors()->all()));
            }

sub_domain_name : this is database column name

Upvotes: 0

Khairu Aqsara
Khairu Aqsara

Reputation: 1310

don't you need to pass duplicate column in ignore Rule To instruct the validator to ignore the website domain, except for it self ? for example like

Rule::unique('apps')->ignore($website->id)

Upvotes: 0

Mohammad Hosseini
Mohammad Hosseini

Reputation: 1807

You passed $request->all() as validation messages. Please Try:

$website = $websiteModel->find($id);

        $request->validate([
            'subDomainName' => ['required','regex:/^[A-Za-z0-9 ]+$/'],
            'subDomainSuffix' => ['required'], 
            'packageType' => ['required'],
            'themeid' => ['required'],
            'lang' => ['required'],
            'user' => ['required'],
            'domain' => [
                'required',
                'string',
                'min:2',
                'max:255',
                Rule::unique('apps')->ignore($website)
            ],
        ]);

Upvotes: 0

Related Questions