Form request validation



  • Hi all,

    I have a simple form with a validation, for an unknow reason I alway have 500 error when I submit the form with incomplete data. The normal result should be, back to the form and display the errors message

    Strange behavior, if APP_DEBUG = true, it is working as expected.

    The route:

    <?php
    
    use Illuminate\Routing\Router;
    /** @var Router $router */
    
    $router->group(['prefix' =>'/sponsors'], function (Router $router) {
        $router->bind('sponsors', function ($id) {
            return app('Modules\Sponsors\Repositories\SponsorsRepository')->find($id);
        });
        $router->get('sponsors', [
            'as' => 'admin.sponsors.sponsors.index',
            'uses' => 'SponsorsController0',
            'middleware' => 'can:sponsors.sponsors.index'
        ]);
        $router->get('sponsors/create', [
            'as' => 'admin.sponsors.sponsors.create',
            'uses' => 'SponsorsController@create',
            'middleware' => 'can:sponsors.sponsors.create'
        ]);
        $router->post('sponsors', [
            'as' => 'admin.sponsors.sponsors.store',
            'uses' => 'SponsorsController@store',
            'middleware' => 'can:sponsors.sponsors.create'
        ]);
        $router->get('sponsors/{sponsors}/edit', [
            'as' => 'admin.sponsors.sponsors.edit',
            'uses' => 'SponsorsController@edit',
            'middleware' => 'can:sponsors.sponsors.edit'
        ]);
        $router->put('sponsors/{sponsors}', [
            'as' => 'admin.sponsors.sponsors.update',
            'uses' => 'SponsorsController@update',
            'middleware' => 'can:sponsors.sponsors.edit'
        ]);
        $router->delete('sponsors/{sponsors}', [
            'as' => 'admin.sponsors.sponsors.destroy',
            'uses' => 'SponsorsController@destroy',
            'middleware' => 'can:sponsors.sponsors.destroy'
        ]);
    // append
    });
    
    

    The form request (only one language)

    <?php namespace Modules\Sponsors\Http\Requests;
    
    use Modules\Core\Internationalisation\BaseFormRequest;
    
    class SponsorsStoreRequest extends BaseFormRequest
    {
    
        public function rules()
        {
            return [
                'title' => 'required',
                'url' => 'sometimes|url',
            ];
        }
    
    
        public function authorize()
        {
            return true;
        }
    
        public function messages()
        {
            return [
                'title.required' => 'Le titre est obligatoire',
                'url.url' => 'Le site n\'est pas valide',
    
            ];
        }
    }
    

    The controller:

    <?php
    
    namespace Modules\Sponsors\Http\Controllers\Admin;
    
    use Illuminate\Http\Request;
    ...
    use Modules\Sponsors\Events\SponsorsWasCreated;
    use Modules\Sponsors\Events\SponsorsWasUpdated;
    use Modules\Sponsors\Http\Requests\SponsorsStoreRequest;
    
    class SponsorsController extends AdminBaseController
    {
    
    ...
    /**
         * Store a newly created resource in storage.
         *
         * @param  Request $request
         * @return Response
         */
        public function store(SponsorsStoreRequest $request)
        {
            $data = $request->all();
    
            $sponsors = $this->sponsors->create($data);
    
            event(new SponsorsWasCreated($sponsors, $data));
    
            return redirect()->route('admin.sponsors.sponsors.index')
                ->withSuccess(trans('core::core.messages.resource created', ['name' => trans('sponsors::sponsors.title.sponsors')]));
        }
    


  • at app/Exceptions/Handler.php find render function:
    if (config('app.debug') === false) { return $this->handleExceptions($e); }

    replace by

    if (config('app.debug') === false && !($e instanceof ValidationException)) { return $this->handleExceptions($e); }


Log in to reply
 

Looks like your connection to AsgardCms was lost, please wait while we try to reconnect.