Extend FormRequest to Allow More Functionality in Laravel (v5.2)

  • view
{{ Form::open(['route' => 'store.files', 'method' => 'POST', 'files'=>true]) }}
  {{ Form::file('photo[]', ['multiple']) }}
  {{ Form::submit('submit') }}
{{ Form::close() }}

@foreach ($errors->get('photo') as $one)
  <li>{!! $one !!}</li>
@endforeach
  • controllers/PostController.php
namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Http\Requests\StoreValidation;

class PostController extends Controller
{
  public function store(StoreValidation $request, $id)
  {
    // if validation failed, it will automaticlly redirect back
    // https://laravel.com/docs/5.2/validation#form-request-validation

    // $request->all()
  }
}
  • Requests\StoreValidation
namespace App\Http\Requests;

use Validator;
use Illuminate\Http\Exception\HttpResponseException;
use Illuminate\Contracts\Validation\Validator as ValidatorContract;

class StoreValidation extends Request
{
  protected $error_container = [];


  // ...


  // used for single input only
  // note we dont use the 'name.*' for mulifile upload anymore
  public function rules()
  {
    return [
      // ...
    ];
  }


  // 1- we call the native validate method
  // and attach any extra checking method before 
  // the validation fail/pass check.
  public function validate()
  {
    $instance = $this->getValidatorInstance();

    $this->validateExtra($instance);

    if ( ! $this->passesAuthorization()) {
      $this->failedAuthorization();
    } elseif ( ! $instance->passes()) {
      $this->failedValidation($instance);
    }
  }


  // 2- then we replace the original rule array with the new array
  // from the extra validation, so we can use the same syntax 
  // to get the errors whether its single or multi
  protected function failedValidation(ValidatorContract $contract)
  {
    throw new HttpResponseException(
      $this->response(
        array_replace_recursive(
          $this->formatErrors($contract),
          $this->error_container
        )
      )
    );
  }


  // 3- the extra validation logic
  // you can change the method name to anything you want
  // you can even make more than one for each input extra validation
  // then add it to the 'validate()' method above.
  protected function validateExtra($instance)
  {
    $request = $this->instance();

    $instance->after(function ($instance) use ($request) {

      // add the multifile validation you want in here
      // something like https://ctf0.wordpress.com/2016/10/17/multifile-upload-validation-in-laravel-v5-2-pt-2/

    });
  }

}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s