0

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

3
  • 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
    Commented Jun 30, 2017 at 21:11
  • What are you "trying to fail it" with?
    – Henry
    Commented Jun 30, 2017 at 21:11
  • @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. Commented Jun 30, 2017 at 21:51

1 Answer 1

2
+50

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

3
  • if there is two(2) fields to be validated by the controller(nameandanother field) the only option here is to do a conditional statement on the rules right? Commented Jul 1, 2017 at 9:05
  • @NathanSiafa yes, you can also use the required_if validation rule to check values of another field. However, the fields are always name and value, are they not?
    – alepeino
    Commented Jul 1, 2017 at 13:29
  • @lepeino thanks very much. You cost me a little, but it was worth it. Commented Jul 3, 2017 at 23:22

Not the answer you're looking for? Browse other questions tagged or ask your own question.