Search code examples
osclass

How do I halt item posting on data validation error in plugin


Let's say I have a plugin Foo in osclass. In that plugin we register a hook for when an item is posted. According to the osclass hooks documentation the only hook that is run when an item is posted is posted_item.

But as far as I can tell from looking at the code this is run after the initial item data has already been validated and stored in the database. What if a validation of some plugin specific code fails and I would like to show the user an error message and present him with the form again to give him/her the chance to alter this information? Much like if you try to submit a new item but don't fill in one of the base information like description for instance.

I can't seem to find a way to do this. Only workaround I find to avoid the item being posted despite containing invalid plugin specific data, without editing the main osclass code, is to delete the posted item again in the posted_item hook callback function of my plugin. This feels extremely cumbersome and also requires every other plugin to check that the item still exists to make sure they don't save data connected to an item that is now deleted.

What I would like, and wonder if I have missed, is a hook that is run when an item is posted but before it's written to the database and have the ability to generate "errors" that would cause the item to not be posted and the user redirected back to the form with the "error" displayed just like for the basic item information.

Anyone have a solution I have missed? This feels like a very important part of plugins and without it posted items could become very fragmented.


Solution

  • A user on the osclass forums (teseo) told me about the undocumented hook pre_item_add that can be used.

    <?php
    function cust_my_plugin($aItem) {
    
        osc_add_flash_error_message('My plugin has a complaint.');
        $pItem = new CWebItem();
        $pItem->redirectTo( osc_item_post_url() );
    }
    
    osc_add_hook('pre_item_add', 'cust_my_plugin');
    ?>
    

    He also sais

    The only bad news is that you can't merge your plugin validation process with that of the core script, so if the ad had an error related to your plugin and other errors, it would be rejected twice, one by your plugin, the second by the core script. I couldn't see any workaround for this little issue.