dragon2025 2013-06-29 18:50
浏览 51
已采纳

cakephp验证视图问题?

First post here on stack overflow so I hope I do it right, I have searched but cannot find what I am looking for.

i am new to cakephp and fairly new to php. I was able to get up and running yesterday no problem and can send data to my database. to day I wanted to work on validation with ajax but I think I am going to leave the ajax out of it for a little while as I have a problem with the validation errors displaying.

The validation is set up for the first two form fields like this;

<?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'
            ),
        );
}?>

and it works fine if those fields are empty it wont write to the database so far so good. However, when I hit submit on the form the page refreshes, the error messages appear under the form fields but it also adds a completely new form under the previous one. here is the controller. Note: the validate_form function is from an cakephp with ajax tutorial i was following and is commented out

<?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']]);
                }

        }
    }*/

    }

    ?>

and the view. note: the divs with id sending and success are also from the tutorial I was following but I dont think would have an effect on this particular issue.

    <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); 
     ?>

so the creation of the second identical form on the same page is my primary problem, I think it has something to do with the controller taking the first form and sending it back to the same view but I dont know how to trouble shoot this.

a second problem is that for some reason if I use

echo $this->Form->submit('submit');

instead of

echo $this->Js->submit('send', array(
'before'=>$this->Js->get('#sending')->effect('fadeIn'),
'success'=>$this->Js->get('sending')->effect('fadeOut'),
'update'=>'#success'));

Then I dont get my error messages anymore I instead just get a bubble that appears and says 'please fill in this field' I am sure this is a jquery issue but again I dont know how to trouble shoot it so that that bullbe does not appear and it instead shows the error messages I want

Thanks in advance

  • 写回答

1条回答 默认 最新

  • dongyongju9560 2013-06-29 21:37
    关注

    Couple things:

    1) Use Caps for your classnames. So People, PeoplesController, etc

    2) Don't mess with Ajax until you get the standard flow working. So go back to $this->Form->submit('submit');.

    3) That "required" tooltip is HTML5. Since you set the validation to notEmpty, Cake adds HTML5 markup to make the field required. Modify your Form->create call to bypass that for now (if you need to, but it provides client-side validation which is more efficient):

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

    See the FormHelper docs for more info on HTML5 validations

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?