In Laravel how to show error message beside specific field?

I am new to laravel. I created a simple page where a user can add a product to the database. If the product title or amount is empty; I can also show the errors. But I want to show error right beside the specific field. If the title is empty the error will show beside the title field.
I searched and found solutions using JS and some other. But is there a way to achieve this using only laravel?

my view is like this

<form method="POST" action="create">
    @csrf
    <input type="text" name="title" placeholder="Product name"><br>
    <textarea name="description" placeholder="Description"></textarea><br>
    <input type="string" name="amount" placeholder="Price per unit"><br>
    <button type="submit">Add Product</button>
</form>
@if(count($errors))
    <ul>
        @foreach($errors->all() as $error)
            <li>{{ $error }}</li>
            @endforeach
    </ul>
@endif

and my controller is like this
public function store()
{   

    $this->validate(request(),[
        'title'=> 'required',
        'amount' => 'required',
    ]);

    $product = new Product;
    $product->title = request('title');
    $product->seller_id =  Auth::guard('seller')->user()->id;
    $product->description = request('description');
    $product->amount = request('amount');

    $product->save();
    return redirect('/dashboard');
}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You need to check for errors and display wherever you want like given below

@if ($errors->has('title')) <p style="color:red;">{{ $errors->first('title') }}</p> @endif

and
@if ($errors->has('amount')) <p style="color:red;">{{ $errors->first('amount') }}</p> @endif

In your code for view it can be placed as
    <form method="POST" action="create">
        @csrf
        <input type="text" name="title" placeholder="Product name">
         @if ($errors->has('title')) <p style="color:red;">{{ $errors->first('title') }}</p> @endif <br>
        <textarea name="description" placeholder="Description"></textarea><br>
        <input type="string" name="amount" placeholder="Price per unit">
        @if ($errors->has('amount')) <p style="color:red;">{{ $errors->first('amount') }}</p> @endif <br>
        <button type="submit">Add Product</button>
    </form>

Additionally you can also print custom messages by returning the error text from controller as shown below
$customMessages = [
  'title.required'  => 'The title field is required to be filled',
  'amount.required' => 'The amount field should be completely filled'
];
$this->validate(request,[
        'title'=> 'required',
        'amount' => 'required',
    ], $customMessages);

Instead, if you want to show all the errors for a field you can print them as follows
@if ($errors->has('title')) 
   <p style="color:red;">
    @foreach ($errors->get('title') as $errormessage) 
      {{ $errormessage }}<br>
    @endforeach
   </p> 
@endif

Method 2

All errors you can get in $errors array

just get them by input field name

Example:

<div class="form-group {{ $errors->has('title') ? 'has-error' : ''}}">
    <label for="titile" class="col-sm-3 control-label">Title: </label>
    <div class="col-sm-6">
        <input class="form-control" placeholder="Product name" required="required" name="title" type="text" id="title">
        {!! $errors->first('title', '<p class="help-block">:message</p>') !!}
    </div>
</div>

Here, $errors->first('title') mean its getting the first index of title errors in $errors array.

in your view you can do this as:

<form method="POST" action="create">
    @csrf
    <input type="text" name="title" placeholder="Product name"><br>
    {!! $errors->first('title', '<p class="help-block">:message</p>') !!} 
    <br> 
    <textarea name="description" placeholder="Description"></textarea><br>
      {!! $errors->first('description', '<p class="help-block">:message</p>') !!} 
    <br> 
    <input type="string" name="amount" placeholder="Price per unit"><br>
     {!! $errors->first('amount', '<p class="help-block">:message</p>') !!} 
    <br> 
    <button type="submit">Add Product</button>
</form>


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x