Keeping modal dialog open after validation error laravel
Asked Answered
S

5

8

So basically I have a blade.php, controller page and a form request page(validation). I'm trying to keep my modal dialog open if there is an error but I just cant figure it out, what part of code am I missing out on or needs to be changed?

blade.php

<div id="register" class="modal fade" role="dialog">
...

<script type="text/javascript">
if ({{ Input::old('autoOpenModal', 'false') }}) {
    //JavaScript code that open up your modal.
    $('#register').modal('show');
}
</script>

Controller.php

class ManageAccountsController extends Controller
{
    public $userRepository;

    public function __construct(UserRepository $userRepository)
    {
        $this->userRepository = $userRepository;
    }

    public function index() 
    {
        $users = User::orderBy('name')->get();
        $roles = Role::all();

        return view('manage_accounts', compact('users', 'roles'));
    }

    public function register(StoreNewUserRequest $request)
    {
        // process the form here
        $this->userRepository->upsert($request);
        Session::flash('flash_message', 'User successfully added!');

        //$input = Input::except('password', 'password_confirm');
        //$input['autoOpenModal'] = 'true'; //Add the auto open indicator flag as an input.

        return redirect()->back();
    }
}

class UserRepository {

    public function upsert($data)
    {

            // Now we can separate this upsert function here
        $user = new User;
        $user->name     = $data['name'];
        $user->email    = $data['email'];
        $user->password = Hash::make($data['password']);
        $user->mobile   = $data['mobile'];
        $user->role_id  = $data['role_id'];

            // save our user
        $user->save();

        return $user;
    }
}

request.php

class StoreNewUserRequest extends Request
{
    /**
     * 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()
    {
        // create the validation rules ------------------------

        return [
        'name'             => 'required',                        // just a normal required validation
        'email'            => 'required|email|unique:users',     // required and must be unique in the user table
        'password'         => 'required|min:8|alpha_num',
        'password_confirm' => 'required|same:password',           // required and has to match the password field
        'mobile'           => 'required', 
        'role_id'          => 'required'
        ];
    }
}
Stakhanovism answered 1/11, 2015 at 4:11 Comment(0)
H
30

Laravel automatically checks for errors in the session data and so, an $errors variable is actually always available on all your views. If you want to display a modal when there are any errors present, you can try something like this:

<script type="text/javascript">
@if (count($errors) > 0)
    $('#register').modal('show');
@endif
</script>
Hepler answered 1/11, 2015 at 5:4 Comment(5)
Thanks so much, and for the additional knowledge on $errors part. =)Stakhanovism
I have used the code now my error messages is ok. But the modal is despairing after click the login button and again when I click the modal it's showing that error message. How can I solve it?Molokai
@KaziRabbiHassan did you manage to solve the problem ??Haeres
yes, I have managed to solve the problem.Molokai
:) Thanks bro very usefulCalyptra
S
6

Put If condition outside from script. This above is not working in my case

  @if (count($errors) > 0)
    <script type="text/javascript">
        $( document ).ready(function() {
             $('#exampleModal2').modal('show');
        });
    </script>
  @endif
Stratum answered 26/5, 2020 at 9:52 Comment(0)
M
1

for possibly multiple modal windows you can expand Thomas Kim's code like following:

<script type="text/javascript">

   @if ($errors->has('email_dispatcher')||$errors->has('name_dispatcher')|| ... )
      $('#register_dispatcher').modal('show');
   @endif

   @if ($errors->has('email_driver')||$errors->has('name_driver')|| ... )
      $('#register_driver').modal('show');
   @endif
  ...
</script>

where email_dispatcher, name_dispatcher, email_driver, name_driver are your request names being validated

Mekka answered 22/9, 2021 at 17:25 Comment(0)
B
0

just replace the name of your modal with "login-modal". To avoid error put it after the jquery file you linked or jquery initialized.

<?php if(count($login_errors)>0) : ?>
    <script>
        $( document ).ready(function() {
            $('#login-modal').modal('show');
        });
    </script>
  <?php endif ?>
Bronchiectasis answered 11/10, 2020 at 17:43 Comment(0)
O
0
<script>

    function openLoginModal() {
        document.getElementById('loginModal').style.display = 'block';
    }
    
</script>
Oxytocin answered 9/4 at 10:48 Comment(1)
Hello, welcome! Please avoid answering questions only with code. Consider adding explanation and context to make it more useful.Hellhole

© 2022 - 2024 — McMap. All rights reserved.