当尝试使用 2 个不相关的模型验证表单时,验证错误不会出现在模型上,即客户模型。
局部视图:
<table cellpadding="0" cellspacing="10">
<tr>
<td align="right" valign="top" width="175">Username:</td>
<td><?php echo $form->input('Customer.username', array('label'=>false)); ?></td>
<td> </td>
</tr>
<tr>
<td align="right" valign="top" width="175">Email Address:</td>
<td><?php echo $form->input('Customer.email', array('label'=>false)); ?></td>
<td> </td>
</tr>
<tr>
<td align="right" valign="top" width="175">First Name:</td>
<td><?php echo $form->input('billing_firstname', array('label'=>false)); ?></td>
</tr>
<tr>
<td align="right" valign="top">Last Name:</td>
<td><?php echo $form->input('billing_lastname', array('label'=>false)); ?></td>
</tr>
<tr>
<td></td>
<td><?php echo $form->button('>> Verify Order'); ?></td>
</tr>
</table>
客户型号:
<?php
class Customer extends AppModel {
public $name = 'Customer';
var $validate = array('username' => array(array('rule'=>'notEmpty'),
array('rule'=>'/.*[a-z]+.*/i', 'message'=>'Your username must have at least one letter in it'),
array('rule'=>'isUnique', 'message'=>'Username already exists')),
'email' => array('between' => array('rule' => array('between', 5, 50), 'message' => 'Between 5 to 50 characters'),
'email' => array('rule' => 'email', 'message' => 'Please enter a valid email address'),
'identicalFieldValues' => array('rule' => array('identicalFieldValues','vemail'), 'message' => 'Both email addresses must match')));
// validation rule
function identicalFieldValues( $field=array(), $compare_field=null )
{
foreach( $field as $key => $value ){
$v1 = $value;
$v2 = $this->data[$this->name][ $compare_field ];
if($v1 !== $v2) {
return FALSE;
} else {
continue;
}
}
return TRUE;
}
}
?>
结帐信息模型:
<?php
class CheckoutInformation extends Model {
var $useTable = false;
var $validate = array('billing_firstname' => 'notEmpty',
'billing_lastname' => 'notEmpty',
'billing_address1' => 'notEmpty',
'billing_city' => 'notEmpty',
'billing_state' => array(array('rule'=>array('minLength', 2)),
array('rule'=>array('maxLength',2))),
'billing_zip' => array(array('rule'=> array('between', 5, 5), 'message'=>'Zip must be 5 digits'),
array('rule'=> 'numeric', 'message'=>'Zip must be 5 digits')),
'shipping_firstname' => 'notEmpty',
'shipping_lastname' => 'notEmpty',
'shipping_address1' => 'notEmpty',
'shipping_city' => 'notEmpty',
'shipping_state' => array(array('rule'=>array('minLength', 2)),
array('rule'=>array('maxLength',2))),
'shipping_zip' => array(array('rule'=> array('between', 5, 5), 'message'=>'Zip must be 5 digits'),
array('rule'=> 'numeric', 'message'=>'Zip must be 5 digits')),
'username' => array(array('rule'=>'notEmpty'),
array('rule'=>'/.*[a-z]+.*/i',
'message'=>'Your username must have at least one letter in it')),
'email' => array(
'email' => array('rule' => 'email',
'message' => 'Please enter a valid email address')
),
'password' => array('identicalFieldValues' => array('rule' => array('identicalFieldValues','vpassword' ), 'message' => 'Both passwords must match'),
'between' => array('rule' => array('between', 6,12),
'message' => 'Between 6 - 12 characters')),
//'member_number' => 'notEmpty',
'cc_type' => 'notEmpty',
'cc_holder' => 'notEmpty',
'cc_num' => array('rule' => array('cc', 'fast', false, null),
'message' => 'The credit card number you supplied was invalid.'),
'cc_exp_mon' => 'numeric',
'cc_exp_year' => 'numeric',
'cc_cvc' => 'numeric',
'ba_name' => 'notEmpty',
'ba_type' => array('rule' => array('inList', array('Foo', 'Bar')),
'message' => 'Please select the type of bank account.'),
'ba_bank_name' => 'notEmpty',
'ba_account_number' => array(array('rule'=>'notEmpty'),
array('rule'=>array('between', 9,15))),
'ba_routing' => array(array('rule' => 'notEmpty'),
array('rule' => array('between', 9, 15))));
// validation rule
function identicalFieldValues( $field=array(), $compare_field=null )
{
foreach( $field as $key => $value ){
$v1 = $value;
$v2 = $this->data[$this->name][ $compare_field ];
if($v1 !== $v2) {
return FALSE;
} else {
continue;
}
}
return TRUE;
}
}
?>