当前位置: 首页 > 知识库问答 >
问题:

Laravel请求验证问题

万俟穆冉
2023-03-14

这是我的视图代码:

<!-- form.blade.php -->
<form class="form-horizontal" action="{{ isset($article) ? URL('console/articles/'.$article->id) : URL('console/articles') }}" method="POST">
  @if (isset($article))
    <input name="_method" type="hidden" value="PUT">
  @endif
  @unless (empty($errors->first()))
      <div class="alert alert-danger alert-dismissible" role="alert">
        <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        ...
      </div>
  @endunless
  <input type="hidden" name="_token" value="{{ csrf_token() }}">
  <div class="form-group {{ $errors->has('title') ? 'has-error' : '' }}">
    <label for="article_title" class="col-sm-2 control-label">...</label>
    <div class="col-sm-8">
      <input type="text" name="article[title]" id="article_title" class="form-control" placeholder="..." value="{{isset($article) ? $article->title : old('title')}}">
      {!! $errors->first('title', '<p class="help-block">:message</p>') !!}
    </div>
  </div>
  <div class="form-group {{ $errors->has('body') ? 'has-error' : '' }}">
    <label for="article_body" class="col-sm-2 control-label">...</label>
    <div class="col-sm-8">
      <textarea name="article[body]" id="article_body" cols="30" rows="10" class="form-control">{{isset($article) ? $article->body : old('body')}}</textarea>
      {!! $errors->first('body', '<p class="help-block">:message</p>') !!}
    </div>
  </div>
  <div class="form-group">
    <div class="col-sm-offset-2 col-sm-8">
      <button type="submit" class="btn btn-success">...</button>
    </div>
  </div>
</form>

和请求验证程序:

// StoreArticleRequest.php
// ...
public function rules()
{
  switch ($this->method()) {
    case 'GET':
    case 'DELETE': {
      return [];
    }
    case 'POST': {
      return [
        'article.title' => 'required|unique:articles|max:255',
        'article.body' => 'required'
      ];
    }
    case 'PUT':
    case 'PATCH': {
      return [
        'article.title' => 'required|unique:articles,title,'.Route::input('articles').'|max:255',
        'article.body' => 'required'
      ];
    }
    default:
      break;
  }
  return [];
}
// ...

发送post请求以创建方法后,它将

SQLSTATE[42S22]:找不到列: 1054未知列'article.title'in'where cluse'

SQLSTATE[42S22]:未找到列:“where子句”中的1054未知列“article.title”(SQL:选择count(*)作为来自articles的聚合,其中articletitle=test)

更新

这是我的控制器代码:

// ArticlesController.php
// ...
public function store(StoreArticleRequest $request)
{
  $article = new Article($request->input('article'));
  if ($article->save()) {
    return redirect('console/articles')->with('success', '...');
  } else {
    return redirect()->back()->withInput();
  }
}

// ...
public function update(StoreArticleRequest $request, $id)
{
  $article = Article::find($id);
  if ($article->update($request->input('article'))) {
    return redirect('console/articles')->with('success', '...');
  } else {
    return redirect()->back()->withInput();
  }
}

// ...

共有2个答案

皇甫卓君
2023-03-14

不使用article[title]作为输入名称,而使用title作为输入名称,在规则中使用title而不是article.title

董嘉祯
2023-03-14

在StoreArticleRequest.php文件中更改规则

case 'POST': {
  return [
    'article.title' => 'required|unique:articles,title|max:255',
    'article.body' => 'required'
  ];
}

如果未在唯一验证中指定列名称,则将“考虑”键作为列名。

 类似资料:
  • 我正在尝试在处理程序中设置自定义错误处理程序。但是当我试图在请求验证未满足时获取错误消息时,我得到了一个空响应。 我的规则设置为: 因此,当我不提供长度为10的参数时,我希望它在$e内输出该错误-

  • 我有一个设置为接收来自WooCommerce的网络钩子的Rails应用程序。具体来说,我正在寻找创建订单的时间。我已经测试并验证了它在我protect_from_forgery时是否有效,除了创建。现在我正在尝试通过验证网络钩子来保护我的应用程序。WooCommerce的留档声明在请求标头中传递以下秘密: 秘密:一个可选的密钥,用于生成请求正文的HMAC-SHA256哈希,以便接收者可以验证Web

  • 如何在Laravel中验证补丁/放置请求 根据Laravel留档http://laravel.com/docs/5.1/controllers,请求由资源控制器的操作处理 由于请求应更新部分资源并将更新整个资源,因此我的 验证应如下所示: 我应该这样做吗 依靠您的专业答案。

  • 所以我正在尝试为内部项目开发一个rest API,我遇到了一个问题,当表单请求验证失败时,它会显示@index响应。 所以我有两条路线; 列出所有客户端,创建一个新客户端,我在方法上获得了一个表单请求验证器,该验证器检查为客户端提供的名称。 我想要的是,当验证器失败时,它会显示带有验证错误的JSON响应。但我认为,验证失败了,所以它重定向回同一个页面,但重定向是GET,而不是POST,因此它列出了

  • 我需要在爪哇中使用摇摆不定(YAML文件)来验证传入的REST请求。所以任何人都可以帮我这个。提前致谢。

  • 我们的例子中要求用户进行身份验证并且在我们应用程序的每个URL这样做。我们可以通过给http.authorizeRequests()添加多个子节点来指定多个定制需求到我们的URL。例如: protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() //1