2014-01-25 14 views
0

試圖找出這裏一個體面的做法塊...的Yii:需要的表單數據

我有一個表格,在哪裏基於如果用戶選擇下拉形式與否,它顯示或隱藏其他一些表單字段。如果表單域可見,我希望它們是必需的。如果它們不可見,我不希望它們被要求。

我試圖找出一種方法來處理這裏面我的模型規則 - 我想這樣的事情在我的模型規則()函數:

$requiredFields = 'cashAtClosing, offerPrice, closingDate, financingType,surveyDays,'. 
     'earnestMoney, escrowAgent, escrowAgentAddress, surveyProvider, surveyDays, titlePolicyPayment,'. 
     'titleObjectionDays, titleCompany, titleCompanyAddress, optionFee, optionDays, optionCredit'; 

    if ($this->financingType == "THIRDPARTYFINANCE") 
    { 
     Yii::trace("Add Financing Type Rules"); 
     $requiredFields .= ',creditApprovalRequired,creditApprovalDays,loan1Amount, loan1DueInFullYears, '. 
     'loan1InterestNotToExceed, loan1InterestNotToExceedYears, loan1OriginationNotToExceed'; 

    } 
    else 
    { 
     $safeFields .= ',creditApprovalRequired,creditApprovalDays,loan1Amount, loan1DueInFullYears, '. 
     'loan1InterestNotToExceed, loan1InterestNotToExceedYears, loan1OriginationNotToExceed'; 
    } 

    array_push($rulesArray, array($requiredFields, 'required')); 

的問題是,它似乎是規則功能在模型被填充之前被調用,所以在我的例子中$ this-> financingType總是空的,所以這段代碼不會起作用。

這裏更好的方法是什麼?

謝謝。

回答

0

嘗試將此代碼添加到模型的beforeValidate()方法中。它應該可以幫助你。

但在模型中創建$ rulesArray屬性。通過beforeValidate()方法中的$ this-> rulesArray向此變量添加新規則,並在您的rules()方法中使用此變量。