Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

zahidnazirkhan's avatar

Form Request validation messages required per field, but not on top of the page.

Hello Everybody,

I am using Laravel Form Request Validation to validate the incoming request before actually hitting the controller.

But what I want is the validation messages to appear with the individual form fields, but not on top of the page.

Also I am using blade engine,how can I use error status to hit the has-error class per field instead of message. For Example,

$errors->has("company_name") ? "has-error" : ""

inside a blade form element.

Thankyou.

0 likes
6 replies
tykus's avatar

You can get the error for the specific field from the MessageBag:

<div class="form-group {{ $errors->has('company_name') ? 'has-error' : ''}}">
    <label for="company_name" class="col-sm-3 control-label">Name: </label>
    <div class="col-sm-6">
        <input class="form-control" required="required" name="company_name" type="text" id="company_name">
        {!! $errors->first('company_name', '<p class="text-small text-danger">:message</p>') !!}
    </div>
</div>
zahidnazirkhan's avatar

Let me give a brief about what I am intending to do:

  1. I am using Laravel 5.6

  2. I am using Form Validation Requests:

    namespace App\Http\Requests;

    use Illuminate\Foundation\Http\FormRequest;

    class GeneralSettingValidation extends FormRequest { /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return true; }

     /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
     public function rules()
     {
         return [
          'company_name' => 'required|max:10'
         ];
     }
    

    }

  3. Here is the concerned method from controller: public function store(GeneralSettingValidation $request) { //do something }

  4. In my blade view, I am getting an alert on top of the page containing all the validation error messages even though I am not referring to $errors session data in my view.

  5. Even if I refer to $errors session data, I get error messages per form field but also the alert on top of the page.

  6. I do not want the alert having validation errors on top of the page (No need for that).

  7. I want validation error messages per form field only.

  8. Inside my blade view elements, i want to utilize the $errors->first('company_name') ? "has-error" : "", for example inside this element

    {{ Form::text('company_name',null,array('class' => 'form-control','placeholder' => 'Company Name')) }}

I am trying to implement, but getting no success.

Kindly guide me so that I can move forward.

Thankyou.

tykus's avatar

I want validation error messages per form field only.

That is what is showed you. You can use the name of the field to get the validation error message using $errors->first('company_name') where $errors is a variable that is available to every view.

zahidnazirkhan's avatar

Indeed, thats what I want and they are coming.

But my concern is point No. 6, an alert consisting of all error messages comes at the top of page. I do not want that.

Only error messages for form fields not the alert on top of the page.

Kindly check point No. 6 in my above reply.

zahidnazirkhan's avatar

I fixed the issue of getting rid of alert pop-up showing errors. Some Configuration issues that skipped my mind.

Thankyou.

Please or to participate in this conversation.