solved Hook for server-based final validation of new comment

TiG
TiG
@tig
3 years ago
184 posts
We have a need to validate a new comment before it is saved to the database. We want to gain access to the final form of the comment content, validate and if error, leave the user in the editor with an error message and thus the means to take corrective action.

The key is to have the validation done at the server (not on the client with Javascript). Ideally this validation would be activated by an event in view_jrComment_comment_save() that would allow us to perform the validation and then fail the transaction with an error message when appropriate.

Stated differently, the jrPostComment() Javascript function executes when a new comment is ready to be posted. This function issues an AJAX transaction which triggers view_jrComment_comment_save(). We would like to insert validation that looks and behaves like an error message issued by the view_jrComment_comment_save() function.

Aside:
The update portion of this problem is solved by a 'form_validate_init' listener. The create portion of the problem remains unsolved. We have a Javascript solution for the create portion but that is not ideal for security reasons and it is redundant code.


--
TiG

updated by @tig: 03/05/22 05:55:38AM
TiG
TiG
@tig
3 years ago
184 posts
Looks like a 'run_view_function' listener can do the trick. Would you folks agree that this is the right way to go?


--
TiG

updated by @tig: 12/02/21 02:35:10PM
michael
@michael
3 years ago
7,714 posts
The normal one you'd use is
'db_create_item'

If you were in a REAL bind after that there is
$func = jrCore_get_active_datastore_function($module, 'db_create_item');

Where you could choose not to use jamrooms datatore function and instead roll your own by copying the
_jrCore_db_create_item()
as a starting point.

--
Yeah those are the last.

Another option to try is to change the priority of the module in its _meta() function, eg the jrSmiley module for some reason wants to be called later in the flow so sets its weight to 80

function jrSmiley_meta(){
    return array(
        'name'        => 'Smiley Support',
        'url'         => 'smiley',
        'version'     => '1.4.2',
        'developer'   => 'The Jamroom Network, ©' . strftime('%Y'),
        'description' => 'Replace smiley string with a graphic in text fields',
        'doc_url'     => 'https://www.jamroom.net/the-jamroom-network/documentation/modules/2915/smiley-support',
        'category'    => 'site',
        'priority'    => 80,
        'license'     => 'mpl',
        'requires'    => 'jrCore:6.5.0'
    );
}

Setting your weight to even higher could put you later than whatever you want to avoid.
michael
@michael
3 years ago
7,714 posts
ah sorry, those are the last points before storing the comment. I need to read better. I'll look at returning validation.
michael
@michael
3 years ago
7,714 posts
sure, 'run_view_function' is a good place to check it. Before the _save() function has even started. Best spot.
TiG
TiG
@tig
3 years ago
184 posts
Thanks Michael!


--
TiG
brian
@brian
3 years ago
10,148 posts
Yep that's a good listener location - the other one if you need to get even earlier is the jrCore parse_url event.


--
Brian Johnson
Founder and Lead Developer - Jamroom
https://www.jamroom.net
TiG
TiG
@tig
3 years ago
184 posts
Excellent because that is how I implemented this. Nice, clean server-based implementation.

Thanks Brian, TiG


--
TiG

Tags