简体   繁体   English

如何在 java 脚本变量中传递 laravel 验证错误计数

[英]How to pass laravel validation error count in java script variable

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.我想将 Laravel 验证错误计数值放入 javascript 变量中,并使用甜蜜警报通知用户您错过了所需的数量。 how does the pass error count to js variable?传递错误如何计入js变量?

{{ $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.此错误计数需要传递 js 变量。提交后将生成 laravel 错误。我如何使用 JS 或 Jquery 获取此值。我尝试按照对我不起作用的方式进行操作。

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

});

Note: $errors->count() works/activate after you submit the form request .注意: $errors->count()在您提交form请求后工作/激活 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如果您希望在提交前显示错误计数,您可能需要AJAX或一些前端验证 javascript 库

the tag can't compile blade templating syntax.标签无法编译刀片模板语法。 Instead of using blade而不是使用刀片

{{ $errors->count() }} 

you can try with php tags with echo and htmlspecialchars您可以尝试使用带有 echo 和 htmlspecialchars 的 php 标签

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

Try:尝试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM