0
votes

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

1
I am having a little bit of trouble understanding your question. Your values are being sent over correctly? But the validation is not being run "correctly"? Is it the max length? The subject relation? - Henry
What are you "trying to fail it" with? - Henry
@Henry the name field is unique and when I tried to insert a name that already exists it passes the validation can cause and Internal error. Normally when I send ajax request without x-editable and try to fail that validation I'm returned the error message through json, which I catch and display it to the user. - Nathan Siafa

1 Answers

2
votes

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.)