laravel 中的唯一字段验证问题

我正在尝试验证我的 laravel 应用程序中的唯一条目


以下是我的验证数组,


$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());

我的验证工作正常但是,


当我尝试为我的域字段输入重复值时,它得到了正确验证但没有显示错误消息,说对不起名称已经存在......


<input type="text" id="domain" class="form-control" name="domain" >

{!! $errors->first('domain', '<span class="help-block" role="alert">:message</span>') !!}

在此跨度中,它只显示常见错误消息区域中的任何内容,它显示抱歉无法更新表单...那么我如何正确验证该字段并显示相关错误消息


UYOU
浏览 111回答 4
4回答

慕尼黑5688855

做这样的事情:根据插入请求使用'domain' => [&nbsp; &nbsp;...&nbsp; &nbsp;'unique:websites,domain']根据更新请求使用'domain' => [&nbsp; &nbsp;...&nbsp; &nbsp;"unique:websites,domain,{$this->website->id}"]要么'domain' => [&nbsp; &nbsp;...&nbsp; &nbsp;Rule::unique('websites', 'domain')->ignore($this->website)]

www说

您不需要在 ignore Rule 中传递重复的列来指示验证器忽略网站域,除了它自己吗?例如像Rule::unique('apps')->ignore($website->id)

慕沐林林

您将 $request->all() 作为验证消息传递。请试试:$website = $websiteModel->find($id);&nbsp; &nbsp; &nbsp; &nbsp; $request->validate([&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'subDomainName' => ['required','regex:/^[A-Za-z0-9 ]+$/'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'subDomainSuffix' => ['required'],&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'packageType' => ['required'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'themeid' => ['required'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'lang' => ['required'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'user' => ['required'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'domain' => [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'required',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'string',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'min:2',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'max:255',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Rule::unique('apps')->ignore($website)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; ]);

梵蒂冈之花

请试试这个 。它有助于解决你的问题在文件顶部使用异常和验证器use Exception;use Validator;$rules = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'subDomainName' => 'required|unique:sub_domain_name',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $validator = Validator::make($request->all(), $rules, $message);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($validator->fails()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new Exception(implode('\n', $validator->errors()->all()));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }sub_domain_name :这是数据库列名
打开App,查看更多内容
随时随地看视频慕课网APP