lundi 2 octobre 2017

Validation messages aren't showed on the view in Laravel

I have ItemController.php and ItemRequest.php.. ItemController.php is responsible for adding items and ItemRequest.php for form validation.

In ItemRequest.php I have this rules

class ItemRequest extends Request
{
    public function authorize()
    {
        return true;
    }
    public function rules()
    {
        return [
            'title' => 'required',
            'category_id' => 'required',
        ];
    }

    public function messages()
    {
        return [
            'title.required' => 'The :attribute field is required.',
            'category_id.requred' => 'The :attribute field is required.',
        ];
    }
}

In my view I have this

@if($errors->has())
    <div class="alert alert-danger alert-dismissible">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
        <h4><i class="icon fa fa-ban"></i> Alert!</h4>
        @foreach ($errors->all() as $error)
               </br>
        @endforeach
    </div>
@endif

The problem is that the form doesn't get any validation. I'm able to click Submit with empty fields and they are inserted in DB.

and this is the store function from ItemController.php

public function store( ItemRequest $request )
{      
    $item = new Item;
    $item->title = $request['title'];
    if( empty( $request['alias']) ) {
      $request['alias'] = $request['title'];
    }
    $item->category_id = $request['category_id'];
    $item->save();
    return redirect()->route('admin.items');
}

Why I don't get any messages or validation on the page?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire