php – Laravel 7 old email input problems-ThrowExceptions

Exception or error:

I make a registration system with laravel. I tried various ways from google, but none of them work.

I want when I enter the wrong email format so that I get the message that it is wrong and that in value = “” it is all recorded.

My AccountController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Redirect;


class AccountController extends Controller
{
    public function getcreate(){
        return view('account.create');
    }

    public function postcreate(){
        $validator = Validator::make(request()->all(), 
        array(
            'email'  =>      'required|max:50|email|unique:users',
            'username' =>    'required|max:20|min:3|unique:users',
            'password' =>    'required|min:6',
            'repeat_pass' => 'required|same:password'   
        ));

        if($validator->fails()){
            return redirect()->route('account-create')->withErrors($validator)->withInput();
        }
        else{

        }
    }
}

My create.blade.php

@extends ('layout.main')

@section ('content')

    <form action="{{ URL::route('account-create-post') }}" method="post">

            <div class="field">
                Email: <input type="text" name="email" placeholder="Email"{{ (old( 'email' )) ? ' value="'. e(old())  .'"' : ''    }}>
                @if($errors->has('email'))
                        {{ $errors->first('email') }}
                @endif
            </div>
            <div class="field">
                Username: <input type="text" name="username" placeholder="Username">
                @if($errors->has('uesrname'))
                        {{ $errors->first('username') }}
                @endif
            </div>
            <div class="field">
                Password: <input type="password" name="password" placeholder="Passowrd">
                @if($errors->has('password'))
                        {{ $errors->first('password') }}
                @endif
            </div>
            <div class="field">
                Repeat Password: <input type="password" name="repeat_pass" placeholder="Repeat Pssword">
                @if($errors->has('repeat_pass'))
                        {{ $errors->first('repeat_pass') }}
                @endif
            </div>


            <input type="submit" value="Create Account">
            @csrf
    </form>
@stop 

The ways from the internet I tried

value="{{ old('email') }}"
value="{{!! old('email') !!}}"
How to solve:

The old method will pull the previously flashed input data from the session, not to display error message.

To display an error message, you should use @error :

<div class="field">
    Email: <input type="text" name="email" placeholder="Email" value="{{ old('email') }}">

    @error('email')
        <strong>{{ $message }}</strong>
    @enderror
</div>

Answer´╝Ü

you can display error message on the blade using this:

                                      @if (count($errors) > 0)
                                        <div class="alert alert-danger alert-dismissible" role="alert">
                                            <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span>
                                            </button>
                                            <strong>Error!</strong> 
                                            @foreach ($errors->all() as $error)
                                            <p>{{ $error }}</p>
                                            @endforeach
                                        </div>
                                        @endif

Leave a Reply

Your email address will not be published. Required fields are marked *