Skip to content
Advertisement

Serialize form validation Laravel 8

could not validate an array of input in laravel 8. from the frontside I have gotten the serialized form data which was successfully accessible from the backend.

public function GetForm(Request $request)
{
        $request = $request->all();
        $input =  array();
        parse_str($request['data'], $input);

        return $input;
}

output:
  {
    "inputID":"26",
    "inputName":"Julie Hughes est",
    "inputPhone":"+94111222333",
    "inputEmail":"testmail@gmail.com"
  }

what I want is to validate the form inside the update function before I update the data when the user changes.

    public function update(Request $request)
    {
        if (request()->ajax()) {
           $request = $this->GetForm($request);
            $object = User::find($request['inputID']);
            $object->name = $request['inputName'];
            $object->phone = $request['inputPhone'];
            $object->email = $request['inputEmail'];
            $object->save();

            return response()->json([ 
                "message" => "User Information has been updated"
            ], 200);
        }
    }

what I have tried on the validation function is below

public function GetForm(Request $request)
    {
        $request = $request->all();
        $input =  array();
        parse_str($request['data'], $input);

        return $this->validate(
            $input, // Expected type 'IlluminateHttpRequest'. Found'array'.intelephense(1006)
            [
                'inputID' => ['required'],
                'inputName' => ['required', 'string', 'max:255'],
                'inputPhone' => ['required', 'string', 'email'],
                'inputEmail' => ['required', 'min:9', 'number'],
            ],
        );
    }

looking recommendation to solve this issue.

      $('#updateBtn').click(function() {
            console.log('update triggered')
            axios.get('/users/update/', {
                    params: {
                        data: $("#updateForm").serialize()
                    }
                })
                .then(function(response) {
                    let i = response.data
                    console.log(i);
                    userTable.ajax.reload();
                    $('#editModel').modal('hide')
                    window.toastr.success(i.message);

                }).catch(function(error) {
                    console.log(error)
                })
        })

Advertisement

Answer

Try the validation like this.

public function getForm(Request $request)
{
    $inputs = $request->all();
    $validator = Validator::make($inputs, [
        'inputID' => 'required',
        // other validations
    ]);

    if ($validator->passes()) {

        return response()->json(['message'=>'User Information has been updated.']);
        
    }

    return response()->json(['error'=>$validator->errors()]);
}

Validator::make uses the Validator facade to create a validator instance for you. Nothing more. You have to execute the validation and implement the error handling yourself.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement