如何在laravel中验证X可编辑请求(服务器端) [英] How to validate X-editable request in laravel (Server side)

查看:85
本文介绍了如何在laravel中验证X可编辑请求(服务器端)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法进行了x可编辑( https://vitalets.github.io/x-editable/)来处理页面,只要我单击一个字段,它就会显示内联并且可以对其进行编辑,并且能够成功提交到POST URI. >

这里的想法是我要发送三个键值对:

array:3 [▼
  "name" => "name"
  "value" => "mathematics"
  "pk" => "1"
]

和我的update()方法捕获数组,它成功更新了数据库中的记录.但是我无法验证数据.

这是我的控制器的外观:

 public function update(Request $request)
{
    //
    $id = $request->pk;
    $subject = Subject::findOrFail($id);

    $rules = array (
        'name' => 'bail|required|max:20|unique:subjects,name,'.$id
    );

即使我尝试失败,该验证也很容易通过

    $validator = Validator::make ( $request->all(), $rules );

    if ($validator->fails ()) {
        return response()->json ( array (

         'errors' => $validator->getMessageBag ()->toArray ()
        ) );
    } else {
        $subject->update([$request->name => $request->value]);
    }
    return response ()->json ( $subject );
 }

就好像我以某种方式没有将正确的" Request对象传递给validate()一样?没有表单提交,但是文档明确指出:

Laravel生成一个包含所有验证错误的JSON响应.该JSON响应将以422 HTTP状态代码发送.


1 https://laravel.com/docs/5.4/validation#quick-ajax-requests-and-validation

解决方案

如果我没记错的话,name是要编辑的字段(DB列),而value是值.似乎您正在更新name列,因此您必须验证请求中value的唯一性,而不是名称".

此外,我建议您使用控制器的validate方法(由ValidatesRequests特性提供):

public function update(Request $request)
{
    $id = $request->pk;
    $subject = Subject::findOrFail($id);

    $this->validate($request, [
        'name' => 'required', // this should be the column to update
        'value' => 'bail|required|max:20|unique:subjects,name,'.$id
    ];        

    $subject->update([$request->name => $request->value]);

    return $subject;
 }

此处validate将自动以422代码和JSON响应中的验证错误来拒绝.如果通过,它将继续进行更新. (return $subject还会在响应中返回该对象的JSON表示形式.)

I've managed to get x-editable (https://vitalets.github.io/x-editable/) to work with a page, in so far as when I click on a field, it displays inline and I can edit it, and I'm able to successfully submit to a POST URI.

The idea here is that I'm sending three key-value pairs:

array:3 [▼
  "name" => "name"
  "value" => "mathematics"
  "pk" => "1"
]

and my update() method catches the array, and it successfully updates the record in the database. But I'm failing to validate the data.

This is how my controller looks:

 public function update(Request $request)
{
    //
    $id = $request->pk;
    $subject = Subject::findOrFail($id);

    $rules = array (
        'name' => 'bail|required|max:20|unique:subjects,name,'.$id
    );

This validation pass easily even if I try to fail it

    $validator = Validator::make ( $request->all(), $rules );

    if ($validator->fails ()) {
        return response()->json ( array (

         'errors' => $validator->getMessageBag ()->toArray ()
        ) );
    } else {
        $subject->update([$request->name => $request->value]);
    }
    return response ()->json ( $subject );
 }

So it's as if I'm somehow not passing the "correct" Request object to validate()? There is no form submission, but the documentation clearly states that:

Laravel generates a JSON response containing all of the validation errors. This JSON response will be sent with a 422 HTTP status code.1

Route:

Route::post('/subjects/update/', 'SubjectsController@update');

script:

$('#subjects').editable({
     container:'body',
     selector:'td.name',
     type:'post',
     dataType:'JSON',
     validate:function(value){
        if ($.trim(value) === '') {
           return "Field is required";
        }
     }
});


1https://laravel.com/docs/5.4/validation#quick-ajax-requests-and-validation

解决方案

If I'm not mistaken, name is the field to be edited (the DB column), and value is, well, the value. It looks like you are updating the name column, so you have to validate the uniqueness of the value in the request, not the "name".

Also, I'd suggest you use the validate method of your controller (provided by the ValidatesRequests trait):

public function update(Request $request)
{
    $id = $request->pk;
    $subject = Subject::findOrFail($id);

    $this->validate($request, [
        'name' => 'required', // this should be the column to update
        'value' => 'bail|required|max:20|unique:subjects,name,'.$id
    ];        

    $subject->update([$request->name => $request->value]);

    return $subject;
 }

Here validate will automatically reject with a 422 code and the validation errors in the JSON response. If it passes, it will continue with the update. (return $subject also returns a JSON representation of the object in the response.)

这篇关于如何在laravel中验证X可编辑请求(服务器端)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆