【问题标题】:Laravel Form validation is not working during Form UpdationLaravel 表单验证在表单更新期间不起作用
【发布时间】:2020-06-18 21:08:32
【问题描述】:

在我的 laravel 项目中,我正在使用 js 验证器,但是在表单更新期间,验证不起作用。如果我没有进行任何更改,我的表单没有提交。有时它会向电子邮件字段显示错误。

以下是我的验证码,

protected $edituserValidationRules = [
        'user_name' => 'required',
        'email' => 'required|email|max:255|unique:users',
    ];

以下是通过验证的代码和其他要查看的内容

public function viewUsers($id)
    {
        $users = User::find($id);

        $validator = JsValidator::make($this->edituserValidationRules,[],[],'#useredit');
        return view('user.update')->with(['validator' => $validator,'users'=> $users]);

    }

以下是更新特定用户的代码

public function updateUsers(Request $request , $id)
    {
        $this->validate($request, [
            'email' => 'required|email|max:255|unique:users' . $request->id
        ]);

        $postdata = $request->all();
        $user = new User;
        $user = User::find($id);
        $hashedRandomPassword = Hash::make($postdata['password'], [
            'rounds' => 12
        ]);
        $api_key = Str::random(16);
        $checkApiKey = User::where('api_key', $api_key)->first();
        if(!empty($checkApiKey)) {
            $api_key = Str::random(16);
        }
       $user = new User;
       $user->name=$postdata['user_name'];
       $user->email=$postdata['email'];
       $user->password= $hashedRandomPassword;
       $user->ip_address=$request->ip();
       $user->api_key=$api_key;
        if($user->save()){
            Session::flash('message', 'User Updated Sucessfully');
            Session::flash('msgclass', 'alert-success');
        }
        return redirect('users');

    }

以下是查看页面中的代码

@extends('user.layout.app')

@section('content')
    <script src="{{ url('js/user/location.js') }}"></script>
    <div class="container-fluid add-location">
        <div class="row">
            <div class="col-md-12">
                <div class="card">
                    <form method="post" action="{!! route('updateUsers', [$users->id]); !!}" name="useredit" id="useredit" enctype="multipart/form-data" novalidate>
                        {{ csrf_field() }}
                        <div class="card-header">
                            <h4 class="card-title"> Edit User </h4>
                        </div>
                        @if(!empty($errors->all()))
                            <div class="row"> @foreach ($errors->all() as $error)
                                    <div class="col-lg-12">
                                        <div class="alert alert-danger"> <span>{{ $error }}</span> </div>
                                    </div>
                                @endforeach </div>
                        @endif
                        <div class="card-content">
                        <div class="row">
                                <div class="col-xs-12 col-sm-12 col-md-12">
                                    <div class="form-group">
                                        <label class="control-label">Full Name
                                            <star>*</star>
                                        </label>
                                        <input id="user_name" name="user_name" class="controls form-control" type="text" placeholder="User Name" value="{{$users->name}}">
                                        @if ($errors->has('user_name')) <span class="help-block"> {{ $errors->first('user_name') }} </span> @endif </div>
                                </div>

                                <div class="col-xs-12 col-sm-12 col-md-12">
                                    <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
                                        <label for="email" class="control-label">Email
                                            <star>*</star>
                                        </label>
                                        <input id="email" type="email" class="form-control" name="email" value="{{$users->email}}" placeholder="Email">
                                        @if ($errors->has('email'))
                                            <span class="help-block">
                                        <strong>{{ $errors->first('email') }}</strong>
                                    </span>
                                        @endif
                                    </div>
                                </div>

                                <!-- <div class="col-xs-12 col-sm-12 col-md-12">
                                    <div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
                                        <label for="password" class="control-label">Password
                                            <star>*</star>
                                        </label>
                                        <input id="password" type="password" class="form-control" name="password" value="{{bcrypt($users->password)}}">
                                        @if ($errors->has('password'))
                                            <span class="help-block">
                                        <strong>{{ $errors->first('password') }}</strong>
                                    </span>
                                        @endif
                                    </div>

                                </div> -->
                            </div>
                            <div class="row">


                                <div class="col-xs-12 col-sm-12 col-md-6">
                                    <div class="col-xs-12 col-sm-12 col-md-12 form-action">
                                        <button type="submit" class="btn btn-fill btn-info">Submit</button>
                                        <a href="" class="btn btn-default btn-fill">Cancel</a> </div>
                                </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    {!! $validator !!}
@endsection

这里有什么问题,在添加客户的过程中,我正在检查电子邮件是否已经存在,这里也在检查。

我不知道发生了什么?请帮忙

【问题讨论】:

  • 写出实际问题并说清楚。
  • @AlexandrBiship 问题是更新期间表单未验证。例如,我只是单击了编辑按钮,但没有更新任何值表单未提交或给出重复输入错误

标签: php laravel validation


【解决方案1】:

尝试改变这一行

$this->validate($request, [
   'email' => 'required|email|max:255|unique:users' . $request->id
]);

$request->validate([
    'email' => 'required|email|max:255|unique:users,email,'.$id
]);

【讨论】:

  • 我认为我的更新功能有问题,因为在添加用户时,我有另一个名为密码的表单,但在更新期间不允许用户更新密码
  • 那么告诉我现在有什么问题吗?没有更新或没有显示错误消息或什么?
  • 更新时收到错误电子邮件 ID 重复,如果我没有更改电子邮件
  • 检查我的答案,通过 $id 因为从你的表单中你没有通过 id 我猜
  • 我正在传递 id
【解决方案2】:

添加 ',id' 语句末尾用于防止电子邮件存在检查当前 id...

$request->validate([
    'email' => 'required|email|max:255|unique:users,email,'.$request->id.',id',
]);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-01
    • 1970-01-01
    • 2019-01-24
    • 2017-10-08
    • 2017-03-12
    • 2021-08-28
    • 1970-01-01
    • 2020-07-07
    相关资源
    最近更新 更多