dongliang1893 2018-11-22 13:04
浏览 21

$ lafs在laravel 5.4中返回null

So i'm working with laravel 5.4 and i'm stuck in this error and can't figure it out.I researched about this error and i've seen that this happened before to, but the solutions are not working on my project.

I created a form to add comments in my page and it works if i type something it saves it in database and validation is working to because its not letting me add empty comment but its not showing the errors in page. This is the comment form in views

<form method="post" action="{{ route('comments.store') }}">
    {{ csrf_field() }}

    <input type="hidden" name="commentable_type" value="App\Company">
    <input type="hidden" name="commentable_id" value="{{ $company->id }}">

    <h2>Add a comment</h2>
    <div class="form-group @if($errors->has('url')) has-error @endif">
        <label for="comment-content">Work done (url/title)</label>
        <textarea placeholder="Enter url/title"
                  style="resize: vertical;"
                  id="comment-content"
                  name="url"
                  rows="2" 
                  spellcheck="false"
                  class="form-control autosize-target text-left">
        </textarea>
    </div>

    <div class="form-group @if($errors->has('body')) has-error @endif">
        <label for="comment-content">Comment</label>
        <textarea placeholder="Enter comment"
                  style="resize: vertical;"
                  id="comment-content"
                  name="body"
                  rows="3"
                  spellcheck="false"
                  class="form-control autosize-target text-left">
        </textarea>
    </div>

    <div class="form-group">
        <input type="submit" class="btn btn-primary" value="Submit"/>
    </div>
</form>

This is the CommentsControlles.php

public function store(CommentSubmitFormRequest $request)
{
    $comment = Comment::create([
        'body' => $request->input('body'),
        'url' => $request->input('url'),
        'commentable_type' => $request->input('commentable_type'),
        'commentable_id' => $request->input('commentable_id'),
        'user_id' => Auth::user()->id
    ]);

    if ($comment)
    {
        return back()->with('success', 'Comment added successfully');
    }
}

And this is the Request CommentSubmitFormRequest.php

class CommentSubmitFormRequest extends FormRequest
{
    public function authorize()
    {
        return true;
    }
    public function rules()
    {
        return [
            'body' => 'required',
            'url' => 'required',
        ];
    }
}

When i submit the empty comment form the $errors is returning null and not the errors

  • 写回答

1条回答 默认 最新

  • dpblwmh5218 2018-11-22 13:24
    关注

    Your validation rules are incomplete. It only said to be required and in your case your body and url are sent because the fields do exists. You should set a minimum amount of characters or do active_url/url for the url field.

    public function rules()
    {
        return [
            'body' => 'required|min:1', // minimum length of 1 character
            'url' => 'required|url', // must be a valid URL
        ];
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?