Fix domain name uniqueness during update, taking into account tld

This commit is contained in:
Deon George 2021-07-13 14:56:14 +10:00
parent 71d2faedb1
commit 53fc25612b
No known key found for this signature in database
GPG Key ID: 7670E8DC27415254
2 changed files with 13 additions and 4 deletions

View File

@ -3,6 +3,7 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Illuminate\View\View; use Illuminate\View\View;
@ -22,7 +23,15 @@ class ServiceController extends Controller
session()->flash('service_update',TRUE); session()->flash('service_update',TRUE);
$validation = $request->validate([ $validation = $request->validate([
'service.domain_name' => sprintf('required|unique:%s,domain_name,%d',$o->type->getTable(),$o->type->id), 'service.domain_name' => ['required',function($attribute,$value,$fail) use ($request,$o) {
if (Service\Domain::where('domain_name',$value)
->where('domain_tld_id',Arr::get($request,'service.domain_tld_id'))
->when($o->type,function($q) use ($o) { return $q->where('id','<>',$o->type->id); })
->count() > 0)
{
$fail('Domain already exists.');
}
}],
'service.domain_expire' => 'required|date', 'service.domain_expire' => 'required|date',
'service.domain_tld_id' => 'required|exists:ab_domain_tld,id', 'service.domain_tld_id' => 'required|exists:ab_domain_tld,id',
'service.domain_registrar_id' => 'required|exists:ab_domain_registrar,id', 'service.domain_registrar_id' => 'required|exists:ab_domain_registrar,id',

View File

@ -12,7 +12,7 @@
</div> </div>
<select class="form-control col-3" name="service[domain_tld_id]"> <select class="form-control col-3" name="service[domain_tld_id]">
@foreach(\App\Models\DomainTld::active()->orderBy('name')->get() as $oo) @foreach(\App\Models\DomainTld::active()->orderBy('name')->get() as $oo)
<option value="{{ $oo->id }}" @if($oo->id == $o->domain_tld_id)selected @endif>{{ $oo->name }}</option> <option value="{{ $oo->id }}" @if($oo->id == old('service.domain_tld_id',$o->domain_tld_id))selected @endif>{{ $oo->name }}</option>
@endforeach @endforeach
</select> </select>
@ -60,7 +60,7 @@
<select class="form-control @error('service.domain_registrar_id') is-invalid @enderror" id="domain_registrar_id" name="service[domain_registrar_id]"> <select class="form-control @error('service.domain_registrar_id') is-invalid @enderror" id="domain_registrar_id" name="service[domain_registrar_id]">
<option></option> <option></option>
@foreach(\App\Models\DomainRegistrar::active()->orderBy('name')->get() as $oo) @foreach(\App\Models\DomainRegistrar::active()->orderBy('name')->get() as $oo)
<option value="{{ $oo->id }}" @if($oo->id == $o->domain_registrar_id)selected @endif>{{ $oo->name }}</option> <option value="{{ $oo->id }}" @if($oo->id == old('service.domain_registrar_id',$o->domain_registrar_id))selected @endif>{{ $oo->name }}</option>
@endforeach @endforeach
</select> </select>
@ -107,7 +107,7 @@
</div> </div>
<select class="form-control @error('service.registrar_ns') is-invalid @enderror" id="registrar_ns" name="service[registrar_ns]"> <select class="form-control @error('service.registrar_ns') is-invalid @enderror" id="registrar_ns" name="service[registrar_ns]">
@foreach(\App\Models\Service\Domain::select('registrar_ns')->distinct()->get() as $oo) @foreach(\App\Models\Service\Domain::select('registrar_ns')->distinct()->get() as $oo)
<option value="{{ $oo->registrar_ns }}" @if($oo->registrar_ns == $o->registrar_ns)selected @endif>{{ $oo->registrar_ns }}</option> <option value="{{ $oo->registrar_ns }}" @if($oo->registrar_ns == old('service.registrar_ns',$o->registrar_ns))selected @endif>{{ $oo->registrar_ns }}</option>
@endforeach @endforeach
</select> </select>
<span class="invalid-feedback" role="alert"> <span class="invalid-feedback" role="alert">