vendredi 23 septembre 2016

Laravel html form not validating form input

I am trying to set up a contact form on a one page site using laravel, I can't seem to be able to get the form to validate the user input and show any errors that the form might have.

email.blade.php:

<ul>
    @foreach($errors->all() as $error)
        <li></li>
    @endforeach
</ul>
{!! Form::open(['route' => 'mail', 'method' => 'post', 'role' => 'form', 'id' => 'footer-form']) !!}
<div class="form-group has-feedback">
    {!! Form::label('first_name', null, ['class' => 'sr-only']) !!}
    {!! Form::text('first_name', null, ['class' => 'form-control', 'placeholder' => 'First Name']) !!}
    <i class="fa fa-user form-control-feedback"></i>
    @if($errors->has('first_name'))
        
    @endif
</div>
<div class="form-group has-feedback">
    {!! Form::label('last_name', null, ['class' => 'sr-only']) !!}
    {!! Form::text('last_name', null, ['class' => 'form-control', 'placeholder' => 'Last Name']) !!}
    <i class="fa fa-user form-control-feedback"></i>
    @if($errors->has('last_name'))
        
    @endif
</div>
<div class="form-group has-feedback">
    {!! Form::label('email', null, ['class' => 'sr-only']) !!}
    {!! Form::email('email', null, ['class' => 'form-control', 'placeholder' => 'Email address']) !!}
    <i class="fa fa-envelope form-control-feedback"></i>
    @if($errors->has('email'))
        
    @endif
</div>
<div class="form-group has-feedback">
    {!! Form::label('textarea', null, ['class' => 'sr-only']) !!}
    {!! Form::textarea('textarea', null, ['class' => 'form-control', 'rows' => 8, 'placeholder' => 'Message']) !!}
    <i class="fa fa-pencil form-control-feedback"></i>
    @if($errors->has('textarea'))
        
    @endif
</div>

{!! Form::submit('Send', ['class' => 'btn btn-default']) !!}

{!! Form::close() !!}

Route: web.php:

Route::get('/ensignhospital', [
    'as' => 'home',
    'uses' => 'HomeController@home'
]);


Route::group(['before' => 'guest'], function () {
    /*
     * CSRF Protection
     *
     * */
    Route::group(['before' => 'csrf'], function () {

        Route::post('/ensignhospital', [
            'as' => 'mail',
            'uses' => 'HomeController@postSendMail'
        ]);
    });


});

controller to handle for request:

class HomeController extends Controller {

    public function home(){
        return View('welcome');
    }



    public function postSendMail(ContactFormRequest $request){

        if($request->fails()){

            return Redirect::route('')
                ->withErrors()
                ->withInput();

        }else{

            return View('passed');
        }


    }

}

The form request validator class:

class ContactFormRequest 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 [
            //
            'first_name'    => 'required',
            'last_name'     => 'required',
            'email'         => 'required|email',
            'message'       => 'required'
        ];
    }
}

Problem the form does not validate when I don't enter a valid input. I need the form to validate user input and remain at the form position on the web page.

please note:

I used a .htaccess file to get the site which is on my computer to display the site root as localhost/mylaravelproject rather than the usual localhost/mylaravelprojec/public that one will normally see on a fresh install of laravel.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire