Mauro Baptista

I'm a software developer! I'm comfortable with PHP and Javascript. Currently developing this blog and a limo reservation app using Laravel and TDD!

Always learning something new!

January 12, 2017 at 00:00 / By Mauro Baptista Laravel PHP

Custom Validation Using Data From a Different Table

You want to confirm a hotel booking, but you want to confirm that the room can fit the number of people on this booking.

The two tables that matter are Room and Booking, and you want to validate a value from Booking that will use an info from Room.

My solution was:

1. Create a custom validation extending the Validator class.


\Validator::extend('greather_than', 'App\Validation\ValidationController@greaterThan');
2. I expect to call this validations as:

'people' => ['required', 'integer', 'min:1', 'greather_than:rooms,max_people,'.request('room_id')],

Where greather_than is my custom validation, and it requires 3 parameters:

  • $parameters[0] -> the table where it will find the value
  • $parameters[1] -> the column on that table that will be used to compare
  • $parameters[2] -> the id from the table

Basically, I want to search the ID request('room_id') on table rooms and return the value from the column max_people.

3. On Validation Controller (I put it in a separate folder, because I'm using some Trait to handle my validations, but that is not important for this issue):


    /**
     * Rule to compare if a given value is greater than a value in
     * another database
     *
     * @param  \Illuminate\Http\\Request  $request
     * @return \Illuminate\Http\\Response
     */
    public function greaterThan($attribute, $value, $parameters, $validator) {
        
        if (count($parameters) != 3)
            throw new \InvalidArgumentException("Validation rule greather_than requires 3 parameters.");

        $className = '\App\' . studly_case(str_singular($parameters[0]));

        $model = new $className;
        $param = $model->find($parameters[2])->pluck($parameters[1])->first();

        return $value <= $param;
    }

Now it is ready to use, but it has a problem: The class namespace and the class name are hard-coded. I can use it because I follow the suggested names by Laravel, but maybe you need to make some changes to make it work.

Whoops, looks like something went wrong.

1/1 ErrorException in Filesystem.php line 111: file_put_contents(): Only 0 of 312 bytes written, possibly out of free disk space

  1. in Filesystem.php line 111
  2. at HandleExceptions->handleError('2', 'file_put_contents(): Only 0 of 312 bytes written, possibly out of free disk space', '/opt/bitnami/apps/carnou.com/blog/vendor/laravel/framework/src/Illuminate/Filesystem/Filesystem.php', '111', array('path' => '/opt/bitnami/apps/carnou.com/blog/storage/framework/sessions/oLvUiN2WplPnoQdyf20sWqKGzls6BGmI5UWwfPtp', 'contents' => 'a:4:{s:6:"_token";s:40:"eZPZ2KDEq9xgURyeabamNdPI6OCsMmU8KYp1HoKh";s:9:"_previous";a:1:{s:3:"url";s:74:"http://carnou.com/post/custom-validation-using-data-from-a-different-table";}s:9:"_sf2_meta";a:3:{s:1:"u";i:1519160010;s:1:"c";i:1519160010;s:1:"l";s:1:"0";}s:6:"_flash";a:2:{s:3:"old";a:0:{}s:3:"new";a:0:{}}}', 'lock' => true))
  3. at file_put_contents('/opt/bitnami/apps/carnou.com/blog/storage/framework/sessions/oLvUiN2WplPnoQdyf20sWqKGzls6BGmI5UWwfPtp', 'a:4:{s:6:"_token";s:40:"eZPZ2KDEq9xgURyeabamNdPI6OCsMmU8KYp1HoKh";s:9:"_previous";a:1:{s:3:"url";s:74:"http://carnou.com/post/custom-validation-using-data-from-a-different-table";}s:9:"_sf2_meta";a:3:{s:1:"u";i:1519160010;s:1:"c";i:1519160010;s:1:"l";s:1:"0";}s:6:"_flash";a:2:{s:3:"old";a:0:{}s:3:"new";a:0:{}}}', '2') in Filesystem.php line 111
  4. at Filesystem->put('/opt/bitnami/apps/carnou.com/blog/storage/framework/sessions/oLvUiN2WplPnoQdyf20sWqKGzls6BGmI5UWwfPtp', 'a:4:{s:6:"_token";s:40:"eZPZ2KDEq9xgURyeabamNdPI6OCsMmU8KYp1HoKh";s:9:"_previous";a:1:{s:3:"url";s:74:"http://carnou.com/post/custom-validation-using-data-from-a-different-table";}s:9:"_sf2_meta";a:3:{s:1:"u";i:1519160010;s:1:"c";i:1519160010;s:1:"l";s:1:"0";}s:6:"_flash";a:2:{s:3:"old";a:0:{}s:3:"new";a:0:{}}}', true) in FileSessionHandler.php line 83
  5. at FileSessionHandler->write('oLvUiN2WplPnoQdyf20sWqKGzls6BGmI5UWwfPtp', 'a:4:{s:6:"_token";s:40:"eZPZ2KDEq9xgURyeabamNdPI6OCsMmU8KYp1HoKh";s:9:"_previous";a:1:{s:3:"url";s:74:"http://carnou.com/post/custom-validation-using-data-from-a-different-table";}s:9:"_sf2_meta";a:3:{s:1:"u";i:1519160010;s:1:"c";i:1519160010;s:1:"l";s:1:"0";}s:6:"_flash";a:2:{s:3:"old";a:0:{}s:3:"new";a:0:{}}}') in Store.php line 263
  6. at Store->save() in StartSession.php line 88
  7. at StartSession->terminate(object(Request), object(Response)) in Kernel.php line 177
  8. at Kernel->terminate(object(Request), object(Response)) in index.php line 56