Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to validate Indian phone numbers #81

Closed
harikrishnanng opened this issue Mar 7, 2018 · 5 comments
Closed

Unable to validate Indian phone numbers #81

harikrishnanng opened this issue Mar 7, 2018 · 5 comments
Labels

Comments

@harikrishnanng
Copy link

harikrishnanng commented Mar 7, 2018

Unable to validate some Indian mobile numbers starting with 81,83,85 etc. Gives the error:
{ "message": "The given data was invalid.", "errors": { "phone_no": [ "The phone number field contains an invalid number." ] } }

//validator in RegisterController.php


protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|string|max:255',
            'email' => 'required|string|email|max:255|unique:users',
            'user_name' => 'required|string|min:6|unique:users',
            'gender' => 'required|string|in:male,female,other',
            'password' => 'required|string|min:6|confirmed',
            'cc' => 'required', 
        ])->setAttributeNames([
            'phone_no' => 'phone number',
            'cc' => 'country code'
        ]);
    }

cc is the country code passed from UI (IN,US etc), phone_no is just 10 digits

//registration func


public function register(Request $request)
{
    $this->validator($request->all())->sometimes('phone_no', 'required|phone:'.mb_strtoupper($request->cc).',mobile|unique:users', function ($input){
        return $input->cc;
    })->validate();

    event(new Registered($user = $this->create($request->all())));

    $this->guard()->login($user);

    if($request->isJson() && !empty(Auth::user())) {
        return response()->json(['code' => 200, 'token' => Auth::user()->api_token,'role' => Auth::user()->getRoleNames(), 'message' => 'success']);
    }

    return $this->registered($request, $user)
    ?: redirect($this->redirectPath());
}

//numbers used
81/83/8590332334
same number starting with 82,84,90,70 are accepted

@Propaganistas
Copy link
Owner

Propaganistas commented Mar 7, 2018

If you expect to receive support you might want to include some example numbers and sample code.

@harikrishnanng
Copy link
Author

I'm using the latest version

@Propaganistas
Copy link
Owner

The number you supplied validates just fine using IN as country. So there's probably something wrong with your code. Dump the value of $request->cc to check if it really holds IN.

Additionally, you might want to use the built-in functionality to using the country field instead of directly inlining $request->cc in the validation rule. Just reference the country field's name.

$this->validator($request->all())->sometimes('phone_no', 'required|phone:cc,mobile|unique:users', function ($input){
    return $input->cc;
})->validate();

@harikrishnanng
Copy link
Author

harikrishnanng commented Mar 8, 2018

capture


Validator::make($request->all(), [
            'phone_no' => 'required|phone:cc,mobile|unique:users'
        ])->setAttributeNames(['phone_no' => 'phone number'])->validate();

can you provide the code you used to check?

@Propaganistas
Copy link
Owner

Bug found and fixed in 79b7901. New release created. Thanks for reporting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants