0

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)
               {{ $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?

2 Answers2

0

$request is an object, not an array. Your code should look like this

$item = new Item;
$item->title = $request->get('title');
if( $request->has('alias') ) {
    // ??? 
    $request['alias'] = $request['title'];
}
$item->category_id = $request->get('category_id');
$item->save();
return redirect()->route('admin.items');

You can also use $request->has('name') to check if a field has been submitted. But i am not sure what are you trying to do in the if statement.

0

You need to define a validator function in ItemController that validates ItemRequest using the rules you so defined.

protected function validator($data)
{
    return Validator::make($data, app(ItemRequest::class)->rules());
}

The Illuminate\Http\Foundation\FormRequest handles a failedValidation redirecting back to the previous URL with the errorBag available in the session for the view.

This isn't something Illuminate\Http\Request handles for you.

In ItemController write a validate method, and use this as a precondition in store before doing work.

function validate(Request $request) {
    $validator = $this->validator($request->all());
    if ($validator->fails()) {
        $this->throwValidationException(
            $request, $validator
        );
    }
}

function store(ItemRequest $request) {
    $this->validate($request);
    ...
}
Oluwafemi Sule
  • 36,144
  • 1
  • 56
  • 81
  • Thanks. I've added this to itemcontroller but still nothing on page –  Oct 02 '17 at 09:23
  • You need to extend FormRequest and not Request. Otherwise, you'll need to implement a bunch of methods to ensure a failed validation is handled correctly. – Oluwafemi Sule Oct 02 '17 at 09:46
  • Okay, I've changed it to `class ItemRequest extends FormRequest` no messages again. Do I need to make so other change? –  Oct 02 '17 at 10:35
  • Yeah, you need to call the validator to check the request and raise the necessary exception. If you don't still get the error messages in the view, you may need to consider the question @DinukaThilanga linked to in your question – Oluwafemi Sule Oct 02 '17 at 11:04
  • Still empty. `var_dump($errors)` return `object(Illuminate\Support\ViewErrorBag)#247 (1) { ["bags":protected]=> array(0) { } }`. It can't be so complex to add some validations –  Oct 02 '17 at 11:14
  • 2
    This fixed the issue. I don't understand why this happen and why this is not fixed. As I understand this is issue(?) from version 5.2... https://stackoverflow.com/a/34900644/8323337 –  Oct 02 '17 at 11:30