skip to Main Content

I have a table with description column. Description has a datatype text (but I tried to change to longText type into phpmyadmin). I have a validation rule too:

   public function rules()
    {
        return [
            'title' => 'required|string|between:6,50',
            'city' => 'required|integer|exists:cities,id',
            'phone' => 'required_without:mail|digits:9',
            'mail' => 'nullable|required_without:phone|email',
            'description' => 'required|string|between:30,600',
            'file' => 'nullable|mimes:jpeg,jpg,png|max:3000'
        ];
    }

And it works but the problem is that if I write a very long text, I don’t receive any error instead just refresh only. I had the same with other controllers method. How I can I solve my problem?

2

Answers


  1. When you already made a rule, and validate it in Controller, you need to make condition if the validator fails, you need to return the error to the view. You can follow code below

    $validator = Validator::make($request, $rule);
    

    After that check the validator with

    if ($validator->fails()) {
        return redirect()->back()
            ->withErrors($validator)
            ->withInput();
    }
    

    In your view, just show the error with

    @if ($errors->any())
        <div class="alert alert-danger">
            <ul>
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif
    
    Login or Signup to reply.
  2. The page refreshes because of the form’s submission.

    As for the validation check,
    There are 3 main types of validation –
    1. Browser validation
    2. Front-end validation
    3. Server-side validation

    The errors that you have seen in other fields of the form are probably because of the Browser validation. It’s built into the browser.
    However, with your current implementation, it is Server-side validation.
    It will work on the background, but will not show in the front end.

    For that, please add the following code inside the body of your html page.

    <div class="alert alert-danger">
      <ul>
        @foreach ($errors->all() as $error)
            <li>{{ $error }}</li>
        @endforeach
      </ul>
    </div>
    

    Also please add the following function to your validatior’s Request class.

    public function messages()
    {
        return [
            'description.between' => "Description should be between 30 and 600 characters",
        ];
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search