英文:
error when I try to validate unique field in Laravel
问题
以下是您的代码的中文翻译部分:
我正尝试在 Laravel 中进行验证:
public function store(Request $request)
{
$validatedData = $request->validate([
'CurrencyName' => ['unique:tblcurrencies', 'max:50']
],[
'CurrencyName.unique' => 'err1!',
'CurrencyName.max' => 'err2!'
]);
Currency::create([
'CurrencyRef' => Helper::GetNewRef(),
'CurrencyName' => $request['txtcurrencyname'],
'CurrencyPrice1' => $request['txtcurrencyprice1'],
'CurrencyPrice2' => $request['txtcurrencyprice2'],
'CurrentUser' => auth()->id()
]);
}
在视图文件中插入了以下内容:
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
以及表单部分的代码:
<form class="needs-validation was-validated" action="{{ route('currencies.store') }}" method="POST">
@csrf
<div class="row row-sm">
<div class="col-lg-12">
<div class="form-group row">
<label for="txtcurrencytype" class="col-lg-2 col-form-label">Type</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="txtcurrencytype" name="txtcurrencytype" value="forg" disabled>
</div>
</div>
<div class="form-group row">
<label for="txtcurrencyname" class="col-lg-2 col-form-label">currency name</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="txtcurrencyname" name="txtcurrencyname" placeholder="currency name" required>
</div>
</div>
<div class="form-group row">
<label for="txtcurrencyprice1" class="col-lg-2 col-form-label">price1</label>
<div class="col-lg-10">
<input type="number" class="form-control" id="txtcurrencyprice1" name="txtcurrencyprice1" step="any" required>
</div>
</div>
<div class="form-group row">
<label for="txtcurrencyprice2" class="col-lg-2 col-form-label">price2</label>
<div class="col-lg-10">
<input type="number" class="form-control" id="txtcurrencyprice2" name="txtcurrencyprice2" step="any" required>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button class="btn ripple btn-primary" type="submit">save</button>
<button class="btn ripple btn-secondary" data-dismiss="modal" type="button">cancel</button>
</div>
</form>
但记录被保存(即使输入了重复的货币名称)并将我重定向到相同的页面,但是页面是白色的!我尝试解决这个问题,但无法解决。以下是模型文件:
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Currency extends Model
{
use HasFactory;
protected $table = 'tblcurrencies';
protected $fillable = [
'CurrencyRef',
'CurrencyName',
'CurrencyPrice1',
'CurrencyPrice2',
'CurrentUser',
];
}
请注意,您的代码中似乎存在问题,导致验证失败时仍然创建了记录并且无法正确处理错误。您可能需要检查 Laravel 的验证规则以及在保存之前检查验证是否成功,以避免创建重复记录。
英文:
I'm trying to validate in Laravel as:
public function store(Request $request)
{
$validatedData = $request->validate([
'CurrencyName' => ['unique:tblcurrencies', 'max:50']
],[
'CurrencyName.unique' => 'err1!',
'CurrencyName.max' => 'err2!'
]);
Currency::create([
'CurrencyRef' => Helper::GetNewRef(),
'CurrencyName' => $request['txtcurrencyname'],
'CurrencyPrice1' => $request['txtcurrencyprice1'],
'CurrencyPrice2' => $request['txtcurrencyprice2'],
'CurrentUser' => auth()->id()
]);
}
in the view file I inserted:
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
and the form:
<form class="needs-validation was-validated" action="{{ route('currencies.store') }}" method="POST">
@csrf
<div class="row row-sm">
<div class="col-lg-12">
<div class="form-group row">
<label for="txtcurrencytype" class="col-lg-2 col-form-label">Type</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="txtcurrencytype" name="txtcurrencytype" value="forg" disabled>
</div>
</div>
<div class="form-group row">
<label for="txtcurrencyname" class="col-lg-2 col-form-label">currency name</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="txtcurrencyname" name="txtcurrencyname" placeholder="currency name" required>
</div>
</div>
<div class="form-group row">
<label for="txtcurrencyprice1" class="col-lg-2 col-form-label">price1</label>
<div class="col-lg-10">
<input type="number" class="form-control" id="txtcurrencyprice1" name="txtcurrencyprice1" step="any" required>
</div>
</div>
<div class="form-group row">
<label for="txtcurrencyprice2" class="col-lg-2 col-form-label">price2</label>
<div class="col-lg-10">
<input type="number" class="form-control" id="txtcurrencyprice2" name="txtcurrencyprice2" step="any" required>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button class="btn ripple btn-primary" type="submit">save</button>
<button class="btn ripple btn-secondary" data-dismiss="modal" type="button">cancel</button>
</div>
</form>
but the record is saved (even if I enter duplicated currency name) and redirects me to the same page but as white page!!
I tried to solve this but I couldn't
and that's the model file:
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Currency extends Model
{
use HasFactory;
protected $table = 'tblcurrencies';
protected $fillable = [
'CurrencyRef',
'CurrencyName',
'CurrencyPrice1',
'CurrencyPrice2',
'CurrentUser',
];
}
答案1
得分: 2
我认为您只是在表单上的名称属性和您的验证器所期望的之间有轻微的差异。
数组键必须与此处的名称字段匹配:
<input type="text" class="form-control" id="txtcurrencyname" name="txtcurrencyname" placeholder="currency name" required>
但是您的规则数组告诉验证器要检查一个名为"CurrencyName"的输入。
调整验证器数组或输入名称属性,以使它们相应匹配。
英文:
I think you just have slight differences between what the name properties on the form are and what your validator is expecting.
The array key is going to have to match the name field here:
<input type="text" class="form-control" id="txtcurrencyname" name="txtcurrencyname" placeholder="currency name" required>
But your rules array is telling the validator to check against an input named "CurrencyName".
Adjust the validator array or the input name properties so that they match up accordingly.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论