How to validate a form for the Next button using Datatables

How to validate a form for the Next button using Datatables

miss89miss89 Posts: 3Questions: 2Answers: 0

I'm using datatables to show results from a survey. I show each question and a set of answers to choose from in a separate page. I have to do validation - if you have not chosen any of the possible answers, to stay at that page and show validation massage for required field. If you answer that question, then to be shown the next question. That's why I need to validate form for the "Next" button. Not to show next question, if it's not chosed answer. Now I saw id of the "next" button in my table is: "example_next". Have you ideas how to do validation? That's my code:

<html>
<head>
<link rel="stylesheet" href="//cdn.datatables.net/1.10.5/css/jquery.dataTables.min.css" type="text/css"  />
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="//cdn.datatables.net/1.10.6/js/jquery.dataTables.min.js"></script>
<script src="//cdn.datatables.net/tabletools/2.2.4/js/dataTables.tableTools.min.js"></script>
<script src="../../media/js/dataTables.editor.min.js"></script>
<script>

    $(document).ready(function() {
    $('#example').dataTable( {

        "pagingType": "simple",
        "lengthMenu": [[1], [1]],
        "bSort": false,

         "language": {
    "paginate": {
      "previous": true

    }
  }


    } );

} );
   $("#button").click(function () {

Field::inst( 'answer' )->validator( 'Validate::notEmpty' );

     }
  <?php
include( "/editor/php/DataTables.php" ); 

use
    DataTables\Editor,
    DataTables\Editor\Field,
    DataTables\Editor\Format,
    DataTables\Editor\Join,
    DataTables\Editor\Upload,
    DataTables\Editor\Validate;
 

Editor::inst( $db, 'survey_answers' )
    ->fields(
        Field::inst( 'answer' )->validator( 'Validate::required' )
)
    ->process( $_POST )
    ->json();

  $survey_id = $this->uri->segment(3);
$question_id = $this->uri->segment(4);
  $att=array("id"=>"form"); 
echo form_open('index/survey_fill/' .$survey_id , $att); ?>
<table id='example' >
  <thead>
    <tr><th>question_id</th></tr>
  </thead>   
  <tbody>
  <?php
    echo validation_errors();

    foreach ($survey as $row)
    {
            
    ?>
        <tr>
        <td> 
        <?php echo "$row->question"; ?><br/>
        <?php echo "<input type='hidden' name='question_id' value='$row->question_id' />"; ?>
        <?php 
         
        $data=array(
            'name' =>  'answer['.$row->question_id.']',
            'value' => '5',
            'onclick'=>"last_question()",
            "data-val" => true,
            "data-val-required" =>"Required field"
        );

        echo "<input type='hidden' name='survey_id' value='$row->survey_id'>"; 
        
        echo form_radio($data); 
        echo " 5 ";
        $data=array(
            'name' =>  'answer['.$row->question_id.']',
            'value' => '4',
            'onclick'=>"last_question()"
        );
        echo form_radio($data);
        echo " 4 ";
      }
  ?>
    </tbody>
</table>
  <?php  echo '<input type="submit" id="button" onsubmit="return validate(this);" name = "submit" value="Submit" class="btn btn-success">';


This discussion has been closed.