【发布时间】:2014-09-24 14:35:50
【问题描述】:
我是 CakePHP 和 Ajax 的新手,在特定情况下使用 Ajax 验证时遇到了一些麻烦。
正如标题中所说,我在 jQuery blur() 事件上提交表单。每次完成 ajax 请求时,成功时我们会显示一个包含一条小消息的 success.ctp。
这里是我的 index.ctp
<h2>Edition - Id client : <?php echo h($id); ?></h2>
<hr>
<div id="success"></div>
<div>
<?php
echo $this->Form->create('Client', array(
"id" => "ajax-form"
));
?>
<fieldset>
<?php
//echo $this->Form->input('nom', array('onchange'=>"this.form.submit()"));
echo $this->Form->input('nom', array(
"id" => "nom",
));
echo $this->Form->input('prenom', array(
"id" => "prenom",
));
echo $this->Form->input('email', array(
"id" => "email",
));
echo $this->Form->input('adresse', array(
"id" => "adresse",
));
echo $this->Form->input('ville', array(
"id" => "ville",
));
echo $this->Form->input('code_postal', array(
"id" => "code_postal",
));
echo $this->Form->input('pays', array(
"id" => "pays",
));
echo $this->Form->input('num_fixe', array(
"id" => "num_fixe",
));
echo $this->Form->input('num_portable', array(
"id" => "num_portable",
));
echo $this->Form->input('genre', array(
"id" => "genre",
"type" => "select",
//"before" => '<p>Genre</p>',
//"after" => '--après--',
//"between" => '--entre---',
"separator" => '<br>',
"options" => array('' => '', 'Homme' => 'homme', 'Femme' => 'femme')
));
echo $this->Form->input('date_naissance', array(
"type" => "text",
"id" => "date_naissance",
"label" => "Date de naissance",
"class"=>'datepicker',
'empty' => true));
?>
<script type="text/javascript">
$(function () {
$('.datepicker').datepicker(
{
format: 'yyyy-mm-dd',
language:'fr',
startView:'decade',
showToday: false,
startDate:'-80y',
endDate:'-18y'
}
);
});
</script>
</div>
</fieldset>
<?php echo $this->Form->submit(
'Sauvegarder les modifications',
array( 'class' => 'btn btn-primary',
'title' => 'Savegarder'
)) ?>
<?php echo $this->Form->end(); ?>
</div>
<div class="spacer"></div>
<hr>
<p>
<?php echo $this->Html->link(
'<span class="glyphicon glyphicon-list"></span> Retour',
array('controller' => 'Clients', 'action' => 'index'),
array('class' => 'btn btn-default', 'escape' => FALSE)
); ?>
</p>
<div style="display:none;" id="sending">Sending...</div>
<?php echo $errors; ?>
<script type="text/javascript">
$(document).ready(function () {
$("#ajax-form input").blur(function () {
$.ajax({beforeSend:function (XMLHttpRequest) {
$("#sending").fadeIn();},
data:$("#ajax-form").closest("form").serialize(),
dataType:"html",
success:function (data, textStatus) {
$("#sending").fadeOut();
if(data.length < 50)
$("#success").html(data);
},
type:"post",
url:"\/clients\/index\/<?php echo h($id); ?>"
});
return false;
});
});
</script>
我的索引函数
public function index($id = null) {
if (!$id) {
throw new NotFoundException(__('Invalid post'));
}
$user = $this->Client->findById($id);
if (!$user) {
throw new NotFoundException(__('Invalid post'));
}
if ($this->request->is(array('post', 'put'))) {
$this->Client->id = $id;
$this->request->data['Client']['modified_at'] = date('Y-m-d H:i:s');
if ($this->Client->save($this->request->data)) {
if($this->RequestHandler->isAjax()){
$this->render('success', 'ajax');
}
else{
$this->Session->setFlash(__('L\'utilisateur a été mis à jour.'));
return $this->redirect(array('action' => 'recap', $id));
}
}
$this->Session->setFlash(
__('Unable to update.')
);
}
$user = $this->Client->findById($id);
$this->set('id',$id);
$this->set('user',$user);
if (!$this->request->data) {
$this->request->data = $user;
}
}
还有模特
public $validate = array(
'nom' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'Nom incorrect'
)
),
'prenom' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'Prenom incorrect'
)
),
'email' => array(
'required' => array(
'rule' => array('email'),
'message' => 'Email incorrect'
)
)
);
当数据有效时,代码可以完美运行。当他们不是时,我必须使用修复来不显示success.ctp:
$("#success").html(data);
这就是'if'所做的。否则我在success.ctp中得到一个重复的index.ctp。
有没有其他方法可以在这个 blur() 事件上使用 Ajax 验证,就像提交按钮一样。
【问题讨论】:
-
另外,当表单出现错误时,blur上的数据库中无法记录其他数据