如何在更新功能上忽略电子邮件地址上的唯一验证规则?

时间:2021-05-09 20:23:55

I have one form and it is in edit page, which has field name email. Now I don't want to check email validation, which is unique. I have already put validation in rules like 'email'=>'unique:users', which perfectly works on create page.So that validation I want to ignore on edit page. And I have to use FormRequest class like FormRequest $request in update function's argument, which defines rules array of form's fields validations.

我有一个表单,它在编辑页面,其中包含字段名称电子邮件。现在我不想检查电子邮件验证,这是唯一的。我已经在'email'=>'unique:users'这样的规则中进行了验证,这完全适用于创建页面。所以我想在编辑页面上忽略该验证。我必须在更新函数的参数中使用FormRequest类,如FormRequest $ request,它定义了表单字段验证的规则数组。

public function rules()
    {
        $business = BusinessModel::all();

        return[
                'companyName' =>'required',
                'address1'    =>'required',
                'pinCode'     =>'required|numeric',
                'city'        =>'required',
                'email'       =>'required|email|unique:users'.$id,
                'phoneNumber' =>'required|numeric|unique:business|size:10',
                'website'     =>'required|url',
                'contactname' =>'required',
                'designation' =>'required',
                'emailaddress'=>'required|email|unique:business',
                'phno'        =>'required|numeric',
              ];
      }

How can we pass id to rules function in FormRequest class..because in this case..It'll show me error like, $id is undefined
'email' =>'required|email|unique:users'.$id, [error undefined variable id]

我们如何在FormRequest类中将id传递给rules函数...因为在这种情况下..它会显示我的错误,$ id未定义'email'=>'required | email | unique:users'。$ id,[错误未定义的变量ID]

2 个解决方案

#1


2  

just pass the primary key of the current record, validator will skip that row in table.

只传递当前记录的主键,验证器将跳过表中的那一行。

'email'=>'unique:users,email,'.$id,

'电子邮件'=> '唯一的:用户,电子邮件,' $ ID,

in which,

其中,

email is the field name. $id is the variable in which primary key is stored.

email是字段名称。 $ id是存储主键的变量。

#2


0  

Because you're using the FormRequest class the $id variable isn't directly available to you.

因为您正在使用FormRequest类,所以$ id变量不能直接使用。

However the same data will already be available to this class through either the form inputs or the route parameters depending on how you've structured things.

但是,通过表单输入或路径参数,相同的数据将可用于此类,具体取决于您的结构。

So if the route is something like myapp/user/234 then you can access the user id using $this->route('user')

因此,如果路由类似于myapp / user / 234,那么您可以使用$ this-> route('user')访问用户ID

This would make your rule look like:

这会使您的规则看起来像:

   public function rules()
    {
        $business = BusinessModel::all();

        return[
                'companyName' =>'required',
                'address1'    =>'required',
                'pinCode'     =>'required|numeric',
                'city'        =>'required',
                'email'       =>'required|email|unique:users'.$this->route('user'),
                'phoneNumber' =>'required|numeric|unique:business|size:10',
                'website'     =>'required|url',
                'contactname' =>'required',
                'designation' =>'required',
                'emailaddress'=>'required|email|unique:business',
                'phno'        =>'required|numeric',
              ];
      }

Alternatively if the id is part of a form field rather than in the route you can access it using the request input method $this->input('id').

或者,如果id是表单字段的一部分而不是路径中的一部分,则可以使用请求输入方法$ this-> input('id')访问它。

Full documentation on this can be found at: https://laravel.com/docs/5.6/requests#retrieving-input

有关此内容的完整文档,请访问:https://laravel.com/docs/5.6/requests#retrieving-input

#1


2  

just pass the primary key of the current record, validator will skip that row in table.

只传递当前记录的主键,验证器将跳过表中的那一行。

'email'=>'unique:users,email,'.$id,

'电子邮件'=> '唯一的:用户,电子邮件,' $ ID,

in which,

其中,

email is the field name. $id is the variable in which primary key is stored.

email是字段名称。 $ id是存储主键的变量。

#2


0  

Because you're using the FormRequest class the $id variable isn't directly available to you.

因为您正在使用FormRequest类,所以$ id变量不能直接使用。

However the same data will already be available to this class through either the form inputs or the route parameters depending on how you've structured things.

但是,通过表单输入或路径参数,相同的数据将可用于此类,具体取决于您的结构。

So if the route is something like myapp/user/234 then you can access the user id using $this->route('user')

因此,如果路由类似于myapp / user / 234,那么您可以使用$ this-> route('user')访问用户ID

This would make your rule look like:

这会使您的规则看起来像:

   public function rules()
    {
        $business = BusinessModel::all();

        return[
                'companyName' =>'required',
                'address1'    =>'required',
                'pinCode'     =>'required|numeric',
                'city'        =>'required',
                'email'       =>'required|email|unique:users'.$this->route('user'),
                'phoneNumber' =>'required|numeric|unique:business|size:10',
                'website'     =>'required|url',
                'contactname' =>'required',
                'designation' =>'required',
                'emailaddress'=>'required|email|unique:business',
                'phno'        =>'required|numeric',
              ];
      }

Alternatively if the id is part of a form field rather than in the route you can access it using the request input method $this->input('id').

或者,如果id是表单字段的一部分而不是路径中的一部分,则可以使用请求输入方法$ this-> input('id')访问它。

Full documentation on this can be found at: https://laravel.com/docs/5.6/requests#retrieving-input

有关此内容的完整文档,请访问:https://laravel.com/docs/5.6/requests#retrieving-input