lewis4u lewis4u - 2 months ago 7
PHP Question

Laravel validate field with external SOAP request

I have created a function soapRequest() in my AuthController.php using .wsdl file from a website to check if VAT number exists:

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);

}


and in my view i just write the number and i can see if the number is valid or not:

<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>


And actually i don't need it to work like this....i just need to verify/validate if user has inputted the existing number in that VAT Number input field.

So the question is:

How to validate that single input field in BACKEND so the user cannot input a non existing VAT number?

enter image description here

Answer

So you have your base Validation with Laravel, and now you can extend the validation by your SOAP Request: Like this:

$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
}