試圖使用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'));
您是否正在保存表單?嘗試使用'$ form-> onSubmit(function($ f){$ f-> update() - > js() - > univ() - > successMessage('All ok') - > execute;});' – romaninsh
嗨@romaninsh它是從CRUD元素中保存的。 –
添加了更多代碼顯示用法@romaninsh –