问题
This is the model file vechile_enquiry.php
<?php
class VechileEnquiry extends AppModel{
var $name ='VechileEnquiry';
var $validate = array('name' => array
('rule' => 'notEmpty',
'message' => 'Please type name')
);
}
?>
This is the view file 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));
?>
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.
回答1:
This should do it for you:
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;
}
}
来源:https://stackoverflow.com/questions/9719033/validation-for-at-least-one-phone-number-is-required-in-cakephp