Al-Amin
Al-Amin

Reputation: 798

Conditional validation in Laravel

How i validate the table, if privacy=public then all user has unique title but privacy=private then every single user has a unique title.

 ---------------------------
 user_id | title | privacy
 ---------------------------
    2    | hello | public
 ---------------------------
    2    | hello | private
 ---------------------------
    2    | hello | private   **Error**
 ---------------------------
    2    | hello | public    **Error**
 ---------------------------
    3    | hello | public    **Error**
 ---------------------------
    3    | hello | private   
 ---------------------------

Upvotes: 0

Views: 1102

Answers (3)

Al-Amin
Al-Amin

Reputation: 798

Hey i can solve my question after a lot of try!, Thanks Everyone who help me or suggest me

Mostly like my own solution

        'title' => Rule::unique('galleries')->where(function ($query) 
        {
            if($this->input('privacy')=='private')
            {
                $query->where([['privacy','=','private'],['user_id','=',Auth::user()->id]]);
            }
            else
                 $query->where('privacy', '=','public');

        }),

Hopefully this is the most easy solution

Upvotes: 1

lephleg
lephleg

Reputation: 1764

You will need a custom validator for this, which basically will be using the built-in unique rule based on privacy condition:

class CustomValidator extends Illuminate\Validation\Validator
{
   public function validateUniqueIfPrivacy($attribute, $value, $parameters) {

      $privacyValue = array_get($validator->getData(), 'privacy_field');

      if ($privacyValue == 'private' ) {
         return $isTitleUniqueForUser = $this->validateUnique($attribute, $value, 'my_table', 'title', NULL, 'user_id', $parameters[0]);
      } else {
         return $isTitleUniqueForAll = $this->validateUnique($attribute, $value, 'my_table', 'title');
      }

   }
}

After you have registered your custom validator and autoloaded its class, you can use it like this, by passing only the $userId as parameter:

$rules = array(
        'title' => 'unique_if_privacy:,' . $user->id,
);

More info about how to implement a custom validator: Laravel 4.2 documentation (also available for Laravel 5)

Upvotes: 1

kapilpatwa93
kapilpatwa93

Reputation: 4411

May be you can use this library if you want to perform in Validator itself

url : https://github.com/felixkiss/uniquewith-validator

Alternate Solution :

if($request->privacy == "private"){
   $count = DB::table('your_table_name')
                ->where('title','=',$request->title)
                ->where('user_id','=,$request->user_id)
                ->count();
   if($count >0){
      return "You error message for privacy private"
    }
}else{
    $count = DB::table('your_table_name')
                ->where('title','=',$request->title)
                ->count();
   if($count >0){
      return "You error message for privacy public"
    }

}

hope so you understand this simple code. ask if any doubt.

Upvotes: 1

Related Questions