首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CakePHP -基于数据库字段的表单验证

问CakePHP -基于数据库字段的表单验证
EN

Stack Overflow用户
提问于 2014-07-22 06:40:01
回答 1查看 218关注 0票数 0

我需要验证货物发送时输入的装运信息。(我在用蛋糕做噩梦!)

每一家航运公司都有不同的跟踪参考格式。我编写了一些regex来验证,这些正则表达式存储在我的数据库中。

CakePHP的所有验证都发生在模型中,因此我不能使用$this检索正确的正则表达式。

正则表达式在视图中可用;在提交表单之前,是否可以使用该正则表达式进行验证?

我目前正在通过ajax调用发送数据。

控制器

代码语言:javascript
复制
    public function editTracking() {
    $this->autoRender = false;

    if ($this->request->is('ajax')) {
        if($this->GoodsOutNote->save($this->request->data['GoodsOutNote'])){
            $this->GoodsOutNote->save($this->request->data['GoodsOutNote']);
            print_r($this->request->data['GoodsOutNote']['tracking_details']);
        }else{
            print_r($errors = $this->GoodsOutNote->validationErrors);
        }

    }
}

视图

代码语言:javascript
复制
<?php echo $this->Form->create('GoodsOutNote',array(
'action'=>'editTracking','default' => false)); ?>
<fieldset>
<?php
    echo $this->Form->input('id',array(
        'default'=>$goodsOutNote['GoodsOutNote']['id']));

    echo $this->Form->input('tracking_details',array(
        'default'=>$goodsOutNote['GoodsOutNote']['tracking_details']));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit'));

$data = $this->Js->get('#GoodsOutNoteEditTrackingForm')->serializeForm(array(
'isForm' => true, 'inline' => true));

$this->Js->get('#GoodsOutNoteEditTrackingForm')->event('submit',
$this->Js->request(
array('action' => 'editTracking', 'controller' => 'goods_out_notes'),
    array(
        'update' => '#tracking_details,#GoodsOutNoteTrackingDetails',
        'data' => $data,
        'async' => true,    
        'dataExpression'=>true,
        'method' => 'PUT'
    )
  )
);
 echo $this->Js->writeBuffer(); 
?>

在视图中,我可以使用$goodsOutNote['ShippingMethod']['valid_regex']访问正确的格式,但我不知道如何将它传递给表单。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-23 00:26:44

我已经使用以下方法修复了这个问题。

在我的控制器中,我检索我将要编辑的全部记录。然后,我可以使用preg_match()验证输入。我真的很想就此发表意见-有没有更好的办法?

代码语言:javascript
复制
public function editTracking() {
    $this->autoRender = false;

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

        $id = $this->request->data['GoodsOutNote']['id'];
        $options = array('conditions' => array('GoodsOutNote.' . $this->GoodsOutNote->primaryKey => $id));
        $goodsOutNote = $this->GoodsOutNote->find('first', $options);

        $trackingRef = $this->request->data['GoodsOutNote']['tracking_details'];
        $regex = "/".$goodsOutNote['ShippingMethod']['valid_regex']."/";

        if(preg_match($trackingRef,$regex)){
            if($this->GoodsOutNote->save($this->request->data['GoodsOutNote'])){
                $this->GoodsOutNote->save($this->request->data['GoodsOutNote']);
            print_r($trackingRef);
            }
            else{
            print_r($errors = $this->GoodsOutNote->validationErrors);
            }
        }
        else {
            print_r($errors = $trackingRef.'is not valid');
        }

    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24880487

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档