第一篇关于堆栈溢出的帖子,所以我希望我做对了,我已经搜索过但找不到我要找的东西。
我是 cakephp 新手,对 php 还很陌生。我昨天能够启动并运行没有问题,并且可以将数据发送到我的数据库。今天我想用 ajax 进行验证,但我想我会暂时不考虑 ajax,因为我在显示验证错误时遇到了问题。
像这样为前两个表单字段设置验证;
<?php
class people extends AppModel
{
public $name = 'people';
public $useTable = 'people';
public $validate = array(
'firstName'=>array(
'rule'=>'notEmpty',
'message'=>'Enter You First Name'
),
'secondName'=>array(
'rule'=>'notEmpty',
'message'=>'Enter Your Second/Family Name'
),
);
}?>
如果这些字段为空,它就可以正常工作,到目前为止它不会写入数据库。但是,当我在页面刷新的表单上点击提交时,错误消息出现在表单字段下,但它还在前一个表单下添加了一个全新的表单。这是控制器。注意: validate_form 函数来自我关注的带有 ajax 教程的 cakephp,并被注释掉了
<?php
class peoplesController extends AppController
{
public $name = "peoples";
public $helpers = array('Html', 'form', 'Js');
public $components = array('RequestHandler');
public function index() {
if( $this->request->is('post'))
{
$data = $this->request->data;
$this->people->save($data);
}
}
/*public function validate_form() {
if ($this->RequestHandler->isAjax()) {
$this->data['people'][$this->params['form']['field']] = $this->params['form']['value'];
$this->people->set($this->data);
if ($this->people->validates()) {
$this->autoRender = FALSE;
}
else {
$error = $this->validateErrors($this->people);
$this->set('error', $error[$this->params['form']['field']]);
}
}
}*/
}
?>
和视图。注意:带有 id 发送和成功的 div 也来自我正在关注的教程,但我认为不会对这个特定问题产生影响。
<div id="success"></div>
<h2> Fill in your profile details </h2>
<?php
echo $this->Form->create('people');
echo $this->Form->input('firstName');
echo $this->Form->input('secondName');
echo $this->Form->input('addressOne');
echo $this->Form->input('addressTwo');
echo $this->Form->input('city');
echo $this->Form->input('county');
echo $this->Form->input('country');
echo $this->Form->input('postCode', array(
'label' => 'Zip Code',
));
echo $this->Form->input('dob', array(
'label' => 'Date of birth',
'dateFormat' => 'DMY',
'minYear' => date('Y') - 70,
'maxYear' => date('Y') - 18,
));
echo $this->Form->input('homePhone');
echo $this->Form->input('mobilePhone');
echo $this->Form->input('email', array(
'type' => 'email'
));
$goptions = array(1 => 'Male', 2 => 'Female');
$gattributes = array('legend' => false);
echo $this->Form->radio('gender',
$goptions, $gattributes
);
echo $this->Form->input('weight');
echo $this->Form->input('height');
$toptions = array(1 => 'Tandem', 2 => 'Solo');
$tattributes = array('legend' => false);
echo $this->Form->radio('trained',
$toptions, $tattributes
);
echo $this->Form->input('referedBy');
/*echo $this->Form->submit('submit');*/
echo $this->Js->submit('Send', array(
'before'=>$this->Js->get('#sending')->effect('fadeIn'),
'success'=>$this->Js->get('#sending')->effect('fadeOut'),
'update'=>'#success'
));
echo $this->Form->end();
?>
<div id="sending" style="display: none; background-color: lightgreen">Sending.... </div>
<?php
echo $this->Html->script(
'validation', FALSE);
?>
所以在同一页面上创建第二个相同的表单是我的主要问题,我认为这与控制器采用第一个表单并将其发送回相同的视图有关,但我不知道如何解决这个问题。
第二个问题是,由于某种原因,如果我使用
echo $this->Form->submit('submit');
代替
echo $this->Js->submit('send', array(
'before'=>$this->Js->get('#sending')->effect('fadeIn'),
'success'=>$this->Js->get('sending')->effect('fadeOut'),
'update'=>'#success'));
然后我不再收到错误消息,而是出现了一个气泡并说“请填写此字段”它反而显示了我想要的错误消息
提前致谢