2012-09-27 23 views
1

試圖使用beforeSave鉤與一些服務器端驗證和投擲場錯誤 - 這是工作.....那種....現場沒有顯示錯誤敏捷工具包

有一個例外是因爲它沒有按要求保存,並且如果沒有定義字段,可以使用正確的消息查看基本異常。如果我定義字段,那麼我看不到錯誤彈出窗口或crud窗體上的錯誤信息。

要清楚,它不讓我保存和拋出異常。當我使用setField('fiscal_year')它依然沒有按照例外保存,它只是不是顯示的例外。下面

代碼:

class Model_Finances extends Model_Table { 
    public $table='finances'; 

    function init() { 
     parent::init(); 

     $this->hasOne('Grant'); 

     $this->addField('fiscal_year')->datatype('int'); 
     $this->addField('requested')->datatype('money'); 
     $this->addField('committed')->datatype('money'); 
     $this->addField('spent')->datatype('money'); 

     $this->getField('grant')->hidden(true); 

     $this->addHook('beforeSave',$this); 
    } 

    function beforeSave($model){ 
     $exist = $this->api->db->dsql() 
         ->table($this->table) 
         ->field('count(1)') 
         ->where('grant_id', 2) //manually set ID for testing. 
         ->where('fiscal_year', $model['fiscal_year']) 
         ->do_getOne(); 
     // Validate fiscal year before saving 
     if($exist[0]) { 
      throw $this->exception('Fiscal year for this grant already exists')->setField('fiscal_year'); 
     } 
    } 
} 

怎麼樣正在使用:

$finances = $grant->ref('Finances'); 
    $finances->getField('fiscal_year')->setValueList($this->fiscalYears)->mandatory(true); 
    $crud=$tabs->addTab('Finances')->add('CRUD'); 
    $crud->setModel($finances); 
    if($crud->grid)$crud->grid->addTotals(array('requested', 'committed', 'spent')); 
+0

您是否正在保存表單?嘗試使用'$ form-> onSubmit(function($ f){$ f-> update() - > js() - > univ() - > successMessage('All ok') - > execute;});' – romaninsh

+0

嗨@romaninsh它是從CRUD元素中保存的。 –

+0

添加了更多代碼顯示用法@romaninsh –

回答

1

這是很難重現的問題,所以我創建了一個測試用例。

我已經改變僅此行:

throw $this->exception('Fiscal year for this grant already exists','ValidityCheck') 
    ->setField('fiscal_year'); 

和這裏導致

enter image description here

如果您提交的形式是這樣的:內部模型 - 生成

$form->onSubmit(function($f){ 
    $f->model->blah(); 
}); 

然後例外>等等也將出現很好。

+0

工作正常! - 我不記得在任何地方閱讀有關「有效性檢查」的信息,很奇怪。 –

+0

ValidityCheck真的有訣竅!這很好 - 所以,我們通常可以將所有重要的驗證移動到它們實際屬於的模型級別。 在這種特殊情況下(也可能在測試套件中),可能需要在我的評論中添加更多的where子句:id!= $ model-> id AND grant_id = $ model [grant_id] AND fiscal_year = $ model [ $ fiscal_year]或類似的東西。否則,不可能編輯現有的贈款,也不能在一年內添加多個(不同的)贈款。 +1 – DarkSide

+0

還有一點需要注意的是,該模型 - >異常('...','ValidityCheck')只有在beforeSave鉤子內部而不在模型的其他功能中才有效。 – DarkSide

0

我想我會無法完全回答你的問題,因爲我很初學者在ATK4過,但

首先,我猜你不應該在Model級別使用$ _GET。我相信它應該像grant_id = $ model ['grant_id']。

其次,據我瞭解,你想檢查一個格蘭特有獨特的財政年度?如果是這樣,那麼你應該添加兩個條件到WHERE(見下文)或類似的。

至於結果,我相信你的查詢應該是這樣的(只是一個僞代碼):

$exist = $this->api->db->dsql() 
        ->table($this->table) 
        ->field('count(*)') // I guess it can be written as ->count() too 
        ->where('grant_id',$model['grant_id']) 
        ->where('id','!=',$model->id) 
        ->where('fiscal_year',$model['fiscal_year']) 
        ->do_getOne(); 
if($exist[0]) { 
    throw $this->exception('Fiscal year for this grant already exists') 
       ->setField('fiscal_year'); 
} 

談到扔一個表單字段關聯例外,我想你應該以某種方式添加,使用表單域驗證掛鉤。但我不知道該怎麼做。

+0

仍然無法正常工作。這將根據需要停止保存,只是不顯示該字段上的錯誤。 –

+0

正如我之前所說: 說到拋出與表單字段相關的異常,我想你應該以某種方式添加使用表單字段驗證掛鉤。但我不知道該怎麼做。 – DarkSide

+0

也許你應該看看這個問題。也許它在Github的最新ATK4版本中得到修復。上週atk4_form.js有一個修復程序。也許這也會對你的情況有所幫助 - 退房! – DarkSide