antonioribeiro / countries

Laravel countries and currencies
BSD 3-Clause "New" or "Revised" License
1.81k stars 282 forks source link

Calling Code Validation #35

Open secrethash opened 7 years ago

secrethash commented 7 years ago

The country code must be a valid callingCode. This is the validation error generated when I am trying to Implement Calling Code Validation in my app. Country Validation works perfectly fine but Calling Code does not. Below is my Implementation:

File: config\countries.php


<?php

return [

    'cache' => [
        'enabled' => true,

        'service' => PragmaRX\Countries\Support\Cache::class,

        'duration' => 180,
    ],

    'hydrate' => [
        'before' => true,

        'after' => true,

        'elements' => [
            'flag' => true,
            'currency' => true,
            'states' => true,
            'timezone' => true,
            'borders' => false,
            'topology' => true,
            'geometry' => true,
            'collection' => true,
        ],
    ],
    'maps' => [
        'lca3' => 'ISO639_3',
        'currency' => 'ISO4217',
    ],

    'validation' => [
        'enabled'    => true,
        'rules'    => [
            'country'            => 'name.common',
            'cca2',
            'cca2',
            'cca3',
            'ccn3',
            'cioc',
            'currency'            => 'ISO4217',
            'language',
            'language_short'    => 'ISO639_3',
            'callingCode',
        ],

File: `app\Http\Controllers...Controller.php

public function validate(array $data) {
        return Validator::make($data, [
            'country' => 'required|alpha|country',
            'country_code' => 'required|callingCode:'.$data['country'],
            'phone' => 'required|numeric|unique:users',
        ]);
}

Note: country field is being successfully validated but country_code is not.

olivernybroe commented 6 years ago

Ah yes, this is an issue which I didn't anticipate. The problem is that callingCode is an array, but all other fields under the rules validation is just basic fields, I can't remember if we ever checked for that.

This is actually poor naming as callingCode should have been callingCodes.

It should be easy to fix the error. We just have to extend how it generates the validations so it supports properties which are arrays too. https://github.com/antonioribeiro/countries/blob/master/src/package/ServiceProvider.php#L102-L115

Is this still something you need?