Manasa
Manasa

Reputation: 187

cakephp 2.9.7 data validation is not working

The problem is very simple but why it is complicating for me.I have written data validation also.But for empty fields also it is accepting the input.please check whether any error in my action.php.

Model/action.php

<?php
App::uses('AppModel', 'Model');

class Actions extends AppModel {
  public $validate = array(
        'value_to_plot' => array(
          'required'=>true,
            'message' => 'atleast select one measure'
        ),
        'column_name' => array(
          'required'=>true,
            'rule'=>array('notBlank'),
            'message' => 'atleast select one table'
          )

      );
}

?>

View/Actions/index.ctp

<div align="center">
<fieldset>
  <?php  echo $this->Form->create('valueToSend',array('type' => 'get'));?>
    <?php if(isset($_GET['table_name'])){ ?>
      <table class="table table-bordered table-hover table-striped">
        <?php echo $this->Form->hidden('table_name', array('hiddenField' => true, 'value'=> $_GET['table_name'],'id'=>'table_name'));
       echo $this->Form->hidden('chart', array('hiddenField' => true, 'value'=> "column3d",'id'=>"chart"));
        ?>
          <tr>
            <th>MEASURES</th>
            <td>
              <?php  echo $this->Form->select('value_to_plot',$measures1,array('class'=>'form-control','id'=>'measures','required'=>true),['empty' => 'choose one']);?>
            </td>

          </tr>
          <tr>
            <th>DIMENSIONS</th>
            <td>
              <?php echo  $this->Form->select('column_name[]',$measures2,array('multiple'=>'true','class'=>'form-control','id'=>'dimensions','required'=>true),['empty' =>'choose one']);?>
            </td>

          </tr>
        </table>
      <div style="text-align:center">
        <?php  echo $this->Form->end('submit'); ?>
      </div>
    <?php } ?>
</fieldset>
</div>

Controller/ActionsController.php

<?php
App::uses('AppController', 'Controller');

class ActionsController extends AppController {

  public function beforeFilter() {
    if(!isset($_SESSION['Auth']) && empty($_SESSION['Auth'])) {
    $userId = $_SESSION['Auth[User[id]]'];
    return $this->redirect(array('controller' => 'Users', 'action' => 'login'));
  }
}

  public Function index(){
    App::import('Model', 'ConnectionManager');
    $con = new ConnectionManager;
    $cn = $con->getDataSource('default');

    $tablequery="SELECT TABLE_NAME as table_name
    FROM INFORMATION_SCHEMA.TABLES
    WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA='phygital_visualize' AND TABLE_NAME != 'report'";
    $rows = $cn->query($tablequery);
    //$rows = $result->fetchAll('assoc');
    $dataToTable = [];
     foreach ($rows as $row) {
         $dataToTable[$row['TABLES']['table_name']] = $row['TABLES']['table_name'];
     }
   $this->set('table',$dataToTable);


   if(isset($_GET['table_name'])){
     $table_name = $_GET['table_name'];
     $column = "select column_name as name from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='$table_name' ";
     $result = $cn->query($column) ;
     foreach ($result as $key => $value) {
       foreach($value as $key => $value) {
        foreach($value as $key =>$value){
         $column_counts[$value] = $value;
         }
       }
    }

    $column_counts = array_unique($column_counts);
    //print_r($column_counts);
     $measures1=array();
     $measures2=array();
     $diff=array();
       foreach($column_counts as $key => $value){

         $sql="select * from $table_name where concat('',$value * 1 ) = $value limit 1";
        $resultset = $cn->query($sql) ;
        if(!empty($resultset)){

                if(!in_array($value, $measures1)){
                  $measures1[$value]= $value;
                }
        }

   }

   $measures2 = array_diff($column_counts,$measures1);
   $this->set('measures1',$measures1);
    $this->set('measures2',$measures2);
   }

   }
}

?>

Upvotes: 0

Views: 68

Answers (2)

Pradeep Singh
Pradeep Singh

Reputation: 1280

This will validate and save your data

if ($this->request->is('post')) {
     $this->Actions->set($this->request->data);
     if ($this->Actions->save($this->request->data)) {
            // Set a session flash message and redirect.
     }
}

Upvotes: 1

I think you're confusing "'required'=>true" with "'allowEmpty' => true". The former means that you cannot save the record without that field being included in the list of fields updated, no matter whether it holds actual data or not. The latter, which I think is what you really mean, allows a field to be empty although it doesn't force a declaration.

Upvotes: 0

Related Questions