0
votes

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

1 Answers

0
votes

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
    ];
}