Yehia Ahmed
Yehia Ahmed

Reputation: 37

how to create right validation in Laravel 7?

i have form for customer data and i want to check if phone is exist in database(for both phone and phone 2) and even check for phone2. and if user write the same phone in both field not accepting.

enter image description here

here is my validation code:-

if($request->customer_id == null) {
            unset($rules['customer_id']);
            $rules['customer_name'] = 'required';
            $rules['customer_address'] = 'required';
            $rules['customer_phone'] = 'max:11|required|unique:customers,phone,phone2';
            $rules['customer_phone2'] = 'max:11|unique:customers,phone,phone2';
            $rules['customer_type'] = 'in:regular,special,jomla';
            $mesages['customer_name.required'] = translate('the name is required');
            $mesages['customer_address.required'] = translate('the address is required');
            $mesages['customer_phone.required'] =translate('the phone is required');
            $mesages['customer_phone.max'] =translate('the phone number is not correct');
            $mesages['customer_phone.unique'] =translate('the phone number is used');
            $mesages['customer_phone2.max'] =translate('the phone number is not correct');
            $mesages['customer_phone2.unique'] =translate('the phone number is used');

Upvotes: 1

Views: 52

Answers (1)

Fahri Gunadi
Fahri Gunadi

Reputation: 142

you can try this

    $rules['customer_phone'] = 'required|max:11|unique:customers,phone|unique:customers,phone2';
    $rules['customer_phone2'] = 'required|max:11|unique:customers,phone|unique:customers,phone2|different:customer_phone';

The different rule ensures the phone and phone2 fields contain different values.

Upvotes: 4

Related Questions