laravelvalidationlaravel-formrequest

Laravel how to validate two request inputs againt single column in form request


I am working with laravel 9.1. I want to validate two request inputs in single column of a table. below is my validation rule in form request.

Basically name = resource_name . permission_type i thought of merging both column before the validation but the problem is both fields needs to be validated first before concatination.

Can anyone please help me how do i validate resource_name and permission_type first and then check for resource_name.permission_type in the name column permission_types table.

Thankyou.

$name = $this->request->input('resource_name') . '.' . $this->request->input('permission_type');
$this->request->merge([
    'name' => $name,
]);

return [
    'type' => ['required'],
    'resource_name' => ['required', 'alpha', 'min:3', 'max:50'],
    'permission_type' => ['required'],
    'name' => ''
];

Solution

  • Ok, this is what i did. the name field is an actual field in the database but its data is coming from two different inputs i.e resource_name and permission_type if the field is not present in request the rules won't get triggered and applied, so i create a hidden input type with permission_name in view. and created a custom validator which does the job.

    <input type="hidden" name="permission_name" value="" /> FORM REQUEST:

    $rules = [
        'resource_type' => ['required'],
        'resource_name' => ['required', 'alpha', 'min:3', 'max:50', new IsPlural],
        'permission_type_name_single' => ['required'],
        'permission_name' => new UniqueTwoInputsInOneColumn('permissions', 'name', $this->request->get('resource_name'), $this->request->get('permission_type_name_single'))
    ];
    

    CUSTOM RULE:

    <?php
        namespace App\Rules;
        
        use Illuminate\Contracts\Validation\Rule;
        use Illuminate\Support\Facades\DB;
        
        class UniqueTwoInputsInOneColumn implements Rule
        {
            protected $tableName;
            protected $columnName;
            protected $value1;
            protected $value2;
            protected $ignoreField;
            protected $ignoreFieldValue;
            
            /**
             * Create a new rule instance.
             *
             * @return void
             */
            public function __construct($tableName, $columnName, $value1, $value2, $ignoreField = null, $ignoreFieldValue = null)
            {
                $this->tableName = $tableName;
                $this->columnName = $columnName;
                $this->value1 = $value1;
                $this->value2 = $value2;
                $this->ignoreField = $ignoreField;
                $this->ignoreFieldValue = $ignoreFieldValue;
            }
            
            /**
             * Determine if the validation rule passes.
             *
             * @param string $attribute
             * @param mixed $value
             * @return bool
             */
            public function passes($attribute, $value)
            {
                if (!$this->ignoreField)
                {
                    return DB::table($this->tableName)->where($this->columnName, $this->value1 . '.' . $this->value2)->count() == 0;
                }
                else
                {
                    return DB::table($this->tableName)->where($this->columnName, $this->value1 . '.' . $this->value2)->where($this->ignoreField, '!=', $this->ignoreFieldValue)->count() == 0;
                }
            }
            
            /**
             * Get the validation error message.
             *
             * @return string
             */
            public function message()
            {
                return 'The Permission name ' . $this->value1 . '.' . $this->value2 . ' already exists.';
            }
        }