2012-02-28 95 views
1

我創造這樣一種形式:Yii的表單元素默認值

$model = new RequestForm; 
$form = new CForm('application.views.site.requestForm', $model); 

我保存表單數據到一個會話,因此,如果用戶訪問那裏有類似形式的其他一些網頁,輸入已經填滿了。 requestForm.php看起來是這樣的:

return array(

'showErrorSummary' => false, 

'elements' => array(
    'first_name' => array(
    'type' => 'text', 
    'class' => 'standard-input', 
    'value' => Yii::app()->requestFormValues->first_name, 
), 
) 

現在,當我提交表單出現問題。假設我修改了名字 - 它從會話中獲取數據,而不是來自輸入的POST數據。我如何驗證requestForm.phprequestFormValues組件是否已提交表單,以便我不指定默認值?

親切的問候,

瑪麗安

+0

不知道這是否會解決你的問題,但我認爲''setFlash'和'getFlash'可以幫助你。當您發佈表單時,將變量設置爲flash消息並在需要時調用。順便說一句,檢查出這個進一步的信息:http://www.yiiframework.com/wiki/21/how-to-work-with-flash-messages/ – 2012-02-28 15:35:14

回答

1

使用默認值,只有當POST設置。否則,使用默認

$RequestForm = new RequestForm; 
if (isset($_POST['RequestForm'])) { 
    $RequestForm->attributes = $_POST['RequestForm']; 
    // validate, save or more.. 
} 
else { 
    $RequestForm->attributes = readFromSession(); //return array 
} 

這是幫助你嗎?

+0

我做了類似的東西:) – Marian 2012-02-29 11:52:19