在尝试验证 Laravel 中的唯一字段时出现错误。

huangapple go评论59阅读模式
英文:

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-&gt;validate([
&#39;CurrencyName&#39; =&gt; [&#39;unique:tblcurrencies&#39;, &#39;max:50&#39;]
],[
&#39;CurrencyName.unique&#39; =&gt; &#39;err1!&#39;,
&#39;CurrencyName.max&#39; =&gt; &#39;err2!&#39;
]); 
Currency::create([
&#39;CurrencyRef&#39; =&gt; Helper::GetNewRef(),
&#39;CurrencyName&#39; =&gt; $request[&#39;txtcurrencyname&#39;],
&#39;CurrencyPrice1&#39; =&gt; $request[&#39;txtcurrencyprice1&#39;],
&#39;CurrencyPrice2&#39; =&gt; $request[&#39;txtcurrencyprice2&#39;],
&#39;CurrentUser&#39; =&gt; auth()-&gt;id()
]);
}

in the view file I inserted:

@if ($errors-&gt;any())
&lt;div class=&quot;alert alert-danger&quot;&gt;
&lt;ul&gt;
@foreach ($errors-&gt;all() as $error)
&lt;li&gt;{{ $error }}&lt;/li&gt;
@endforeach
&lt;/ul&gt;
&lt;/div&gt;
@endif

and the form:

&lt;form class=&quot;needs-validation was-validated&quot; action=&quot;{{ route(&#39;currencies.store&#39;) }}&quot; method=&quot;POST&quot;&gt;
@csrf
&lt;div class=&quot;row row-sm&quot;&gt;
&lt;div class=&quot;col-lg-12&quot;&gt;
&lt;div class=&quot;form-group row&quot;&gt;
&lt;label for=&quot;txtcurrencytype&quot; class=&quot;col-lg-2 col-form-label&quot;&gt;Type&lt;/label&gt;
&lt;div class=&quot;col-lg-10&quot;&gt;
&lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;txtcurrencytype&quot; name=&quot;txtcurrencytype&quot; value=&quot;forg&quot; disabled&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;form-group row&quot;&gt;
&lt;label for=&quot;txtcurrencyname&quot; class=&quot;col-lg-2 col-form-label&quot;&gt;currency name&lt;/label&gt;
&lt;div class=&quot;col-lg-10&quot;&gt;
&lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;txtcurrencyname&quot; name=&quot;txtcurrencyname&quot; placeholder=&quot;currency name&quot; required&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;form-group row&quot;&gt;
&lt;label for=&quot;txtcurrencyprice1&quot; class=&quot;col-lg-2 col-form-label&quot;&gt;price1&lt;/label&gt;
&lt;div class=&quot;col-lg-10&quot;&gt;
&lt;input type=&quot;number&quot; class=&quot;form-control&quot; id=&quot;txtcurrencyprice1&quot; name=&quot;txtcurrencyprice1&quot; step=&quot;any&quot; required&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;form-group row&quot;&gt;
&lt;label for=&quot;txtcurrencyprice2&quot; class=&quot;col-lg-2 col-form-label&quot;&gt;price2&lt;/label&gt;
&lt;div class=&quot;col-lg-10&quot;&gt;
&lt;input type=&quot;number&quot; class=&quot;form-control&quot; id=&quot;txtcurrencyprice2&quot; name=&quot;txtcurrencyprice2&quot; step=&quot;any&quot; required&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;modal-footer&quot;&gt;
&lt;button class=&quot;btn ripple btn-primary&quot; type=&quot;submit&quot;&gt;save&lt;/button&gt;
&lt;button class=&quot;btn ripple btn-secondary&quot; data-dismiss=&quot;modal&quot; type=&quot;button&quot;&gt;cancel&lt;/button&gt;
&lt;/div&gt;
&lt;/form&gt;

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 = &#39;tblcurrencies&#39;;
protected $fillable = [
&#39;CurrencyRef&#39;,
&#39;CurrencyName&#39;,
&#39;CurrencyPrice1&#39;,
&#39;CurrencyPrice2&#39;,
&#39;CurrentUser&#39;,
];
}

答案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.

huangapple
  • 本文由 发表于 2023年6月25日 20:52:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76550491.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定