Home > Blockchain >  Error when trying to fill in form in Laravel 8
Error when trying to fill in form in Laravel 8

Time:06-07

I am trying to create a form in Laravel, which has different fields that should be filled in. This is my code for the rules. They are stored in custom Request class:

class CategoryFormRequest 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()
    {
        $rules = [
            'name' => [
                'required',
                'string',
                'max:200'
            ],
            'slug' => [
                'required',
                'string',
                'max:200'
            ],
            'description' => [
                'required'
               
            ],
            'image' => [
                'required',
                'mimes:jpeg,jpg,png'
            ], 
            'meta_title' => [
                'required',
                'string',
                'max:200'
            ],
            'meta_description' => [
                'required',
                'string'
            ],
            'meta_keyword' => [
                'required',
                'string'
            ],

            'navbar_status' => [
                'nullable',
                'boolean',
            ],
            'status' => [
                'nullable',
                'boolean',
            ]
        ];
        return $rules;
    }
}

The store method is the following:

public function store(CategoryFormRequest $request){

        $data = $request->validated();

        $category = new Category;
        $category->name = $data['name'];
        $category->slug = $data['slug'];
        $category->description = $data['description'];

        if ($request->file('image')) {
            $file = $request->file('image');
            $filename = date('YmdHi').$file->getClientOriginalExtension();
            $file-> move(public_path('public/Image'), $filename);
            $data['image']= $filename;

        }

        $category->meta_title = $data['meta_title'];
        $category->meta_description = $data['meta_description'];
        $category->meta_keyword = $data['meta_keyword'];
        
        $category->navbar_status = $request->navbar_status == true ? '1' : '0';
        $category->status = $request->status == true ? '1' : '0';;
        $category->created_by = Auth::user()->id;
        $category->save();

        return redirect('admin/category')->with('message', 'Category Added Successfully');
    }

This is my view:

<div >

    <div >
        <div >
            <h4 > Add Category</h1>
        </div>
        <div >

            @if ($errors->any())
            <div >

                @foreach ($errors->all() as $error )
                <div>{{ $error }}</div>
                    
                @endforeach
            </div>
                
            @endif

            <form action="{{ url('admin/add-category') }}" method="POST" enctype="multipart/form-data">
                @csrf

                <div >
                    <label>Category Name</label>
                    <input type="text" name="name" >
                </div>

                <div >
                    <label>Slug</label>
                    <input type="text" name="slug" >

                </div>

                <div >
                    <label>Description</label>
                    <textarea name="description" rows="5" ></textarea>
                </div>

                <div >
                    <label>Image</label>
                    <input type="file" name="name" >
                </div>

                <h6>SEO Tags</h6>
                <div >
                    <label>Meta Title</label>
                    <input type="text" name="meta_title" >
                </div>

                <div >
                    <label>Meta Description</label>
                    <textarea name="meta_description" rows="3" ></textarea>
                </div>

                <div >
                    <label>Meta Keyword</label>
                    <textarea name="meta_keyword" rows="3" ></textarea>
                </div>

                <h6>Status Mode</h6>
                <div >
                    <div >
                        <label>Navbar Status</label>
                        <input type="checkbox" name="navbar_status"/>
                    </div>
                    <div >
                        <label>Status</label>
                        <input type="checkbox" name="status"/>
                    </div>
                    <div >
                       <button type="submit" >Save Category</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

Now, when I try to fill in everything I get always the following error:

The name must be a string. The image field is required.

I tried different approaches with validate, but is always the same. What could be the reason for this error?

CodePudding user response:

You need to give unique value as name parameter of an input field, and this name parameter must be aligned with the request file.

Since you have this in your request file:

  'image' => [
                'required',
                'mimes:jpeg,jpg,png'
            ], 

You need to change the input type file to have the same name:

   <div >
      <label>Image</label>
      <input type="file" name="image" >
   </div>
  • Related