cakephp 2.9.7数据验证不起作用
这个问题很简单,但为什么对我来说很复杂。我也编写了数据验证。但是对于空字段,它也接受输入。请检查我的action.php中是否有任何错误 Model/action.phpcakephp 2.9.7数据验证不起作用,php,validation,cakephp,Php,Validation,Cakephp,这个问题很简单,但为什么对我来说很复杂。我也编写了数据验证。但是对于空字段,它也接受输入。请检查我的action.php中是否有任何错误 Model/action.php <?php App::uses('AppModel', 'Model'); class Actions extends AppModel { public $validate = array( 'value_to_plot' => array( 'required'=>
<?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/actionscocontroller.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);
}
}
}
?>
我想你把“'required'=>true”和“'allowEmpty'=>true”混淆了。前者意味着您不能在更新的字段列表中不包含该字段的情况下保存记录,无论该字段是否包含实际数据。后者,我认为是您真正的意思,允许字段为空,尽管它不强制声明。这将验证并保存您的数据
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.
}
}
请显示控制器代码。我已经发布了控制器。验证方法调用在哪里?谢谢。我没有编写验证方法调用。如果我编写了$this->request->save()它会自动调用验证。但我正在编写SQL查询以获取数据,因此我不知道如何调用$validate。请任何人帮助我解决此问题。您确定使用cakephp 2.9.7?非常感谢