【问题标题】:Looping through validation data received from Ajax in laravel controller在 laravel 控制器中循环从 Ajax 接收到的验证数据
【发布时间】:2021-06-16 17:33:54
【问题描述】:

我正在通过 Ajax 发送一个 json 请求,该请求包含多个对象,我正在尝试找到一种方法来验证请求是否失败,如果至少一个特定属性不符合规则(“值”中最少 5 个字符);但是dosent似乎有效,有人可以帮忙吗? 我试图循环数据,我觉得我很接近但仍然没有工作。

my Ajax:
-------    
< script >
  function ajaxUpdate() {
    var formdata = document.getElementsByTagName('input'); 
    var formT = [].map.call(formdata, function(input) {

      if (input.style.textDecoration === "line-through") {
        input['completed'] = "1"; //add key value pairs
      } else {
        input['completed'] = "0"; //add key value pairs
      }
      return {
        'value': input.value,
        'id': input.id,
        'completed': input.completed
      }; //determine what keys to show.
    });

    formT.shift(); // Removes the first element from an array, because it contains token
    formT = formT.filter(function(e) {
      return e != null; //remove null elements.
    });

    $.ajaxSetup({
      headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
      }
    });
    // e.preventDefault();


    $.ajax({
      url: "/updateAddTask",
      dataType: "json",
      contentType: "application/json",
      data: {
        objJSON: JSON.stringify(formT),
      },

      success: function(response) {

        if ($.isEmptyObject(response.error)) {

          if (response.success === true) {
            successSweetAlert();
          } else {
            alert(JSON.stringify(response.errors.value.toString()));
          }

        }
      },
      // location.reload();
    })
  } 
</script>



My controller:
--------------

  public function loopData(Array $data)
{
    foreach ($data  as $key => $jsons) {
        foreach ($jsons as $key1 => $value) {
            return $jsons;
        }
    }
}
   
  public function ajaxUpdateTasks(Request $request)

    {
        $data = json_decode($request->objJSON, true);//this will give an array of all the input elements

// dd($data);//output is below:
// array:3 [
//  0 => array:3 [
//    "value" => "kilo"
//    "id" => "55"
//    "completed" => "0"
//  ]
//  1 => array:3 [
//    "value" => "new123"
//    "id" => "793"
//    "completed" => "1"
//  ]
//  2 => array:3 [
//    "value" => "ef4"
//    "id" => "794"
//    "completed" => "0"
//  ]
//]


        $rules = [
            'value' => 'required|max:255|min:5', //value is
        ];


        $messages = [
            'value.max' => 'Todo title should be less than 255 characters',
            'value.min' => 'Todo title should be more than 4 characters'
        ];


        $validator = Validator::make($this->loopData($data),$rules, $messages);// ?????

        if ($validator->fails()) {

            $errors = $validator->getMessageBag()->toArray();
            $result = ['success' => false, 'errors' => $errors];
            return response()->json($result);
        } else {
            $result = ['success' => true, 'errors' => null];

            $count = count(json_decode($request->objJSON, true));
            $id = json_decode($request->objJSON, true)[0] ['id'];
            $value = json_decode($request->objJSON, true)[0] ['value'];
            $completed = json_decode($request->objJSON, true)[0] ['completed'];
            
            
            -----remaining of the code --------
    }
    

感谢您的帮助。

【问题讨论】:

  • 我尝试了以下方法: $validator=Validator::make($data, [ "objJSON"=> 'array', "objJSON.value"=> 'min:5', ]);但还是不行。
  • 由于你必须解码你的 json,它没有正确发送你作为一个错误的字符串发送

标签: php json ajax laravel


【解决方案1】:

我通过简单地更改规则和消息来解决它: $rules = [ " *.value" => 'min:4', ]; $messages = [ " *.value.max" => '待办事项标题应少于 255 个字符', " *.value.min" => '文本应多于 4 个字符' ]; $validator=Validator::make($data, $rules,$messages);

【讨论】:

    猜你喜欢
    • 2014-05-16
    • 2021-12-15
    • 2016-10-17
    • 2021-11-23
    • 2016-04-15
    • 2018-04-07
    • 1970-01-01
    • 1970-01-01
    • 2018-11-17
    相关资源
    最近更新 更多