【问题标题】:cakephp validation view issue?cakephp 验证视图问题?
【发布时间】:2013-06-29 18:50:33
【问题描述】:

第一篇关于堆栈溢出的帖子,所以我希望我做得对,我已经搜索过但找不到我要找的东西。

我是 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'));

然后我不再收到我的错误消息,而是出现一个气泡并显示“请填写此字段”我确定这是一个 jquery 问题,但我不知道如何解决问题没有出现,而是显示我想要的错误消息

提前致谢

【问题讨论】:

    标签: php javascript jquery ajax cakephp


    【解决方案1】:

    几件事:

    1) 使用大写字母作为你的类名。所以PeoplePeoplesController

    2) 在标准流程正常工作之前,不要乱用 Ajax。所以回到$this-&gt;Form-&gt;submit('submit');

    3) “必需”的工具提示是 HTML5。由于您将验证设置为notEmpty,Cake 添加了 HTML5 标记以使该字段成为必填项。修改您的 Form-&gt;create 调用以暂时绕过它(如果需要,但它提供更有效的客户端验证):

    $this->Form->create('People', array('novalidate' => true));
    

    See the FormHelper docs for more info on HTML5 validations

    【讨论】:

    • 谢谢,我不使用大写字母的原因是因为我的数据库字段不是大写字母,我可以假设那并没有什么不同?
    • 不,类名没有链接到数据库字段。模型类名链接到 db tables,但它们总是转换为小写。
    猜你喜欢
    • 2011-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-05
    • 2011-05-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多