Laravel验证有效载荷中的JSON数据

时间:2019-04-04 14:37:21

标签: php laravel laravel-5

我想知道如何在Laravel中验证我的json数据。

我有这样的数据

Payload

在控制器中,我要求这样

request('payment')['cardnumber']

我如何验证这一点。

目前,我有一个像这样的RequestValidator类

public function rules()
    {
        return [
            'billing_address' => 'required',
            'town' => 'required',
            'county' => 'required',
        ];
    }

这是我的控制人

public function ProcessAdvertPayment(Property $property, Request $request, PropertyPaymentRequest $propertyPaymentRequest)
{

    $payload = $request->input('payload', false);
    $nonce = $payload['nonce'];

    $payment = Braintree_Transaction::sale([
        'amount' => 1,
        'paymentMethodNonce' => $nonce,
        'creditCard' => ['number' => request('payment')['cardnumber'], 'expirationDate' => request('payment')['month'] . '/' . request('payment')['year'], "cvv" => request('payment')['cvv']],
    ]);

    if($payment->success)
    {
        $property->payment()->create(['amount' => $payment->transaction->amount, 'braintree_transaction_id' => $payment->transaction->id, 'billing_address' => request('payment')['billing_address'], 'town' => request('payment')['town'], 'county' => request('payment')['county']]);
        return response()->json($payment);
    } 

    return response()->json(['error' => 'Payment Failed. Please try again or contact your payment provider for further help.'], 400);
}

1 个答案:

答案 0 :(得分:0)

我建议您遵循有关Form Validation Requests的Laravel教程。它展示了如何检索请求的验证数据。

如果您要验证已提交的JSON有效负载的属性,则可以给验证者一个诸如payment.country之类的密钥(/字段)名称,以验证付款的国家/地区。