Search code examples
phplaravellaravel-validationlaravel-request

Laravel request validation in shallow nested resource


I have two resources

  • Organizations

  • OrganizationUsers

Given that I want to create in both resources and that the creation must follow specific requirements, I'm using Request rules() and attributes().

For instance, since organizations need to have a unique name, I'm using

public function rules()
    {
        return [
            'name' => [
                'required', 'min:3', Rule::unique((new Organization)->getTable())->ignore($this->route()->organization->id ?? null)
            ],
            (...)
        ];
    }

and that works fine. Adapting the same process to validate organization users

class OrganizationUserRequest extends FormRequest
{
     
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'user_id' => [
                'required', 'exists:'.(new User)->getTable().',id', Rule::unique((new OrganizationUser)->getTable())->ignore($this->route()->user->id ?? null)
            ],
            (...)
        ];
    }

    /**
     * Get the validation attributes that apply to the request.
     *
     * @return array
     */
    public function attributes()
    {
        return [
            'user_id' => 'user',
            (...)
        ];
    }
}

then whatever user I try to add will get me a

The user has already been taken.

enter image description here

no matter which user is used.


The OrganizationUser model is the

class OrganizationUser extends Model
{
    protected $fillable = [
        'user_id', (...)
    ];

    /**
     * Get the user
     *
     * @return \App\User
     */
    public function user()
    {
        return $this->belongsTo(User::class);
    }

    (...)

}

Also, the OrganizationUserController is as

/**
 * Show the form for creating a new organization
 *
 * @return \Illuminate\View\View
 */
public function create($id, Organization $model1, User $model2)
{

    return view('organizations.users.create', [
        'id'=> $id,
        'organizations' => $model1::where('id',$id)->get(['id', 'name']),
        'portal_users' => $model2->all(),
        ]);
    
}

and that field in create.blade.php

<div class="form-group{{ $errors->has('user_id') ? ' has-danger' : '' }}">
    <label class="form-control-label" for="input-user_id">{{ __('User') }}</label>
    <select name="user_id" id="input-organization" class="form-control{{ $errors->has('user_id') ? ' is-invalid' : '' }}" placeholder="{{ __('User') }}" required>
        @foreach ($portal_users as $portal_user)
            <option value="{{ $portal_user->id }}" {{ $portal_user->id == old('user_id') ? 'selected' : '' }}>{{ $portal_user->name }}</option>
        @endforeach
    </select>
    @include('alerts.feedback', ['field' => 'user_id'])
</div>

Solution

  • In order to solve it, I've gone through the next steps

    1. Create a provider
    php artisan make:provider UniqueOrgUserServiceProvider
    
    1. In that provider
    <?php
    
    namespace App\Providers;
    
    use App\OrganizationUser;
    use Illuminate\Support\ServiceProvider;
    
    class UniqueOrgUserServiceProvider extends ServiceProvider
    {    
        /**
         * Bootstrap services.
         *
         * @return void
         */
        public function boot()
        {
            \Validator::extend('uniqueorguser', 
                        function($attribute, $value, $parameters, $validator)
            {
                $value1 = (int)request()->get($parameters[0]);
                if (is_numeric($value) && is_numeric($value1))
                {
                    return (!(OrganizationUser::where($attribute, $value)
                        ->where($parameters[0], $value1)
                        ->count() > 0));
                }
                return false;
            });
        }
    
        /**
         * Register services.
         *
         * @return void
         */
        public function register()
        {
            //
        }
    
    }
    
    1. Register the provider in config/app.php
    'providers' => [
    
            /*
             * Application Service Providers...
             */
            App\Providers\UniqueOrgUserServiceProvider::class,
    
        ],
    
    1. Change the OrganizationUserRequest to
    class OrganizationUserRequest extends FormRequest
    {
        /**
         * Determine if the user is authorized to make this request.
         *
         * @return bool
         */
        public function authorize()
        {
            return auth()->check();
        }
    
        /**
         * Get the validation rules that apply to the request.
         *
         * @return array
         */
        public function rules()
        {
            return [
                'name' => [
                    'required', 'min:3'
                ],
                'is_admin' => [
                    'required','boolean'
                ],
                'organization_id' => [
                    'required', 'exists:'.(new Organization)->getTable().',id'
                ],
                'user_id' => [
                    'required', 'uniqueorguser:organization_id', 'exists:'.(new User)->getTable().',id',
                ]
            ];
        }
    
        /**
         * Get the validation attributes that apply to the request.
         *
         * @return array
         */
        public function attributes()
        {
            return [
                'user_id' => 'user',
                'organization_id' => 'organization'
            ];
        }
    
        public function messages()
        {
            return [
                'user_id.uniqueorguser' => 'The user already exists in this organization!'
            ];
        }
    }
    

    This answer is based in this one.