0

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   
 ---------------------------
1
  • can u put the code about what have you done so far? Commented Dec 3, 2016 at 11:32

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Have any way to use default validation system in laravel
1

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)

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.