本文介绍了Cakephp验证后,所有表单字段都变为空与形式错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题..我尝试填写窗体,然后点击提交按钮,然后它显示窗体错误,但所有的他们的文本框字段空白空可以显示窗体错误。我想它变成相同的值从之前验证。像我不想清除/清空验证后的值。



我做了这样的事情:(in view)

 < div class =regform> 
< div id =formleft>名字< asterix> *< / asterix>< / div>
< div id =formright><?php echo $ this-> Form-> input('First Name',array(
'name'=>'firstname' ,
'label'=> false
)); ?>< / div>
< div id =formerror><?php echo $ this-> Form-> error('Customer.firstname'); ?>< / div>
< / div>

(在型号中):

 'firstname'=> array(
'firstname_cant_be_empty'=> array(
'rule'=>'notEmpty',
'message'=>'必须填写名字'
),
'firstname_must_be_alphabet'=> array(
'rule'=>'/ ^ [az] {3,} $ / i',
'message'=&名字只有字母,没有数字,虚线等。


(在控制器中):

  if($ this-> request-> is('Post')) {

if($ this-> Customer-> save($ this-> request-> data)&& $ this-> Customer-> validates()) {
// debug($ this-> request-> data);
$ this-> Session-> setFlash('good!');
// $ this-> redirect('/');
} else {
$ this-> Session-> setFlash('Fill up form now!');
$ this-> data = $ this-> request-> data;
}

}


解决方案

问题出在您的视图中:

 < div id =formright><?php echo $ this-> Form-> input('First Name',array(
'name'=>'firstname',
'label'=> false
) ?>< / div>

具体:

 code> $ this-> Form-> input('First Name',array(

 'name'=>'firstname',
pre>

这将导致生成表单元素,如:

 < input id =UserFirstnametype =textmaxlength =50name =firstname> 


b $ b

您的表单输入应该是:

  echo $ this-> Form-> input 'firstname',array(
'label'=> false
));

注意差异,这会导致form元素生成为:

 < input id =UserFirstnametype =textmaxlength =50name =data [User] [firstname]> 

主要区别是 name =data [User] [firstname]。这将导致数据以熟悉的数组格式发布$ this-> request->数据,这也允许形式在形成错误时重新填充。


I have problem.. I tried fill up form then click submit button then it show form error but all of them textbox field went blank empty with can show form error. I want it become same value from before validate. like i dont want to clear/empty out value after validation.

i did something like this: (in view)

<div class="regform">
        <div id="formleft">First Name<asterix>*</asterix></div>
        <div id="formright"><?php echo $this->Form->input('First Name', array(
                'name' => 'firstname',
                'label'=> false
            )); ?></div>
        <div id="formerror"><?php echo $this->Form->error('Customer.firstname'); ?></div>
    </div>

(in model):

'firstname' => array(
                    'firstname_cant_be_empty' => array(
                            'rule' => 'notEmpty',
                            'message' => 'First Name must be filled.'
                        ),
                    'firstname_must_be_alphabet' => array(
                            'rule' => '/^[a-z]{3,}$/i',
                            'message' => 'First Name is only letters. No numbers, Dotted, etc.'
                        )
                )

(in controller):

if ($this->request->is('Post')) {

            if ($this->Customer->save($this->request->data) && $this->Customer->validates()) {
                //debug($this->request->data);
                $this->Session->setFlash('good!');
                //$this->redirect('/');
            } else {
                $this->Session->setFlash('Fill up form now!');
                $this->data = $this->request->data;
            }

        }
解决方案

The problem is in your view:

    <div id="formright"><?php echo $this->Form->input('First Name', array(
            'name' => 'firstname',
            'label'=> false
        )); ?></div>

Specifically:

$this->Form->input('First Name', array(

and

'name' => 'firstname',

This causes to be the form element to be generated like:

<input id="UserFirstname" type="text" maxlength="50" name="firstname">

Your form input should instead be this:

    echo $this->Form->input('firstname', array(
            'label'=> false
    ));

Note the differences. This causes the form element to be generated as:

<input id="UserFirstname" type="text" maxlength="50" name="data[User][firstname]">

The main difference is name="data[User][firstname]". This causes the data to be posted in the familiar array format of $this->request->data, which also allows the form to be repopulated upon form errors.

这篇关于Cakephp验证后,所有表单字段都变为空与形式错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 14:39