Support the ongoing development of Laravel.io →
Requests Validation Laravel
Last updated 1 year ago.
0

I have the same isue. Running latest Laravel 6.

I have tried this in my controller: $mValidatedData = $pRequest -> validate( $mRules, $mRuleMessages ); but that doesn't display any error messages in the form when directed back to the form. Although validation fails, and the $errors is being populated, when it gets to generating the view the _$errors _is empty.

0

Well, It looks like you are not doing anything with the method after validation. That block of code doesnt give too much information, but I can tell you 3 things.

1- You need to redirect to the view after the $model->save(); method.

2- You need to add a div on the form to display the errors.

3- In your model you need to add the 'fillable' fields.

Here's an example.

Add the model at the top of the controller:

    use App\Model;

Method to store :

    public function store(Request $request)
    {
        $request->validate([
            'category_id'=>'required',
            'name'=>'required',
            'description'=>'required',
            'image'=>'required',
            'status'=>'required',
            'created_by'=>'required',
            ]);
        $model= new Model();
        $model->category_id = $request->category_id;
        $model->name        =   $request->name;
        $model->description =   $request->description;
        $model->image       =   $request->image;
        $model->status      =   $request->status;
        $model->created_by  =   $request->created_by;
        $model->save();
        return redirect('path/to/redirect/after/save')->with('success', 'Model added successfully);
    }

or you can use:

    $model = new Model();
    $model->fill($request->all());
    $model->save();

Also if you want the 'created_by' field filled automatically with the user id you need to remove the "required" on the top and add this:

    $model->created_by = Auth::id();

before the "save()" method, and of course include:

    use Auth;

In top of your controller.

In your model you need to have this:

    protected $fillable = [
        'category_id',
        'name',
        'description',
        'image',
        'status',
        'created_by',
    ];

And finally in your view for every input you need to add this after the element:

    @if ($errors->has('name'))
        <span class="invalid-feedback" role="alert">
            <strong>{{ $errors->first('name') }}</strong>
        </span>
    @endif

or you could add this after the "form" tag:

    @if ($errors->any())
        <div class="alert alert-secondary" role="alert">
            <div class="alert-icon">
                <i class="flaticon-warning "></i>
            </div>
            <div class="alert-text">
                <ul>
                    @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </div>
        </div><br />
    @endif

TIP: if you want the user to keep the information they had in the form, the attribute value of the input should be like this:

    <input type="text" class="form-control" name="name" value="{{ old('name') }}">

I hope this helps. Regards. Daniel

Last updated 4 years ago.

sangrialdrete, manojibcarockers liked this reply

3

Thank you friends for your valuable solution .

I use input validation in **api side ** so This may be a reason of the issue.Because(i use front-end side angularjs)

I tried above solution but not solved. i don't know exact solution for this issue

But

I Got a solution with use of Manually Creating Validators

Solution : -

[https://laravel.com/docs/6.x/validation#manually-creating-vali...]

[https://laravel.com/docs/6.x/validation#working-with-error-mes...]

Code : -

        use Illuminate\Support\Facades\Validator;
        public function store(Request $request)
        {
       $validator = Validator::make($request->all(), [
       'name'=>'required|unique:categories',
       'description'=>'required',
       'image'=>'required',
       'status'=>'required',
       'created_by'=>'required',
        ]);
       if (!$validator->fails())
       {
       $validated_data=$request->all();
       $category = new Categories();
       $category->fill($validated_data);
       $category->save();
       return $category->category_id;
       } 
      else
       return $validator->errors();
       }

Finally thanks for all.(@ sangrialdrete,@ vstokesjr)

Last updated 4 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.