gpt4 book ai didi

validation - 在 Blade 中调用 @if ($errors->has()) 时,调用数组上的成员函数 has()

转载 作者:行者123 更新时间:2023-12-02 21:21:00 25 4
gpt4 key购买 nike

我正在尝试使用 Ardent 从 Blade 模板中的表单返回错误。这是我在 Controller 中使用的函数:

public function store()
{
$subscriber = new Subscriber;

$subscriber->first_name = Input::get('first_name');
$subscriber->last_name = Input::get('last_name');
$subscriber->email = Input::get('email');

if(!$subscriber->save())
{
return Redirect::to('/admin/subscriber/create')->with('errors', $subscriber->errors()->all());
}

return Redirect::to('/admin/subscriber')->with('status', 1);
}

我在模型中的热心规则:

public static $rules = array(
'email' => 'required|email|unique:users',
'first_name' => 'required',
'last_name' => 'required',
);

public static $customMessages = array(
'first_name.required' => 'First name is required.',
'last_name.required' => 'Last name is required.',
'email.required' => 'Email is required.',

'email.email' => 'Use a real email address!',
'email.unique' => 'This email address already exists!',
);

我在 Blade 模板中调用的内容:

@if ($errors->has())
@foreach ($errors->all() as $error)
<div class='bg-danger alert'>{{ $error }}</div>
@endforeach
@endif

每次我尝试在表单中输入不应验证的数据时,都会收到错误 Call to a member function has() on array ,它引用了 $errors->has( )

有人有什么想法吗?干杯

最佳答案

当您向 View 提供 $errors 时,您可以使用:

->with('errors', $subscriber->errors()->all())

这意味着您已经调用了将错误转换为数组的 all() 方法。您需要删除 all() 调用,即:

->with('errors', $subscriber->errors())

然后您可以像您一样在 View 中使用 has()all()

关于validation - 在 Blade 中调用 @if ($errors->has()) 时,调用数组上的成员函数 has(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27663175/

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