0

I have a form. Type field is a drop-down and has only two possible values. Second field is bcode.
E.g. if table has data like-

ID  TYPE  BCODE
1     1     2
2     1     3
3     2     2
4     1     4
5     2     2

Now I am making a popup form to insert record in this table. So if type is entered 1 and bcode is entered 2,3 or 4 its already in table so it will be invalid but any other numeric value will be valid. Similarly if type is entered 2 then bcode can't be anything other than 2,3 or 4. All other values will be invalid.

I read the Laravel docs here-
https://laravel.com/docs/6.x/validation#custom-validation-rules
But did not find anything.
Anyone tried something similar?

2
  • I dont unserstand your question
    – sta
    Jul 16, 2020 at 10:57
  • 1
    It seems like you are trying to make a custom validation rule. How far along did you get? php artisan help make:rule will help you understand how to use the built in generator for this. That will put a new rule class in the app/Rules folder where you can implement what you need. Jul 16, 2020 at 11:15

1 Answer 1

1

Assuming your table structure is like in your example and its called type_bcode, I'm offering the following solution:

  1. Create custom rule by running php artisan make:rule TypeBcode
    It should look like this:

    private $type;
    
    public function __construct($type)
    {
        $this->type = $type;
    }
    public function passes($attribute, $value)
    {
        $type_bcode = DB::table('type_bcode')
            ->where('type', $this->type)
            ->get()
            ->pluck('bcode')
            ->toArray();
    
        return in_array($value, $type_bcode);
    }
    public function message()
    {
        return 'The validation error message.';
    }
    
  2. In your controllers store function do this:

    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'type' => 'required',
            'bcode' => ['required', new TypeBcode($request->type)],
        ]);
        if ($validator->fails()) {
            //some action
        }
        //some action
    }
    

Hope this will help

2
  • Not exactly what I was expecting but logic is correct.I did as bellow & it worked- $typeset = array_unique(DB::table('type_bcode')->get()->pluck('bcode')->toArray()); if($this->type == 1 && in_array($value, $typeset)){ return false; } elseif($this->type == 2) { return in_array($value, $typeset); } else{ return true; } Only I was unable to get to work was unique values with distinct.Thats why I used array_unique function.If you have a better way please suggest.Thanks for your help.
    – Rohit S
    Jul 18, 2020 at 4:48
  • Validator side code is pretty much same so not included in my comment.
    – Rohit S
    Jul 18, 2020 at 4:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.