php - How to validate at least one checkbox is checked in a loop in Laravel form?
问题描述
I have a collection of checkboxes in my application. What I want to do is validate whether at least one is checked and enable a submit button. Basically a front end validation. According to the current code, the user must select all the checkboxes. I know this should be a small issue but as I am new to Laravel framework it's a little bit confusing for me to understand. Please, someone, help me here.
@foreach ($roles as $role)
<li>
{{ Form::checkbox('roles[]', $role->id, null, ['class="role-li"', 'required']) }}
{{ Form::label($role->name, ucfirst($role->name)) }}
</li>
@endforeach
解决方案
<form id="testform" action="/comment" method="post" class="m-0 mt-4">
@for($i=0;$i<5;$i++)
<input type="checkbox" name="vehicle1" value="Bike" class="role-li role{{$i}}" onclick='checktest()'> I have a bike<br>
@endfor
</form>
<script>
var minimumonechecked;
var noofboxes = $('.role-li').length;
function checktest(xyz) {
minimumonechecked = false;
for(i=0;i<noofboxes;i++){
if ($('.role' + i).is(':checked')) {
minimumonechecked = true;
}
}
console.log(minimumonechecked)
};
</script>
this code works for me :)
推荐阅读
- matlab - linux上的matlab:线图不起作用
- javascript - CodeIgniter - 将表单中动态添加的输入字段中的值存储到 MySQL 表中
- python-3.x - IndexError:索引 10 超出轴 1 的范围,大小为 10
- python - 按常用名称划分数据框行对
- selenium - Selenium IDE 不适用于画布
- r - Calculating distance between two points using the distm function inside mutate
- android - Not compile library file of Svg image Loader
- angularjs - for serverside pagination does the server should have the support for it? any "API" with pagination?
- maven - 在档案上部署时 Maven 401 未经授权
- java - 如何使用 JNA 获取活动窗口的应用程序名称?