title: How to do data validation in Codeigniter 3
body: <p>I made a crud using codeigniter 3, where there are two tables that are connected to each other using a primary key and a foreign key, how do I validate so that the primary key cannot be deleted if there is still a foreign key connected by displaying alerts, and the primary key can be deleted if there is no data connected.</p>
  <p><a href="https://i.stack.imgur.com/Uq73G.png" rel="nofollow noreferrer">Alert display</a></p>
  <p>Controllers</p>
  <pre><code>public function delete($id) {
  $data['kelas'] = $this-&gt;Kelas_model-&gt;delete_data($id);
  $this-&gt;session-&gt;set_flashdata('pesan', 
    '&lt;div class=&quot;alert alert-danger alert-dismissible fade show&quot; role=&quot;alert&quot;&gt;
      Data Deleted Successfully!
      &lt;button type=&quot;button&quot; class=&quot;btn-close&quot; data-bs-dismiss=&quot;alert&quot; aria-label=&quot;Close&quot;&gt;&lt;/button&gt;
    &lt;/div&gt;');
  redirect('kelas'); }
  </code></pre>
  <p>Models</p>
  <pre><code>public function delete_data($id_kelas) {
  $this-&gt;db-&gt;where('id_kelas', $id_kelas);
  return $this-&gt;db-&gt;delete('kelas'); }
  </code></pre>