dongtingxiao4697
2016-02-28 14:53
浏览 23
已采纳

CakePHP 2中的表单验证

In my view I have

<?= $this->Form->create('Asc201516', array(
'url' => array(
    'controller' => 'asc201516s', 
    'action'     => 'submit_asc201516'
    ), 
'class' => 'form-inline',
'onsubmit' => 'return check_minteacher()'
)); ?>

<div class="form-group col-md-3">
    <?= $this->Form->input('bi_school_na', array(
        'type' => 'text',
        'onkeypress' => 'return isNumberKey(event)',
        'label'       => 'NA', 
        'placeholder' => 'NA', 
        'class'       => 'form-control'
    )); ?>
</div>
<?php 
$options = array(
    'label' => 'Submit',
    'class' => 'btn btn-primary');
echo $this->Form->end($options); 
?>

In my Controller, I have

$this->Asc201516->set($this->request->data['Asc201516']);
        if ($this->Asc201516->validates()) {
            echo 'it validated logic';
            exit();

        } else {                
            $this->redirect(
                array(
                    'controller' => 'asc201516s',
                    'action' => 'add', $semisid
                    )
                );

        }

In my Model, I have

 public $validate = array(
        'bi_school_na' => array(
            'Numeric' => array(
                'rule' => 'Numeric',
                'required' => true,
                'message' => 'numbers only',
                'allowEmpty' => false
            )
        )
    );

When I submit the form, logically it should not get submitted and print out the error message but the form gets submitted instead and validates the model inside controller which breaks the operation in controller.

图片转代码服务由CSDN问答提供 功能建议

在我的视图中我有
 &lt;?= $  this-&gt; Form-&gt; create('Asc201516',array(
'url'=&gt; array(
'controller'=&gt;'asc201516s',
'action'=&gt;'submit_asc201516'\  n),
'class'=&gt;'form-inline',
'onsubmit'=&gt;'return check_minteacher()'
));  ?&gt; 
 
&lt; div class =“form-group col-md-3”&gt; 
&lt;?= $ this-&gt; Form-&gt; input('bi_school_na',array(
'type)  '=&gt;'text',
'onkeypress'=&gt;'return isNumberKey(event)',
'label'=&gt;'NA',
'占位符'=&gt;'NA',
  'class'=&gt;'form-control'
));  ?&gt; 
&lt; / div&gt; 
&lt;?php 
 $ options = array(
'label'=&gt;'Submit',
'class'=&gt;'btn btn-primary'); \  necho $ this-&gt; Form-&gt; end($ options);  
?&gt; 
   
 
 

在我的控制器中,我有
  $ this-&gt; Asc201516-&gt  ;设置($ this-&gt; request-&gt; data ['Asc201516']); 
 if($ this-&gt; Asc201516-&gt; validates()){
 echo'the validated logic'; 
 exit  (); 
 
}其他{
 $ this-&gt;重定向(
数组(
'控制器'=&gt;'asc201516s',
'动作'=&gt;'添加',$ semisid \  n)
); 
 
} 
   
 
 

在我的模型中,我有
  public $  validate = array(
'bi_school_na'=&gt; array(
'Numeric'=&gt; array(
'rule'=&gt;'Numeric',
'required'=&gt; true,
'消息 '=&gt;'仅限数字',
'allowEmpty'=&gt; false 
)
)
); 
   
 
 

当我提交表单时 ,逻辑上它不应该提交并打印出错误消息但是 改为提交表单并验证控制器内部的模型,该模型打破了控制器中的操作。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doupeng6890 2016-02-29 11:43
    已采纳

    You have to check validation in your controller like

    $this->Asc201516->set($this->request->data);  
    if($this->Asc201516->validates()){  
        $this->Asc201516->save($this->request->data);  
    }else{  
        $this->set("semisid",$semisid);
        $this->render("Asc201516s/add");
    }
    

    You will have your ID there in variable $semisid, or you can set data in $this->request->data = $this->Asc201516->findById($semisid);

    点赞 打赏 评论

相关推荐 更多相似问题