doutonghang2761 2013-07-09 16:30
浏览 27
已采纳

在beforeSave函数之后进行CakePHP 2.x验证

I am trying to add a validation rule to my Model in CakePHP to check that an ip address is unique. The problem is that I am saving the ip address in my database as an unsigned int, but the user is entering it as a string. To do this I am using a beforeSave function that changes the ip address to the int value that will be saved. Is there a way to make the isUnique rule run after the beforeSave function? Currently my validation rules look like this.

    public $validate = array(
    'ip_address' => array(
        'notEmpty' => array(
            'rule' => 'notEmpty',
            'message' => 'You must enter an IP address'
        ),
        'unique' => array(
            'rule' => 'isUnique',
            'required' => 'create',
            'message' => 'This IP address already exists'
        )
    )
);
  • 写回答

1条回答 默认 最新

  • doumengbai2031 2013-07-09 17:43
    关注

    Do this in beforeValidate():

    $this->data['alias']['ip_address'] = str_replace('.', '', $this->data['alias']['ip_address'];
    

    And it will work fine. Why int and not string by the way? int might give you duplicates. I don't know your code to turn the ip into an int so I might be wrong.

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

报告相同问题?

悬赏问题

  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了