skip to Main Content

This is my code, double form so this is the same input name like eg. email. When i send form with empty email field i have an error how can i display one error to one correct form. Now of course i see this error on each form.

<form action="{{ route('signup') }}" method="post" class="col-md-9" id="webinar-form-{{ $data->id }}">
    @csrf
    <div class="input-group mb-3">
        <input type="text" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" placeholder="Adres email">
        <button type="submit" class="btn btn-primary">Sign Up</button>
    </div>

    @error('email')
        <div class="alert alert-light" role="alert">
            <span class="text-danger">{{ $message }}</span>
        </div>
    @enderror
</form>

<form action="{{ route('signup') }}" method="post" class="col-md-9" id="webinar-form-{{ $data->id }}">
    @csrf
    <div class="input-group mb-3">
        <input type="text" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" placeholder="Adres email">
        <button type="submit" class="btn btn-primary">Sign Up</button>
    </div>

    @error('email')
        <div class="alert alert-light" role="alert">
            <span class="text-danger">{{ $message }}</span>
        </div>
    @enderror
</form>

2

Answers


  1. You need to change the name of your email, probably something like this

    <form action="{{ route('signup') }}" method="post" class="col-md-9" id="webinar-form-{{ $data->id }}">
        @csrf
        <div class="input-group mb-3">
            <input type="text" class="form-control @error('email_1') is-invalid @enderror" name="email_1" value="{{ old('email_1') }}" placeholder="Adres email">
            <button type="submit" class="btn btn-primary">Sign Up</button>
        </div>
    
        @error('email_1')
            <div class="alert alert-light" role="alert">
                <span class="text-danger">{{ $message }}</span>
            </div>
        @enderror
    </form>
    
    <form action="{{ route('signup') }}" method="post" class="col-md-9" id="webinar-form-{{ $data->id }}">
        @csrf
        <div class="input-group mb-3">
            <input type="text" class="form-control @error('email_2') is-invalid @enderror" name="email" value="{{ old('email_2') }}" placeholder="Adres email">
            <button type="submit" class="btn btn-primary">Sign Up</button>
        </div>
    
        @error('email_2')
            <div class="alert alert-light" role="alert">
                <span class="text-danger">{{ $message }}</span>
            </div>
        @enderror
    </form>
    

    Because your input name is same for both form. And you can set the variable in your controller to match the name

    Login or Signup to reply.
  2. You need to use array validation in laravel

    $request->validate([
            "email"    => "required|array",
            "email.*"  => "required|email|unique:table_name",
        ]);
    

    Use this code in controller and check it’s working or not.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search