为什么代码不输入“ if($ country == $ request-> country)”?

时间:2018-07-30 00:45:09

标签: php laravel

我有以下方法来更新用户的增值税号。

但是,当用户在“增值税”字段中输入一些数字并单击“更新”按钮时,它将显示“ Undefined variable: countryKey”。

问题似乎在于,该代码永远不会输入“ if ($country == $request->country) { “。但是你知道为什么吗?

您知道可能是什么问题吗?

HTML类似于:

<form method="post" action="{{route('user.updateGeneralInfo')}}" class="clearfix">
...
    <div class="form-row">
        <div class="form-group col-12 col-md-6">
            <label for="vat">VAT:</label>
            <input type="text" value="{{$user->VAT}}" name="vat" class="form-control"
                   id="vat"
                    ">
        </div>
    </div>
...

    <div class="form-group col-md-6">
    <label for="country">Country</label>
    <select class="form-control" name="country" id="country">
        @if($user->country)
            <option selected="selected">{{$user->country}}</option>
        @endif
        @foreach($countries as $key => $country)
            <option value="{{$key}}">{{ $country}}</option>
        @endforeach
    </select>
</div>

</form>

updateGeneralInfo()类似于:

public function updateGeneralInfo(Request $request){
        $rules = [
            'name' => 'required',
            'surname' => 'required',
        ];

        $customMessages = [
            'name.required' => 'The field name is mandatory.',
            'surname.required' => 'The field surname is mandatory.'
        ];

        if (isset($request->vat)) {

            $countries = Countries::all(); // get all countries using a package method
            // $country is equal to the current $country in the $countries array

            while ($country = current($countries)) {
            // if the $country is equal to the country selected by the user
                if ($country == $request->country) {
                    dd($country. '  ' .$request->country);

                  // the $countryKey is the key of the array in the $countries array. For example if the user selects the country "Germany" the $countryKey should be "DE"
                    $countryKey = (key($countries));
                }
                // if is not equal continue until it is equal
                next($countries);
            }

            $rules['vat'] = [
                function ($attr, $value, $fail) use ($request, $countryKey) {
                    if (!VatValidator::validateFormat($countryKey . $request->vat)) {
                        $fail('Please insert a valid VAT.');
                    }

                }];
        }

        $this->validate($request, $rules, $customMessages);


        $user = Auth::user();
        $user->name = $request->name;
        $user->surname = $request->surname;

        $countries = Countries::all();

        if($request->country != $user->country){
            $user->country = $countries[$request->country];
        }

        $user->VAT = $request->vat;
        $user->save();

        Session::flash('general_success', 'Info updated with success.');

        return redirect(route('user.index', ['user' => Auth::id()]) . '#generalInfo');
    }

1 个答案:

答案 0 :(得分:0)

您要将$countryKey的声明包装在if块中。如果该块返回false,则$countryKey不会被定义或在稍后调用时具有值,并且会出错。在if块之外声明变量,您的问题应解决。