验证“需要至少一个电话号码”在CakePHP [英] Validation for "at least one phone number is required" in CakePHP

查看:198
本文介绍了验证“需要至少一个电话号码”在CakePHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是模型文件 vechile_enquiry.php

<?php
    class VechileEnquiry  extends AppModel{
        var $name ='VechileEnquiry';
        var $validate = array('name' => array
                                            ('rule' => 'notEmpty',
                                            'message' => 'Please type name')
                            );
    }
?>

这是视图文件 vechile.ctp

<?php
    echo $this->Form->input('name', array('label'=>false));
?>

至少需要一个电话号码:

At least one phone number is required:

<?php
    echo $this->Form->input('mobile_phone', array('label'=>false));
    echo $this->Form->input('work_phone', array('label'=>false));
    echo $this->Form->input('home_phone', array('label'=>false));
?>

验证在名称字段中有效,但我没有得到如何在 mobile_phone
work_phone home_phone 至少需要一个电话号码。

Validation is working in the name field but I'm not getting how to implement validation in mobile_phone, work_phone, home_phone for the condition that at least one phone number is required.

推荐答案

这应该适合您:

    var $validate = array(
    'name' => array(
        'rule' => 'notEmpty',
        'message' => 'Please type name'
    ),
    'mobile_phone' => array(
        'check_phone' => array(
            'rule' => array('hasPhone'),
            'required' => false,
            'allowEmpty' => true,
            'message' => 'At least one phone number is required.'
        )
    ),
    'work_phone' => array(
        'check_phone' => array(
            'rule' => array('hasPhone'),
            'required' => false,
            'allowEmpty' => true,
            'message' => 'At least one phone number is required.'
        )
    ),
    'home_phone' => array(
        'check_phone' => array(
            'rule' => array('hasPhone'),
            'required' => false,
            'allowEmpty' => true,
            'message' => 'At least one phone number is required.'
        )
    )
); 


function hasPhone($field){
    if(!empty($this->data[$this->name]['mobile_phone']) || !empty($this->data[$this->name]['work_phone']) || !empty($this->data[$this->name]['home_phone'])){
        return true;
    } else {
        return false;
    }
}

这篇关于验证“需要至少一个电话号码”在CakePHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆