Page 1 of 1

after_action_edit is never called

PostPosted: Mon Aug 03, 2009 12:44 pm
by linux123
Hi Steve,

for a price input form, I need a field that is automatically updated when another is changed, i.e. when a price of a product is entered in field 1, the net price (excluding VAT) is automatically displayed in the second.

From the docs, I learned that form pre- and postprocessing on a per-field base is not implemented (as is done in commercial products that cost a lot of $), but can be done manually with Ajax/Javascript.
So I was thinking about using the after_action_edit method for the first shot, so that the field with the net price is automatically corrected before the record is written to the database.

This is what I've come up with so far:

Code: Select all
<?
class tables_LocalProducts
{
    function after_action_edit()
    {
        $app =& Dataface_Application::getInstance();
        $record =& $app->getRecord();
        $VAT=$record->val('VAT');
        $SRP=$record->val('SRP');
        $Price=$record->val('Price');
        $SRP_net=($SRP*100/(100+$VAT);
        $Price_net=($SRP*100/(100+$VAT);
        $record->setValue('Price_net',$Price_net);
        $record->setValue('SRP_net',$SRP_net);
    }


But this doesn't do anything - the Price_net and SRP_net fields are never touched.
I know the class is defined correctly since my __import__csv is working well.

What am I doing wrong? Is there a FAQ or HOWTO I missed?

Thanks+Regards
Bernd

PostPosted: Wed Aug 05, 2009 12:17 pm
by shannah
Hi Bernd,

The after_action_edit trigger is called after the record has been successfully saved and the application is ready to forward to the "Success" page. The problem here is that you are setting values on your record, but you haven't saved those changes.

If, at the end of this method you called:
$record->save()

It would work.

However I think you might be better served by the beforeSave() trigger.

e.g.
Code: Select all
function beforeSave(&$record){
        if ( $record->valueChanged('VAT') or
             $record->valueChanged('SRP') or
             $record->valueChanged('Price') ){
                $VAT=$record->val('VAT');
                $SRP=$record->val('SRP');
                $Price=$record->val('Price');
                $SRP_net=($SRP*100/(100+$VAT);
                $Price_net=($SRP*100/(100+$VAT);
                $record->setValue('Price_net',$Price_net);
                $record->setValue('SRP_net',$SRP_net);
        }
}


In this case you don't need to call $record->save() because these changes are made to the record in the course of saving - so the changes you make via set value will automatically be saved.

I threw in the call to valueChanged() just to show you that you can make these changes conditional on whether or not a particular value had been updated in the record. This can be useful if your processing can be time consuming and you want to perform it only when necessary. In this case your processing looks minimal so it looks like it wouldn't hurt anything to omit the if() statement.

Best regards

Steve

PostPosted: Thu Aug 06, 2009 3:05 am
by linux123
Hi Steve,

yes, that works fine now, thanks a lot!

I would really love to help you with the sparse docs on the delegate class functions, but I am not native English and my PHP and xataface knowledge is only beginner's level.
But maybe it's possible to add a sample to each delegate function and an overview (diagram) which function is called in what order. Probably the number of brain-dead questions like mine could be reduced by 90% ;-)

Bernd