Home > front end >  How to pass laravel validation error count in java script variable
How to pass laravel validation error count in java script variable

Time:10-08

I want to get Laravel validation error count value into the javascript variable and notify to the user to you have missout the required number to the user using sweet alert. how does the pass error count to js variable?

{{ $errors->count() }}

this error count needs to pass the js variable.laravel errors will generate after the submission .how do i get this value using JS or Jquery.i try following way it not work to me.

$("#btn-submit").click(function(event) {
    var error_count = "<?php echo $errors->count(); ?>";
    alert(error_count);

});

CodePudding user response:

the tag can't compile blade templating syntax. Instead of using blade

{{ $errors->count() }} 

you can try with php tags with echo and htmlspecialchars

<?php echo htmlspecialchars($errors->count()) ?>

Try:

var error_count =  <?php echo htmlspecialchars($errors->count()) ?>;

CodePudding user response:

Note $errors->count() works/activate after you submit the form request. If you need to get the count afterwards, you can do it like this

@if($errors->count())
    <script>
        alert( {{$errors->count()}} );
    </script>
@endif

If you wish to show the error count before submitting, you might have to AJAX or some frontend validation javascript libraries

  • Related