PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, March 18, 2022

[FIXED] Unique validation across two fields

 March 18, 2022     laravel, laravel-5, php, validation     No comments   

Issue

I'm building a website in Laravel 5 where a user can create a character by choosing a first name and a last name.

I've built some validation rules in around this, but am a little stumped on implementing unique validation as I need it to validate their full name, which is across two columns in my database. For example, a user can create a character called "Jon Snow", but the validation would need to check that the combination of those two fields was unique as someone else may want to create the character "Jon Doe".

I realise now whilst I write this that I could just combine the two columns in to one, and then have validation working on that.

But before I go down that route, is there any way to run validation across two fields like I need?

Below is my validation:

public function store(Request $request)
    {
        $character = new Characters;

         $validator = Validator::make($request->all(), [
            'firstname' => 'required|between:2,15',
            'lastname' => 'required|between:2,15',
        ], [
            'firstname.required' => 'You need to provide a first name!',
            'lastname.required' => 'You need to provide a last name!',
            'firstname.between' => 'The first name must be between :min - :max characters long.',
            'lastname.between' => 'The last name must be between :min - :max characters long.',
        ]);

Solution

Have a look at this package felixkiss/uniquewith-validator. It contains a variant of the validateUnique rule for Laravel, that allows for validation of multi-column UNIQUE indexes.



Answered By - Pᴇʜ
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing