Add Validation in Bootstrap modal

Viewed 66

I want to add validation in form modal, but the modal keep coming out. The validation is working if im open the modal again.

My Controller:

    public function save()
    {
        if(!$this->validate([
            'pertanyaan'=>[
                'rules' => 'required',
                'error' => 'belum diisi'
            ]
        ])){
            $validation = \Config\Services::validation();
            return redirect()->to(base_url('SAdmin/pertSurvei'))->withInput()->with('validation',$validation);
        }
        
        $data = array(
            'pertanyaan'     => $this->request->getVar('pertanyaan'),
            'id_pengguna'   => $this->request->getVar('id_pengguna')
        );

        $this->pertModel->savePertanyaan($data);
        return redirect()->to(base_url('SAdmin/pertSurvei'));
    }

My Views

<form action="<?= base_url('SAdmin/PertSurvei/save'); ?>" method="post">
        <div class="form-group row">
          <label class="col-sm-4 col-form-label">Pertanyaan</label>
          <div class="col-sm-8">
            <input type="text" class="form-control <?= ($validation->hasError('pertanyaan')) ? 'is-invalid' : ''; ?>" name="pertanyaan" placeholder="Masukkan Pertanyaan">
            <div class="invalid-feedback">
              Please provide a valid state.
            </div>
          </div>
        </div>

        <div class="form-group row">
          <label class="col-sm-4 col-form-label">Unit</label>
          <div class="col-sm-8">
            <select name="id_pengguna" class="form-control">
                <option value="">-Pilih-</option>
                <?php foreach($pengguna as $key => $value):?>
                <option value="<?= $value['id'];?>"><?= $value['nama_unit'];?></option>
                <?php endforeach;?>
            </select>
          </div>
        </div>
      
      </div>
      <div class="modal-footer">
          <button type="submit" class="btn1 btn-success">Tambah Pertanyaan</button>
      </div>
      </div>
  </div>
  </div>
</form>

For the session i put in basecontroller Anyone know how to fix it? thanks before

2 Answers

You can send the form by Ajax instead. An alternative would be to show up the modal automatically if there has been any error in the form. Another simpler option could be to put HTML validations in the form fields, like 'required'.

//It won't work this way. You have to send it through ajax.
// Give your form an ID. like id="myForm"

// "serialize" will take your form's name attributes.

$(document).ready(function(){
$('#submit').click(function(){
    $.ajax({
            url: 'your_action_url',
            method: 'get',
            data: $('#myForm').serialize(),  
            dataType: 'json',
            success: function(data){
                // here you will bring your data as a json encoded format and break them and set them in required places. or just play with your data.
            },
            error: function(){
                alert('Could not displaying data');
            }           
        });
   });
});
Related