1

我正在尝试使用 Ardent 从刀片模板中的表单返回错误。这是我在控制器中使用的功能:

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!',
);

我在刀片模板中调用的是:

@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()

有人知道吗?干杯

4

1 回答 1

4

当你提供$errors给视图时,你使用这个:

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

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

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

然后你可以像你一样在视图中使用has()和。all()

于 2014-12-27T01:07:45.633 回答