我遇到了CakePHP验证系统的问题:要保存我的数据,我使用“数组格式化数据”方法,如下所示:
$this->ReleveCompteur->save($tempData, false, $fieldList)
如果我将验证参数设置为“true”,它总是返回This field cannot be left blank
以下是我的模型中的验证规则:
public $validate = array(
'PERKEY' => 'alphaNumeric',
'RCO_TITLE' => 'alphaNumeric',
'RCO_SUPPLIER_SERIAL_NUMBER' => 'alphaNumeric',
'PER_EMAIL' => 'email',
'ASA_COUNTER_ID' => 'numeric',
'CST_STATEMENT_DATE' => 'date',
'CST_STATEMENT_QUANTITY' => 'numeric'
);
如果我将验证参数设置为“false”,一切正常,我的数据库中有正确的数据。 我不明白为什么我的验证不起作用。所有数据都正确传递。
以下是验证返回的内容:
["The Post could not be saved. Please, try again.",
{"RCO_TITLE":["This field cannot be left blank", "This field cannot be left blank"],
"CST_STATEMENT_DATE":["This field cannot be left blank", "This field cannot be left blank"]},
{"CST_STATEMENT_QUANTITY":["3231", "321312", "231"], "PER_KEY1":["745632", "745632", "745658"],
"RCO_TITLE":["1.018.462", "1.018.462", "1.018.462"],
"RCO_SUPPLIER_SERIAL_NUMBER":["0208800817", "6508410817", "3608850817"],
"PER_EMAIL":["johndoe@domain.com", "johndoe@domain.com", "johndoe@domain.com"],
"ASA_COUNTER_ID":["105", "108", "109"],
"cpt":"3",
"CODE_ACCES":"474125gh69474125gh69"},
{"RCO_TITLE":["This field cannot be left blank", "This field cannot be left blank"],
"CST_STATEMENT_DATE":["This field cannot be left blank", "This field cannot be left blank"]}]
以下是“POST”中的内容
ASA_COUNTER_ID[] 105
ASA_COUNTER_ID[] 108
ASA_COUNTER_ID[] 109
CODE_ACCES 474125gh69474125gh69
CST_STATEMENT_QUANTITY[] 3231
CST_STATEMENT_QUANTITY[] 321312
CST_STATEMENT_QUANTITY[] 231
PER_EMAIL[] johndoe@domain.com
PER_EMAIL[] johndoe@domain.com
PER_EMAIL[] johndoe@domain.com
PER_KEY1[] 745632
PER_KEY1[] 745632
PER_KEY1[] 745658
RCO_SUPPLIER_SERIAL_NUMBE... 0208800817
RCO_SUPPLIER_SERIAL_NUMBE... 6508410817
RCO_SUPPLIER_SERIAL_NUMBE... 3608850817
RCO_TITLE[] 1.018.462
RCO_TITLE[] 1.018.462
RCO_TITLE[] 1.018.462
_method POST
cpt 3
我的问题是我必须在一个表中一次插入多行。我得到了从另一个表中插入的行数。然后,我使用循环来创建表单,对于每个字段,我将数据插入数据库。
有人可以帮我一把,或者只是向我解释我做错了什么?非常感谢!非常感谢你!
这是我的控制器:
<?php
class ReleveCompteursController extends AppController {
public $helpers = array('Html', 'Form', 'Js' => array('Jquery'),'Ajax','Javascript');
public $components = array('RequestHandler');
public function index() {
$cpt = ClassRegistry::init('ListeCompteur')->find('all', array('conditions' => array('ListeCompteur.CODE_ACCES' => '474125gh69474125gh69'), 'order'=>array('ListeCompteur.PER_KEY1 ASC')));
$this->set('cpt', $cpt);
}
public function add() {
if (!empty($this->data)) {
for ($i = 0; $i < $this->data['cpt']; $i++) {
$tempData = array(
'PER_KEY1' => $this->data['PER_KEY1'][$i],
'RCO_TITLE' => $this->data['RCO_TITLE'][$i],
'RCO_SUPPLIER_SERIAL_NUMBER' => $this->data['RCO_SUPPLIER_SERIAL_NUMBER'][$i],
'PER_EMAIL' => $this->data['PER_EMAIL'][$i],
'ASA_COUNTER_ID' => $this->data['ASA_COUNTER_ID'][$i],
'CST_STATEMENT_DATE' => date('Y-m-d H:i:s'),
'CST_STATEMENT_QUANTITY' => $this->data['CST_STATEMENT_QUANTITY'][$i]
);
$fieldList = array('PER_KEY1','RCO_TITLE','RCO_SUPPLIER_SERIAL_NUMBER','PER_EMAIL','ASA_COUNTER_ID','CST_STATEMENT_DATE','CST_STATEMENT_QUANTITY');
$previous = $this->ReleveCompteur->find('first', array('conditions' => array('ReleveCompteur.ASA_COUNTER_ID' => $this->data['ASA_COUNTER_ID'][$i])));
if( !empty( $previous ) ) {
$tempData['ASA_COUNTER_ID'] = $previous[ 'ReleveCompteur' ][ 'ASA_COUNTER_ID' ];
}
else {
$this->ReleveCompteur->create();
}
if ($this->ReleveCompteur->save($tempData, false, $fieldList)) {
$message = __('The Post has been saved.', true);
$Post = 'OK';
$data = $this->data;
$errors = '';
}
else {
$message = __('The Post could not be saved. Please, try again.', true);
$Post = $this->ReleveCompteur->invalidFields();
$data = $this->data;
$errors = $this->ReleveCompteur->validationErrors;
}
}
//Via AJAX
if($this->request->isAjax()){
$array = array($message, $Post, $data, $errors);
return new CakeResponse(array('body' => json_encode($array)));
}
else{
$this->Session->setFlash($message);
return $this->redirect(array('action' => 'index'));
}
}
}
}
这是我的观点:
<!-- File: /app/View/ReleveCompteurs/index.ctp -->
<?php
echo $this->Form->create('ReleveCompteur', array('default' => false, 'id' =>'ReleveCompteurForm', 'type' => 'post', 'action' => '#'));
$i = 1;
$perKey1 = '';
$cptSize = sizeof($cpt);
foreach($cpt as $item) :
if ($perKey1 != $item['ListeCompteur']['PER_KEY1']) {
$perKey1 = $item['ListeCompteur']['PER_KEY1'];
?>
<dl>
<dt>Client : <?php echo $item['ListeCompteur']['PER_KEY1']; ?></dt>
<?php } ?>
<dd><?php echo $item['ListeCompteur']['RCO_TITLE']; ?></dd>
<?php
echo $this->Form->input('CST_STATEMENT_QUANTITY',
array(
'label' => array(
'text' => 'Etat du compteur'
),
'name' => "CST_STATEMENT_QUANTITY[]",
'id' => "CST_STATEMENT_QUANTITY[]",
'placeholder' => 'Etat du compteur',
'error' => array(
'attributes' => array(
'wrap' => 'div',
'class' => 'alert-box'
)
)
)
);
echo $this->Form->input('PER_KEY1',
array('type' => 'hidden',
'value' => $item['ListeCompteur']['PER_KEY1'],
'name' => "PER_KEY1[]",
'id' => "PER_KEY1[]")
);
echo $this->Form->input('RCO_TITLE',
array('type' => 'hidden',
'value' => $item['ListeCompteur']['RCO_TITLE'],
'name' => "RCO_TITLE[]",
'id' => "RCO_TITLE[]")
);
echo $this->Form->input('RCO_SUPPLIER_SERIAL_NUMBER',
array('type' => 'hidden',
'value' => $item['ListeCompteur']['RCO_SUPPLIER_SERIAL_NUMBER'],
'name' => "RCO_SUPPLIER_SERIAL_NUMBER[]",
'id' => "RCO_SUPPLIER_SERIAL_NUMBER[]")
);
echo $this->Form->input('PER_EMAIL',
array('type' => 'hidden',
'value' => $item['ListeCompteur']['PER_EMAIL'],
'name' => "PER_EMAIL[]",
'id' => "PER_EMAIL[]")
);
echo $this->Form->input('ASA_COUNTER_ID',
array('type' => 'hidden',
'value' => $item['ListeCompteur']['ASA_COUNTER_ID'],
'name' => "ASA_COUNTER_ID[]",
'id' => "ASA_COUNTER_ID[]")
);
if ($perKey1 != $item['ListeCompteur']['PER_KEY1'] || $i == $cptSize) { ?>
</dl>
<?php }
$i++;
endforeach;
echo $this->Form->input('cpt',
array('type' => 'hidden',
'value' => $cptSize,
'name' => "cpt",
'id' => "cpt")
);
echo $this->Form->input('CODE_ACCES',
array('type' => 'hidden',
'value' => $cpt[0]['ListeCompteur']['CODE_ACCES'],
'name' => "CODE_ACCES",
'id' => "CODE_ACCES")
);
echo $this->Form->end('Sauvegarder');
?>
<?php
//URL du Controller
$url = Router::url(array('controller' => 'ReleveCompteurs','action' => 'add'));
?>
<script>
$('#ReleveCompteurForm').submit(function(event) {
event.preventDefault();
var form = $(this).serialize();
$.ajax({
type: "POST",
url: "<?php echo $url; ?>",
data: form,
success: function(data){
$.parseJSON(data);
console.log(data);
}
});
return false;
});