2
votes

Using the latest Laravel 5.7.8 and what worked in Laravel 5.6.3 might not be applicable in my case.

I'm trying to create a Contact form validation (required ALERT/SUCCESS messages)

contactUS.blade.php

@extends('layout.app')

@section('content')


<div id="page">
	<div id="publish">
    <div class="frameterms">
<h1>Contact</h1>
<script src='https://www.google.com/recaptcha/api.js'></script>
 
@if(Session::has('success'))
  <div class="notify yellow" style="display:block;">
     {{ Session::get('success') }}
   </div>
@endif
 

{!! Form::open(['route'=>'contactus.store']) !!}
<div class="row">
<label>Name<span class="asterix">*</span></label>
</div>
<div class="row {{ $errors->has('name') ? 'has-error' : '' }}">

{!! Form::text('name', old('name'), ['class'=>'row', 'placeholder'=>'Enter Name']) !!}
<span class="text-danger">{{ $errors->first('name') }}</span>
</div>
 
<div class="row {{ $errors->has('email') ? 'has-error' : '' }}">
  <label>Email<span class="asterix">*</span></label>
{!! Form::text('email', old('email'), ['class'=>'row', 'placeholder'=>'Enter Email']) !!}
<span class="text-danger">{{ $errors->first('email') }}</span>
</div>
 

<div class="row {{ $errors->has('message') ? 'has-error' : '' }}">
  <label>Subject<span class="asterix">*</span></label>
   <div class="selector">
   <div class="selector1">
   
  {!! Form::select('subject', array(
    'Select subject...' => 'Select subject...',
    'Web' => 'Web development',
    'Marketing' => 'Marketing',
    'App' => 'App development',
    'Advertising and Promotion' => 'Advertising and Promotion',
    'Other' => 'Other') ) !!}</div>
    <div>
  <span class="text-danger">{{ $errors->first('message') }}</span>
  </div>


<div class="row {{ $errors->has('message') ? 'has-error' : '' }}">
  <label>Message<span class="asterix">*</span></label>

{!! Form::textarea('message', old('message'), ['class'=>'row', 'placeholder'=>'Enter Message']) !!}
<span class="text-danger">{{ $errors->first('message') }}</span>
</div>


<div class="row">
  <small class="required"><span class="asterix">*</span> Required fields</small>
</div>

<div class="form-group {{ $errors->has('g-recaptcha-response') ? ' has-error' : '' }}">
  
   
  <div class="col-md-6 pull-center">
  {!! app('captcha')->display() !!}
  
  @if ($errors->has('g-recaptcha-response'))
  <span class="help-block">
  <strong>{{ $errors->first('g-recaptcha-response') }}</strong>
  </span>
  @endif
  </div>
</div>

 
<div class="rowbutton">
		<input onclick="$('.loader').show(); $(this).hide();" type="submit" value="Send message">
		<div class="loader" style="display:none;"></div>
</div>
 
{!! Form::close() !!}
 
</div>

</div>

</div>
</div>


@endsection

ContactUSController.php

<?php
 
namespace App\Http\Controllers;
 
use Illuminate\Http\Request;
use App\Http\Requests;
use App\ContactUS;
use Illuminate\Support\Facades\Input;
use Validator;
use Redirect;

 
class ContactUSController extends Controller
{
   /**
    * Show the application dashboard.
    *
    * @return \Illuminate\Http\Response
    */
   public function contactUS()
   {
       return view('pages.contactUS');
   }
 
   /**
    * Show the application dashboard.
    *
    * @return \Illuminate\Http\Response
    */
   public function contactUSPost(Request $request)
   {
       $this->validate($request, [
        'name' => 'required',
        'email' => 'required|email',
        'subject' => 'required',
        'message' => 'required',
        'g-recaptcha-response' => 'required|captcha'
        ]);
 
       ContactUS::create($request->all());
 
       return back()->with('success', 'Thanks for contacting us!');
       return redirect()->back();
   }
}

web.php

Route::get('contact', 'ContactUSController@contactUS');
Route::post('contact', ['as'=>'contactus.store','uses'=>'ContactUSController@contactUSPost']);

When I fill out the contact form, it adds a post to the SQL database on the backend. And when it fails, it goes back to the contact form view.

The problem is that the validator errors and SUCCESS message don't show up in the view even though I have coded in.

Does anyone know how to solve this problem? Is there any workaround? Any suggestion will be appreciated. Thank you in advance.

This is the picture from Laravel 5.6.3 project and required fields are working:

enter image description here

This is a picture from Laravel 5.7.8 project and required fields are NOT working:

enter image description here

5
can you try my answer? - Iftikhar uddin
As before, the message is created and sent but, the success cloud does not appear. - Roga Men
Did u tried my exact code? Bcz it's working fine on my side - Iftikhar uddin
Yes exact code! This code work on laravel 5.6. x but not on newer version 5.7.8 Maybe there is a problem in css? : S - Roga Men
Why the <div class="row"> do not change to <div class="row has-error"> ? - Roga Men

5 Answers

0
votes

Update your response as below.

return redirect()->back()->with('success', 'Thanks for contacting us!');

Remove last return as well.

0
votes

According to Laravel 5.7 Documentation

  return redirect('contact')->with('success', 'Thanks for contacting us!');

After the user is redirected, you may display the flashed message from the session. For example, using Blade syntax:

@if (session('success'))
    <div class="alert alert-success">
        {{ session('success') }}
    </div>
@endif
0
votes

Try specifying the view route where you want to show the message like:

return redirect('contact')->with('success', 'Thanks for contacting us!');

And then in your blade view get it like:

@if (session('success'))
    <div class="alert alert-success">
        {{ session('success') }}
    </div>
@endif

Ref: Docs

UPDATE:

From your this comment Why the <div class="row"> do not change to <div class="row has-error"> ? it seems you are talking about $errors so what you need to do is check whether error messages exists or not and after that display it like below:

If you want to display error under each field you can do like this.

@if ($errors->has('firstname'))
    <div class="error">{{ $errors->first('firstname') }}</div>
@endif

In new version of laravel it is changed to something like if ($errors->any()) However you can refer to docs here.

0
votes

After ContactUS::create($request->all()) line you can use
This is my code

    Session::flash('successMsg','what ever you want to message');

    @if( Session::has('successMsg'))
      <p class="btn btn-block btn-success disabled successMsg m-b-30">{{ 
         Session::get('successMsg') }}</p>
    @endif 

make validation like this. USe validator in top. use Validator;

make a validation array which $request you want to verify. Then make verify like this. In else section you write your code . and in if section give a valid message.

$v = [
        'image' => 'image|mimes:png,ico|max:200',
    ];
    $validator  = Validator::make($request->all(),$v);
    if($validator->fails()){
        Session::flash('errorMsg',$validator->errors());
        return redirect()->back();
    }else{

     }
0
votes

Ok, if you want to get an error for required fields you must put it like this:

{!! Form::text('name', old('name'), ['class'=>'row', 'placeholder'=>'Enter Name','required']) !!}

The key is 'required' and not like required without ''. I still didn't figure it out how to, do success message properly.