noblemfd
1 month ago
887
46
Laravel

checkbox stores 0 whatever value the user enters

Posted 1 month ago by noblemfd

I am using data-bootstrap-switch checkbox in dynamic input field

Controller:

public function store(StoreLeaveTypeRequest $request)
{ 
    $leavetype = new HrLeaveType();
    $leavetype->leave_type_name             = $request->leave_type_name;
    $leavetype->leave_type_code             = $request->leave_type_code;

   $leavetype->save();        

    foreach ($request->employee_type_id as $key => $employee_type_id){

   $insert_array = [
        'employee_type_id'                  => $request->employee_type_id[$key],    
        'weekend_inclusive'                 => $request->weekend_inclusive[$key] ?? 0,
        'holiday_inclusive'                 => $request->holiday_inclusive[$key] ?? 0,
        'leave_type_id'                     => $leavetype->id,
     ];

      HrLeaveTypeDetail::create($insert_array );

       } 
    }

view

   <form  action="{{route('leave.leave_types.store')}}" method="post" class="form-horizontal" enctype="multipart/form-data">
       {{csrf_field()}}
       
       
       <div class="card-body">
        <div class="form-body">
        <div class="row">  
          <div class="col-12 col-sm-6">
            <div class="form-group">
              <label class="control-label"> Leave Type Name:<span style="color:red;">*</span></label>
              <input  type="text" name="leave_type_name" value="{{ old('leave_type_name', $leavetype->leave_type_name) }}" placeholder="Enter leave type name" class="form-control @error('leave_type_name') is-invalid @enderror">
                @error('leave_type_name')
                    <span class="invalid-feedback" role="alert">
                        <strong>{{ $message }}</strong>
                    </span>
                @enderror                 
            </div>
          </div>

          <div class="col-12 col-sm-6">
            <div class="form-group">
              <label class="control-label"> Leave Type Code:</label>
              <input  type="text" name="leave_type_code" value="{{ old('leave_type_code', $leavetype->leave_type_code) }}" placeholder="Enter leave typecode" class="form-control @error('leave_type_code') is-invalid @enderror">
                @error('leave_type_code')
                    <span class="invalid-feedback" role="alert">
                        <strong>{{ $message }}</strong>
                    </span>
                @enderror                 
            </div>
          </div>                
            
            
          <div class="col-sm-12">
            <div class="form-group">
                <label>Description</label>
                <textarea rows="2" name="description" class="form-control @error('description') is-invalid @enderror" value="{{old('description',$leavetype->description)}}" placeholder="Enter Description here ...">{{old('description',$leavetype->description)}}</textarea>
                @error('description')
                    <span class="invalid-feedback" role="alert">
                        <strong>{{ $message }}</strong>
                    </span>
                @enderror                 
            </div>
          </div>

   <div class="col-sm-12">
            <table class="table table-bordered">
                        <thead>
                        <tr>
                            <th scope="col">Employee Type<span style="color:red;">*</span></th>
                            <th scope="col">Weekend Inclusive</th>
                            <th scope="col">Holiday Inclusive</th>   
                            <th scope="col"><a class="btn btn-info addRow"><i class="fa fa-plus"></i></a></th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr>
                            <td width="40%">                    
                                <select class="form-control select2bs4" data-placeholder="Select Employee Type" tabindex="1" name="employee_type_id[]">
                                    <option value="0" selected="true" disabled="true">Select Employee Type</option>
                                     @if($employeetypes->count() > 0 )
                                        @foreach($employeetypes as $employeetype)
                                         <option name="employee_type_id[]"  value="{{$employeetype->id}}">{{$employeetype->employee_type_name}}</option>
                                        @endforeach
                                    @endif                                         
                                </select>
                            </td>                                                         
                            <td width="10%"><input type="checkbox" name="weekend_inclusive[]"  class="form-control weekend_inclusive" value="{{old('weekend_inclusive') ? 'checked' : '' }}" unchecked data-bootstrap-switch data-off-color="danger" data-on-color="success" data-off-text="NO" data-on-text="YES"></td>
                            <td width="10%"><input type="checkbox" name="holiday_inclusive[]"  class="form-control holiday_inclusive" value="{{old('holiday_inclusive') ? 'checked' : '' }}" unchecked data-bootstrap-switch data-off-color="danger" data-on-color="success" data-off-text="NO" data-on-text="YES"></td>                                
                            <td width="5%"><a class="btn btn-danger remove"> <i class="fa fa-times"></i></a></td>
                         </tr>
                        </tbody>
                    </table>
        </div>
       </div>
     </div>
    </div>          
    <!-- /.card-body -->
    <div class="card-footer">
      <button type="submit" class="btn btn-primary">{{ trans('global.save') }}</button>
    </div>

jQuery

<script type="text/javascript">
    $(document).ready(function(){
        $('.addRow').on('click', function () {
            addRow();

        });
        function addRow() {
            var addRow = '<tr>\n' +
'   <td width="40%"><select class="form-control select2bs4" data-placeholder="Choose Employee Type" tabindex="1" name="employee_type_id[]">\n' +
       '             <option value="0" selected="true" disabled="true">Select Employee Type</option>\n' +
       '              @if($employeetypes->count() > 0 )\n' +
       '                 @foreach($employeetypes as $employeetype)\n' +
       '                 <option value="{{$employeetype->id}}">{{$employeetype->employee_type_name}}</option>\n' +
        '                 @endforeach\n' +
        '             @endif\n' +
        '         </select></td>\n' +
        '        <td width="10%"><input type="checkbox" name="weekend_inclusive[]" class="form-control weekend_inclusive" value="{{old('weekend_inclusive') ? 'checked' : '' }}" unchecked data-bootstrap-switch data-off-color="danger" data-on-color="success" data-off-text="NO" data-on-text="YES"></td>\n' +
       '        <td width="10%"><input type="checkbox" name="holiday_inclusive[]" class="form-control holiday_inclusive" value="{{old('holiday_inclusive') ? 'checked' : '' }}" unchecked data-bootstrap-switch data-off-color="danger" data-on-color="success" data-off-text="NO" data-on-text="YES"></td>\n' +
'   <td width="5%"><a   class="btn btn-danger remove"> <i class="fa fa-times"></i></a></td>\n' +
'         </tr>';
            $('tbody').append(addRow);
            addRemoveListener();
        };
    addRemoveListener();
 });
</script>

I am using bootstrap switch checkbox. The checkbox can either be 0 or 1

0=No and 1=Yes

I am using dynamic input fields. When I submitted, What I observed is that whatever the user selects for:

    weekend_inclusive
    holiday_inclusive

the application stores 0 for both:

    weekend_inclusive
    holiday_inclusive

How do I resolve this?

Please sign in or create an account to participate in this conversation.