Cakephp试图验证非现有输入

时间:2015-07-01 17:01:22

标签: php validation cakephp

我以前从未见过这个。我有一个表单,其中多个模型连接到一个。当我提交表单时,Cake正在尝试验证没有vaildation的字段,它甚至试图验证我更改名称的字段。我已经清除了tmp文件夹中的模型缓存。我很难过。这是我的代码。 视图:

    echo $this->Form->create('Lead', array('class' => 'form form-vertical'));
    echo $this->Form->input('Lead.project_name', array('class' => 'form-control', 'div'=>array('class'=>'col-md-12'), 'value' => 'Project'.$userId));
    echo $this->Form->input('Lead.sales_owner', array('class' => 'form-control', 'value' => $fullName, 'div'=>array('class'=>'col-md-4')));
    $leadSourceOptions = array('Door to Door' => 'Door to Door','Referral' => 'Referral','Inbound Call' => 'Inbound Call','Self Generated by Closer' => 'Self Generated by Closer','Tradeshows' => 'Tradeshows','Telemarketing' => 'Telemarketing',);
    echo $this->Form->input('Lead.lead_source', array('class' => 'form-control', 'type' => 'select', 'options' => $leadSourceOptions, 'div'=>array('class'=>'col-md-4')));
    $typeOptions = array('Individual' => 'Individual', 'Business' => 'Business', 'Government' => 'Government');
    echo $this->Form->input('Lead.type', array('class' => 'form-control', 'type' => 'select', 'options' => $typeOptions, 'div'=>array('class'=>'col-md-6')));
    $taxOptions = array('Individual' => 'Individual', 'Corporation' => 'Corporation', 'LLC (Corporation)' => 'LLC (Corporation)', 'LLC (Non-Corporation)' => 'LLC (Non-Corporation)', 'Partnership' => 'Partnership', 'Sole Proprietor' => 'Sole Proprietor', 'Tax Exempt' => 'Tax Exempt');
    echo $this->Form->input('Lead.tax_entity', array('class' => 'form-control', 'type' => 'select', 'options' => $taxOptions, 'div'=>array('class'=>'col-md-6')));
    echo $this->Form->input('Customer.first_name', array('class' => 'form-control', 'div'=>array('class'=>'col-md-5')));
    echo $this->Form->input('Customer.last_name', array('class' => 'form-control', 'div'=>array('class'=>'col-md-5')));
    $titleOptions = array('Mr' => 'Mr', 'Mrs' => 'Mrs', 'Ms' => 'Ms');
    echo $this->Form->input('Customer.title', array('class' => 'form-control', 'type' => 'select', 'options' => $titleOptions, 'div'=>array('class'=>'col-md-2')));
    echo $this->Form->input('Customer.email', array('class' => 'form-control', 'div'=>array('class'=>'col-md-5')));
    echo $this->Form->input('Customer.phone', array('class' => 'form-control', 'placeholder' => '123-456-7890', 'div'=>array('class'=>'col-md-5')));
    echo $this->Form->input('Customer.phone_extension', array('required' => false, 'class' => 'form-control', 'label' => 'Ext', 'div'=>array('class'=>'col-md-2')));
    $phoneOptions = array('Home' => 'Home', 'Mobile' => 'Mobile', 'Business' => 'Business', 'Business Fax' => 'Business Fax', 'Home Fax' => 'Home Fax', 'Pager' => 'Pager', 'Skype' => 'Skype');
    echo $this->Form->input('Customer.phone_type', array('class' => 'form-control', 'type' => 'select', 'options' => $phoneOptions, 'div'=>array('class'=>'col-md-6')));
    echo $this->Form->input('Customer.best_time_of_day_to_reach', array('class' => 'form-control', 'div'=>array('class'=>'col-md-6')));
    echo $this->Form->input('Customer.address', array('class' => 'form-control', 'div'=>array('class'=>'col-md-6')));
    echo $this->Form->input('Customer.address_line_2', array('class' => 'form-control', 'div'=>array('class'=>'col-md-6')));
    echo $this->Form->input('Customer.city', array('class' => 'form-control', 'div'=>array('class'=>'col-md-5')));
    echo $this->Form->input('Customer.state', array('class' => 'form-control', 'type' => 'select', 'options' => $states, 'selected' => 'UT', 'div'=>array('class'=>'col-md-5')));
    echo $this->Form->input('Customer.zip', array('class' => 'form-control', 'div'=>array('class'=>'col-md-2')));
    echo '<div class="clear"></div>';
    echo $this->Form->input('mailing', array('type' => 'checkbox', 'label' => 'Mailing address the is different from the home address?', 'hiddenField' => false, 'div'=>array('class'=>'col-md-12')));
    echo $this->Form->input('Customer.mailing_street', array('class' => 'form-control', 'div'=>array('class'=>'col-md-6')));
    echo $this->Form->input('Customer.mailing_address_line_2', array('class' => 'form-control', 'div'=>array('class'=>'col-md-6')));
    echo $this->Form->input('Customer.mailing_city', array('class' => 'form-control', 'div'=>array('class'=>'col-md-5')));
    echo $this->Form->input('Customer.mailing_state', array('class' => 'form-control', 'type' => 'select', 'options' => $states, 'selected' => '', 'div'=>array('class'=>'col-md-5')));
    echo $this->Form->input('Customer.mailing_zip', array('class' => 'form-control', 'div'=>array('class'=>'col-md-2')));
    $serviceTypeOptions = array('Residential' => 'Residential', 'Commercial' => 'Commercial');
    echo $this->Form->input('EnergyUsage.service_type', array('class' => 'form-control', 'type' => 'select', 'options' => $serviceTypeOptions, 'div'=>array('class'=>'col-md-4')));
    echo $this->Form->input('EnergyUsage.utility', array('class' => 'form-control', 'type' => 'select', 'options' => $utilities, 'selected' => 'Rocky Mountain Power (Utah Power)', 'div'=>array('class'=>'col-md-4')));
    $voltageOptions = array('240 V : 120 V Split Phase' => '240 V : 120 V Split Phase', 'V Delta: 277 V Wye 3 Phase' => 'V Delta: 277 V Wye 3 Phase', '208 V Delta 3 Phase' => '240 V Split Phase Delta 3 Phase (Aka Stinger, Or High Leg Delta)', '240 V Split Phase Delta 3 Phase (Aka Stinger, Or High Leg Delta)' => '208 V Delta : 120 V Wye 3 Phase', '208 V Delta : 120 V Wye 3 Phase' => '240 V Delta 3 Phase', '240 V Delta 3 Phase');
    echo $this->Form->input('EnergyUsage.service_config_voltage', array('class' => 'form-control', 'type' => 'select', 'options' => $voltageOptions, 'div'=>array('class'=>'col-md-4')));
    echo $this->Form->input('EnergyUsage.utility_account_number', array('class' => 'form-control', 'div'=>array('class'=>'col-md-6')));
    echo $this->Form->input('EnergyUsage.billing_period_one_start_date', array('class' => 'form-control datepicker', 'div'=>array('class'=>'col-md-6')));
    echo $this->Form->input('EnergyUsage.notes', array('class' => 'form-control', 'div'=>array('class'=>'col-md-12')));
    function daysInMonth($month){
        $today = date('Y-m-d');
        $date = explode('-',$today);
        if($month > $date[1]){ $year = $date[0]-1;}else{$year = date('Y');}
        return cal_days_in_month(CAL_GREGORIAN, $month, $year);
    }
    echo $this->Form->input('MonthyUsage.jan_billing_days', array('class' => 'form-control', 'value' => daysInMonth(1), 'div'=>array('class'=>'col-md-6 col-sm-6')));
    echo $this->Form->input('MonthyUsage.jan_consumption', array('class' => 'form-control', 'div'=>array('class'=>'col-md-6 col-sm-6')));
    echo $this->Form->input('MonthyUsage.feb_billing_days', array('class' => 'form-control', 'value' => daysInMonth(2), 'div'=>array('class'=>'col-md-6 col-sm-6')));
    echo $this->Form->input('MonthyUsage.feb_consumption', array('class' => 'form-control', 'div'=>array('class'=>'col-md-6 col-sm-6')));
    echo $this->Form->input('MonthyUsage.mar_billing_days', array('class' => 'form-control', 'value' => daysInMonth(3), 'div'=>array('class'=>'col-md-6 col-sm-6')));
    echo $this->Form->input('MonthyUsage.mar_consumption', array('class' => 'form-control', 'div'=>array('class'=>'col-md-6 col-sm-6')));
    echo $this->Form->input('MonthyUsage.apr_billing_days', array('class' => 'form-control', 'value' => daysInMonth(4), 'div'=>array('class'=>'col-md-6 col-sm-6')));
    echo $this->Form->input('MonthyUsage.apr_consumption', array('class' => 'form-control', 'div'=>array('class'=>'col-md-6 col-sm-6')));
    echo $this->Form->input('MonthyUsage.may_billing_days', array('class' => 'form-control', 'value' => daysInMonth(5), 'div'=>array('class'=>'col-md-6 col-sm-6')));
    echo $this->Form->input('MonthyUsage.may_consumption', array('class' => 'form-control', 'div'=>array('class'=>'col-md-6 col-sm-6')));
    echo $this->Form->input('MonthyUsage.jun_billing_days', array('class' => 'form-control', 'value' => daysInMonth(6), 'div'=>array('class'=>'col-md-6 col-sm-6')));
    echo $this->Form->input('MonthyUsage.jun_consumption', array('class' => 'form-control', 'div'=>array('class'=>'col-md-6 col-sm-6')));
    echo $this->Form->input('MonthyUsage.jul_billing_days', array('class' => 'form-control', 'value' => daysInMonth(7), 'div'=>array('class'=>'col-md-6 col-sm-6')));
    echo $this->Form->input('MonthyUsage.jul_consumption', array('class' => 'form-control', 'div'=>array('class'=>'col-md-6 col-sm-6')));
    echo $this->Form->input('MonthyUsage.aug_billing_days', array('class' => 'form-control', 'value' => daysInMonth(8), 'div'=>array('class'=>'col-md-6 col-sm-6')));
    echo $this->Form->input('MonthyUsage.aug_consumption', array('class' => 'form-control', 'div'=>array('class'=>'col-md-6 col-sm-6')));
    echo $this->Form->input('MonthyUsage.sep_billing_days', array('class' => 'form-control', 'value' => daysInMonth(9), 'div'=>array('class'=>'col-md-6 col-sm-6')));
    echo $this->Form->input('MonthyUsage.sep_consumption', array('class' => 'form-control', 'div'=>array('class'=>'col-md-6 col-sm-6')));
    echo $this->Form->input('MonthyUsage.oct_billing_days', array('class' => 'form-control', 'value' => daysInMonth(10), 'div'=>array('class'=>'col-md-6 col-sm-6')));
    echo $this->Form->input('MonthyUsage.oct_consumption', array('class' => 'form-control', 'div'=>array('class'=>'col-md-6 col-sm-6')));
    echo $this->Form->input('MonthyUsage.nov_billing_days', array('class' => 'form-control', 'value' => daysInMonth(11), 'div'=>array('class'=>'col-md-6 col-sm-6')));
    echo $this->Form->input('MonthyUsage.nov_consumption', array('class' => 'form-control', 'div'=>array('class'=>'col-md-6 col-sm-6')));
    echo $this->Form->input('MonthyUsage.dec_billing_days', array('class' => 'form-control', 'value' => daysInMonth(12), 'div'=>array('class'=>'col-md-6 col-sm-6')));
    echo $this->Form->input('MonthyUsage.dec_consumption', array('class' => 'form-control', 'div'=>array('class'=>'col-md-6 col-sm-6')));
    echo '<div class="col-md-6 margin-top3"></div>';
    echo $this->Form->input('EnergyUsage.average_billing_amount', array('class' => 'form-control', 'div'=>array('class'=>'col-md-6')));
    //echo $this->Form->input('image_upload', array('class' => 'form-control', 'div'=>array('class'=>'col-md-6'), 'type' => 'file', 'accept' => 'image/*', 'catpture' => 'camera'));
    echo $this->Form->end(__('Submit')); ?>

型号:

App::uses('AppModel', 'Model');
class Lead extends AppModel {
    public $validate = array('project_name' => array( 'notEmpty' => array('rule' => array('notEmpty'),//'message' => 'Your custom message here',),),);
    public $belongsTo = array('User' => array('className' => 'User','foreignKey' => 'user_id','conditions' => '','fields' => '', 'order' => ''));
    public $hasMany = array(
        'Customer' => array('className' => 'Customer','foreignKey' => 'lead_id','dependent' => false,),
        'EngeryUsage' => array('className' => 'EngeryUsage','foreignKey' => 'lead_id','dependent' => false, ),
        'MonthlyUsage' => array('className' => 'MonthlyUsage', 'foreignKey' => 'lead_id','dependent' => false,),);
}

App::uses('AppModel', 'Model');
class Customer extends AppModel {
    public $validate = array(
        'lead_id' => array('numeric' => array('rule' => array('numeric'),//'message' => 'Your custom message here', ),),
        'first_name' => array('notEmpty' => array('rule' => array('notEmpty'),//'message' => 'Your custom message here', ),),
        'last_name' => array('notEmpty' => array('rule' => array('notEmpty'), //'message' => 'Your custom message here',), ),
        'phone' => array('notEmpty' => array('rule' => array('notEmpty'),//'message' => 'Your custom message here',),),
        'phone_extension' => array('numeric' => array('rule' => array('numeric'), 'message' => 'Your custom message here','allowEmpty' => true,'required' => false,),),
        'address' => array('notEmpty' => array('rule' => array('notEmpty'),//'message' => 'Your custom message here',),),
        'city' => array('notEmpty' => array('rule' => array('notEmpty'),//'message' => 'Your custom message here',),),
        'state' => array('notEmpty' => array('rule' => array('notEmpty'),//'message' => 'Your custom message here',),),
        'zip' => array('numeric' => array('rule' => array('numeric'),//'message' => 'Your custom message here',),),
    );
public $belongsTo = array('Lead' => array('className' => 'Lead','foreignKey' => 'lead_id',));
}


App::uses('AppModel', 'Model');
class EnergyUsage extends AppModel {
    public $validate = array('lead_id' => array('numeric' => array('rule' => array('numeric'),//'message' => 'Your custom message here',),),);
    public $belongsTo = array('Lead' => array('className' => 'Lead','foreignKey' => 'lead_id'));
}


App::uses('AppModel', 'Model');
class MonthyUsage extends AppModel {
    public $belongsTo = array( 'Lead' => array('className' => 'Lead','foreignKey' => 'lead_id'));
}
调试验证错误时,控制器中的

我得到:

array(
    'phone_extension' => array(),
    'best_time_of_day_to_reach' => array(),
    'address_line_2' => array(),
    'mailing_street' => array(),
    'mailing_address_line_2' => array(),
    'mailing_city' => array(),
    'mailing_zip' => array(),
    'ext' => array()
)

这是删除了客户输入字段的调试语句:

array(
    'Lead' => array(
        'project_name' => 'Project1',
        'sales_owner' => 'Nate Branning',
        'lead_source' => 'Door to Door',
        'type' => 'Individual',
        'tax_entity' => 'Individual'
    ),
    'Customer' => array(
        'first_name' => '',
        'last_name' => '',
        'title' => 'Mr',
        'email' => '',
        'phone' => '',
        'phone_extension' => '',
        'phone_type' => 'Home',
        'best_time_of_day_to_reach' => '',
        'address' => '',
        'address_line_2' => '',
        'city' => '',
        'state' => 'UT',
        'zip' => '',
        'mailing_street' => '',
        'mailing_address_line_2' => '',
        'mailing_city' => '',
        'mailing_state' => 'AL',
        'mailing_zip' => ''
    ),
    'EnergyUsage' => array(
        'service_type' => 'Residential',
        'utility' => 'Rocky Mountain Power (Utah Power)',
        'service_config_voltage' => '240 V : 120 V Split Phase',
        'utility_account_number' => '',
        'billing_period_one_start_date' => '',
        'notes' => '',
        'average_billing_amount' => ''
    ),
    'MonthyUsage' => array(
        'jan_billing_days' => '31',
        'jan_consumption' => '',
        'feb_billing_days' => '28',
        'feb_consumption' => '',
        'mar_billing_days' => '31',
        'mar_consumption' => '',
        'apr_billing_days' => '30',
        'apr_consumption' => '',
        'may_billing_days' => '31',
        'may_consumption' => '',
        'jun_billing_days' => '30',
        'jun_consumption' => '',
        'jul_billing_days' => '31',
        'jul_consumption' => '',
        'aug_billing_days' => '31',
        'aug_consumption' => '',
        'sep_billing_days' => '30',
        'sep_consumption' => '',
        'oct_billing_days' => '31',
        'oct_consumption' => '',
        'nov_billing_days' => '30',
        'nov_consumption' => '',
        'dec_billing_days' => '31',
        'dec_consumption' => ''
    )
)

提前致谢。

1 个答案:

答案 0 :(得分:0)

我解决了我的问题。我的关联错了。客户,EnergyUsage和MonthyUsage属于Lead。如果错误发生在引导控制器中,我将关联作为hasMany。我把它改成了hasOne,它解决了这个问题。我认为蛋糕试图不止一次验证客户。