Laravel使用外部SOAP请求验证字段

时间:2016-09-29 10:10:07

标签: php laravel soap

我使用网站上的.wsdl文件在我的AuthController.php中创建了一个函数soapRequest()来检查增值税号是否存在:

public function soapRequest(Request $request) {

    $countryCode = $request->countryCode;
    $vatNo = $request->vat_number;

    $client = new \SoapClient("http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl");

    $result = $client->checkVat(['countryCode' => $countryCode, 'vatNumber' => $vatNo]);

    $result = ($result->valid == true) ? "VAT number OK" : "VAT number INVALID";


    return response()->json(['msg' => $result], 200);

}

在我看来,我只是写下号码,我可以看看号码是否有效:

<div class="row form-group">
    <div class="col-md-3 col-xs-3">
        {!! Form::select('countryCode', [
                            ''  => '--Land auswählen',
                            'AT' => 'AT-Austria',
                            'BE' => 'BE-Belgium',
                            'BG' => 'BG-Bulgaria',
                            'CY' => 'CY-Cyprus',
                            'CZ' => 'CZ-Czech Republic',
                            'DE' => 'DE-Germany',
                            'DK' => 'DK-Denmark',
                            'EE' => 'EE-Estonia',
                            'EL' => 'EL-Greece',
                            'ES' => 'ES-Spain',
                            'FI' => 'FI-Finland',
                            'FR' => 'FR-France',
                            'GB' => 'GB-United Kingdom',
                            'HR' => 'HR-Croatia',
                            'HU' => 'HU-Hungary',
                            'IE' => 'IE-Ireland',
                            'IT' => 'IT-Italy',
                            'LT' => 'LT-Lithuania',
                            'LU' => 'LU-Luxembourg',
                            'LV' => 'LV-Latvia',
                            'MT' => 'MT-Malta',
                            'NL' => 'NL-The Netherlands',
                            'PL' => 'PL-Poland',
                            'PT' => 'PT-Portugal',
                            'RO' => 'RO-Romania',
                            'SE' => 'SE-Sweden',
                            'SI' => 'SI-Slovenia',
                            'SK' => 'SK-Slovakia'], 
                            null, ['id' => 'countryCode', 'class' => 'form-control']
                        ) !!}
        </div>
    <div class="col-md-2 col-xs-2">
        {!! Form::text('code', null, ['id' => 'code', 'disabled' => 'disabled', 'class' => 'form-control']) !!}
    </div>
    <div class="col-md-3 col-xs-3">
        {!! Form::text('vat_number', null, ['id' => 'vat', 'class' => 'form-control']) !!}
    </div>
    <div class="col-md-4 col-xs-4">
        {!! Form::text('msg', null, ['id' => 'msg', 'disabled' => 'disabled', 'class' => 'form-control']) !!}
    </div>
</div>

的javascript

<script type="text/javascript">

    $(document).on('change', '#countryCode', function () {
        $('input[id=code]').val($('#countryCode').val());
    });

    $(document).on('input paste', '#vat, #countryCode', function () {
        $.ajax({
            type:'POST',
            url:'/soap',
            headers: {'X-CSRF-TOKEN': '{{ csrf_token() }}' },
            data: $('#countryCode, #vat').serialize(),
                success:function(data){
                    $('input[id=msg]').val(data.msg);
                }
            });
    });

</script>

实际上我并不需要它像这样工作....我只需要验证/验证用户是否已在增值税号输入字段中输入现有号码。

所以问题是:

如何验证BACKEND中的单个输入字段,以便用户无法输入非现有的增值税号?

enter image description here

1 个答案:

答案 0 :(得分:1)

因此,您可以使用Laravel进行基本验证,现在您可以通过SOAP请求扩展验证:像这样:

$validation = [
    'name' => 'required|max:255',
    'email' => 'required|confirmed|email|max:255|unique:users',
    'password' => 'required|confirmed|min:6',     
    ...

$YourFormValidation = Validator::make($data,$validation);

if( $YourFormValidation->passes() ) { 
  // so all your data seems valid, check out VAT

  $countryCode = $request->countryCode;
  $vatNo = $request->vat_number;

  $client = new \SoapClient("http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl");

  $result = $client->checkVat(['countryCode' => $countryCode, 'vatNumber' => $vatNo]);

  if($result->valid == true) {
     // everyhting is correct, save data to database (or whatever)
  } else {
     // VAT seemed correct, but Webservice returns false. Show user an Information that the VAT is not correct.
  }

} else { 
    //code for validation failure
}