0

i'm trying to make validation in laravel but the code isn't returning in ajax here is my controller function :

public function postIndex(CategoryRequest $request ){

    $request->store();

    return ['status' => 'success' ,'data' => 'Data has been added successfully'];
}

and that's my request :

<?php

namespace App\Http\Requests;

use App\Category;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\ValidationException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Exceptions\HttpResponseException;

class CategoryRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name_ar' => 'required',
            'name_en' => 'required'
        ];
    }

    /**
     * Get the validation messages
     *
     * @return array
     */
    public function messages()
    {
        return [
            'name_ar.required' => 'Please enter the name in arabic',
            'name_en.required' => 'Please enter the name in english'
        ];
    }


    public function failedValidation(Validator $validator)
    {

        if ($validator->fails()){
            return ['status' => 'error' ,'data' => $validator->messages()->getMessages()];
        }
    }

    /**
     * Store data function
     *
     */
    public function store()
    {
        $category = new Category();

        if ($category->save()){
            $category->details()->create([
                'name' => $this->name_en,
                'lang' => 'en'
            ]);
            $category->details()->create([
                'name' => $this->name_ar,
                'lang' => 'ar'
            ]);
        }
    }

    /**
     *Edit data function
     */
    public function edit($id){
        $category = Category::find($id);

        $category->english()->update([
            'name' => $this->name_en
        ]);
        $category->arabic()->update([
            'name' => $this->name_ar
        ]);
    }
}

i need to get the error messages using ajax but i don't know how to make it from request , i already can do it from the controller but i want my code to be clear so how can i do it ?

1 Answer 1

1

hey guys i found out the error , i had to add these two function in request file :

protected function formatErrors(Validator $validator)
{
    $result = ['status' => 'error' ,'data' => implode(PHP_EOL ,$validator->errors()->all())];

    return $result;
}

public function response (array $errors) {
    return response()->json($errors, 200);
}

after i added them , the errors are now shown in ajax

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for setting me in the right direction. Helped me to find this comment with the latest options. laracasts.com/discuss/channels/laravel/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.