验证增值税号无效

时间:2018-06-02 10:11:40

标签: php laravel

我有一个表格供用户介绍一些数据,增值税号码和国家/地区。当用户输入一些数据并单击"转到步骤2"是对store()方法的ajax请求,这里应根据所选国家验证增值税号。

但它不起作用," dd($request->country);"显示为null。你知道为什么吗?

HTML:

<form method="post" id="step1" action="">

...
<div class="form-group font-size-sm">
        <select class="form-control" name="country" id="country">
            @foreach($countries as $country)
                <option value="">{{$country}}</option>
            @endforeach
        </select>
    </div>
    <div class="form-group font-size-sm">
        <label for="vat"
               class="text-gray">VAT</label>
        <input type="text" id="vat"
               name="vat"
               class="form-control" value="">
    </div>
    ...
</form>

// store()方法

public function store(Request $request, $id, $slug = null, Validator $validator){

      ...

        $rules = [];

        foreach ($request->question_required as $key => $value) {
            $rule = 'string|max:255'; 

            if ($value) {
                $rule = 'required|' . $rule;
            }
            $rules["question.{$key}"] = $rule;
        }

        if ($all) {

            $rules["name.*"] = 'required|max:255|string';
        }

        // the issue is here, country is null
        dd($request->country);
        dd(substr($request->country, 0, 2) . $request->vat);

        //Validator::validate( substr($request->country, 0, 2).$request->vat); // false (checks format + existence)

        $validator = Validator::make($request->all(), $rules, $messages);

        $errors = $validator->errors();
        $errors = json_decode($errors);

        if ($validator->fails()) {
            return response()->json([
                'success' => false,
                'errors' => $errors
            ], 422);
        }
        return response()->json([
            'success' => true,
            'message' => 'success'
        ], 200);

    }

// jQuery that do an ajax request

1 个答案:

答案 0 :(得分:0)

尝试将值传递到您的代码中。 Php没有从您的输入中获取任何值,因此它显示为空