【问题标题】:Laravel validate incoming ajax using requestLaravel 使用请求验证传入的 ajax
【发布时间】:2018-01-17 00:31:16
【问题描述】:

我有这样的看法:

//视图的sn-p

<td><input class="form-field" type="text" id="entity" name="name" data="{{$entity->id}}" value="{{$entity->name}}" onkeyup="validation(this.value);" onblur="updateEntity(this.value,this.name,this.id,{{$entity->id}})"></td>

<td><input class="form-field" type="text" id="entity" name="type" value="{{$entity->type}}" onkeyup="validation(this.value);" onblur="updateEntity(this.value,this.name,this.id,{{$entity->id}})"></td>

其中有一个ajax:

function updateEntity(value, name, data, id) {
  $.ajax({
        url: '/entityadmin/' + value + '/' + name + '/' + data + '/' + id,
        method: 'POST',
        dataType: 'json',
        success: function(save) {
          $('.messages').append('<div class="alert alert-success">Type Updated!<div>');
            setTimeout(function() {
              $(".alert").fadeTo(2000, 500).slideUp(500, function(){
                $(".alert").slideUp(500);
              });  
            }, 4000); 
        },
        error: function(data) {
          console.log(data);
          $('.messages').append('<div class="alert alert-danger">Error, please try again!<div>');
            setTimeout(function() {
              $(".alert").fadeTo(2000, 500).slideUp(500, function(){
                $(".alert").slideUp(500);
              });  
            }, 4000); 
        },
        headers: {
        'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
        }
    });
}

控制器:

public function entityUpdate($value, $name, $data, $id, EntityRequestUpdate $request) {

        $request->$name = $value; //like this?
        if($data == "entity") {
            $save = Entity::find($id);
        }else{
            $save = User::find($id);
        }
        $save->$name = $value;
        $save->save();
        return response()->json(['results' => $save]);       
    }

和请求:

public function rules()
    {
        return [
            'startdate' => 'required|date',
            'endate' => 'nullable|date',
            'startime' => 'required|time',
            'endtime' => 'required|time',
            'title' => 'required',
            'type' => 'required',
            'description' => 'required',
            'frequency' => 'required',
            'interval' => 'nullable|numeric',
            'monthday' => 'nullable|numeric|min:1|max:3',
            'weekday' => 'nullable|alpha|max:3',
            'month' => 'nullable|numeric',
            'until' => 'nullable|date',
            'tags' => 'nullable',
            'img' => 'nullable|file|image',
        ];
    }

问题是它只需要验证一个字段,因为每次都会更改一个字段,我如何使用此验证来验证传入变量并将错误返回给 ajax,如果有任何错误消息?

【问题讨论】:

    标签: ajax laravel laravel-5


    【解决方案1】:

    您可以manually create a validator 来验证这一字段,如下所示:

    $validator = Validator::make(
        [ 'name' => $value ], 
        collect($this->rules())->only([$name])->all()
    );
    

    此验证器将从定义的规则中获取 name 验证器,并根据第一个值数组进行检查。

    【讨论】:

    • 它如何知道该特定字段的验证规则?因为可能需要对姓名或电子邮件或其他任何内容进行验证,具体取决于实际正在编辑的内容
    • 我更新了问题,因此验证器使用动态$name
    猜你喜欢
    • 2020-08-20
    • 2018-01-24
    • 1970-01-01
    • 2016-09-20
    • 2016-09-20
    • 1970-01-01
    • 2017-09-05
    • 2017-11-21
    • 2020-01-13
    相关资源
    最近更新 更多