gpt4 book ai didi

laravel 和请求验证

转载 作者:行者123 更新时间:2023-12-04 17:26:31 24 4
gpt4 key购买 nike

我无法在 Laravel 中正确设置验证。除了其他功能,我在 Controller 中有这个

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Exports\RimborsiExport;

use DB;
use App\Customers;
use App\Claims;
use App\Refunds;
use App\Services;
use App\Http\Requests\RefundsPost;

use Excel;
use DataTables;
use Redirect;
use Response;

class RefundsController extends Controller
{
public function storeRefundsData(RefundsPost $request){
dd($request);

//$validated = $request->validated();
$customers = Customers::create($request->all());
return back()->with('status', 'Dati Cliente inseriti correttamente');
}

}

我还定义了一个自定义请求类型

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class RefundsPost extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
$rules = [
'contr_nom' => 'required|max:255',
'contr_cog' => 'required',
'polizza' => 'required',
'email' => 'required',
'targa' => 'required',
'iban' => 'required|iban',
'int_iban' => 'required',
];
return $rules;
}

public function messages()
{
return [
'contr_nom.required' => "Il Nome Contraente e' obbligatorio",
'contr_cog.required' => "Il Cognome Contraente e' obbligatorio",
'polizza.required' => "Il numero di polizza e' obbligatorio",
'email.required' => "Una mail e' obbligatoria per le comunicazioni",
'targa.required' => "La targa e' obbligatoria",
'data_sin.required' => "La data sinistro e' obbligatoria",
'iban.required' => "Il numero IBAN e' obbligatorio",
'int_iban.required' => "L'intestatario dell' IBAN e' obbligatorio",
'dossier.required' => "Il numero di dossier e' obbligatorio",
'cliente.required' => "Il cliente e' obbligatorio",
'stato.required' => "Lo stato del rimborso e' obbligatorio",
'date_ref.required' => "La data della richiesta e' obbligatoria",
];
}
}

我有这个 Blade ,里面有表格

    <div class="container-fluid">
<form method="POST" action="{{ route('storeRefundsData') }}" novalidate>

{{ csrf_field() }}

<h5 class="mb-3">Anagrafica</h5>
<div class="row">
<div class="col-md-6 mb-3">
<label for="contr_nom">Nome Contraente</label>
<input type="text" class="form-control @error('contr_nom') is-invalid @enderror" id="contr_nom" name="contr_nom" value="{{old('contr_nom')}}">
@error('contr_nom')
<div class="invalid-feedback">{{ $message }}</div>
@enderror
</div>
<div class="col-md-6 mb-3">
<label for="contr_cog">Cognome Contraente</label>
<input type="text" class="form-control @error('contr_cog') is-invalid @enderror" id="contr_cog" name="contr_cog" value="{{old('contr_cog')}}">
@error('contr_cog')
<div class="invalid-feedback">{{ $message }}</div>
@enderror
</div>
</div>

<div class="row">
<div class="col-md-6 mb-3">
<label for="polizza">Numero Polizza <span class="text-muted"></span></label>
<input type="text" class="form-control @error('polizza') is-invalid @enderror" id="polizza" name="polizza" value="{{old('polizza')}}">
@error('polizza')
<div class="invalid-feedback">{{ $message }}</div>
@enderror
</div>

<div class="col-md-6 mb-3">
<label for="email">Email <span class="text-muted"></span></label>
<input type="email" class="form-control @error('email') is-invalid @enderror" id="email" name="email" placeholder="tu@esempio.it" value="{{old('email')}}">
@error('email')
<div class="invalid-feedback">{{ $message }}</div>
@enderror
</div>
</div>

<div class="row">
<div class="col-md-6 mb-3">
<label for="targa">Targa veicolo</label>
<input type="text" class="form-control @error('targa') is-invalid @enderror" id="targa" name="targa" placeholder="Inserisci la targa" value="{{old('targa')}}">
@error('targa')
<div class="invalid-feedback">{{ $message }}</div>
@enderror
</div>
</div>

<h5 class="mb-3">Dati bancari</h5>
<div class="row">
<div class="col-md-6 mb-3">
<label for="iban">IBAN <span class="text-muted"></span></label>
<input type="text" class="form-control @error('iban') is-invalid @enderror" id="iban" name="iban" placeholder="Inserisci il tuo IBAN" value="{{old('iban')}}">
@error('iban')
<div class="invalid-feedback">{{ $message }}</div>
@enderror
</div>

<div class="col-md-6 mb-3">
<label for="int_iban">Intestatario IBAN <span class="text-muted"></span></label>
<input type="text" class="form-control @error('int_iban') is-invalid @enderror" id="int_iban" name="int_iban" placeholder="Inserisci l'intestatario dell'IBAN" value="{{old('int_iban')}}">
@error('int_iban')
<div class="invalid-feedback">{{ $message }}</div>
@enderror
</div>
</div>

<hr class="mb-4">
<!-- <button class="btn btn-primary btn-lg btn-block" type="submit">Continue to checkout</button>-->
<input type="submit" class="btn btn-primary btn-lg btn-block" value="Salva">

@if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
@endif
</form>
</div>

我不明白为什么当我点击表单提交按钮在数据库中创建一个新行时,它似乎没有进入 Controller 功能。如果我使用 Request 而不是自定义类型 RefundsPost 它可以工作并将数据存储在数据库中但显然没有验证。我该如何解决?非常感谢

最佳答案

不确定这是否能解决您的问题,但我已经按照我的方式重写了您的函数。您可以指定规则适用的 http 方法,在本例中我指定了 POST 请求的规则。

RefundsPost -> 规则:

public function rules() {
$rules = [];

switch($this->method()) {
case 'POST':
{
$rules = [
'contr_nom' => 'required|max:255',
'contr_cog' => 'required',
'polizza' => 'required',
'email' => 'required',
'targa' => 'required',
'iban' => 'required|iban',
'int_iban' => 'required',
];
}

default:
break;
}

return $rules;
}

并且在 storeRefundsData 函数中,您应该使用 $request->validated() 并在您继续插入时使用它在验证后返回的属性。

RefundsController -> storeRefundsData:

public function storeRefundsData(RefundsPost $request) {

$attributes = $request->validated();

$customers = Customers::create($attributes);

if (!($customers instanceof Customers)) {
// could not create customer
return ['result' => false];
}

$customers = $customers->fresh(); // if you need to retrieve the objects id

return ['result' => true];
}

关于laravel 和请求验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62936963/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com