Recent Topics

1 Mar 20, 2012 06:05    

I'm trying to write a plugin to add additional fields to comment form.

I managed to display the fields, however, I'm a bit lost which hooks to use for validation. This is my first attempt to write a plugin and I've gone through the hooks in the source code, but the descriptions are vague and I can't find any examples.

1) Which hook is best used for field validation and display errors on the form? I tried to do it on BeforeCommentFormInsert() but the error is displayed on a different page. Is checking values in $_POST the correct way to check user input?

2) This is totally unrelated, but I was testing out GetSpamKarmaForComment(), just put a single dummy line; return 100 in this function. Shouldn't the comment always be treated as spam when this is the case? But the comment was able to submit through and was saved into database. Is there a way to prevent this from happening if it's 100? Or this is the wrong hook to use?

2 Mar 20, 2012 09:07

1) Which hook is best used for field validation and display errors on the form? I tried to do it on BeforeCommentFormInsert() but the error is displayed on a different page. Is checking values in $_POST the correct way to check user input?

CommentFormSent()

2) This is totally unrelated, but I was testing out GetSpamKarmaForComment(), just put a single dummy line; return 100 in this function. Shouldn't the comment always be treated as spam when this is the case? But the comment was able to submit through and was saved into database. Is there a way to prevent this from happening if it's 100? Or this is the wrong hook to use?

You need to configure Antispam setting for delete threshold in order to use karma. Make sure it's < 100, e.g. 99

// Change status accordingly:
if( ! is_null($spam_karma) )
{
	if( $spam_karma < $Settings->get('antispam_threshold_publish') )
	{ // Publish:
		$this->set( 'status', 'published' );
	}
	elseif( $spam_karma > $Settings->get('antispam_threshold_delete') )
	{ // Delete/No insert:
		return false;
	}
}

3 Mar 21, 2012 01:24

1) I can't figure out how to validate the new field. A 'subject' field was added to the form. What is the value to check? Any examples?

2) Thanks, set the antispam threshold to 99 and it works!

4 Mar 21, 2012 04:06

Sorry, I mistyped the function name. I can now do validation in CommentFormSent(). What is the best way to pass information to display the error message on the form?

5 Mar 21, 2012 04:53

function DisplayCommentFormFieldset( & $params )
{
    // display a text input field
    $params['Form']->text_input( $this->get_class_id('myparam'), 'default value', 50, 'My param', 'Enter the value here' );
}

function CommentFormSent( & $params )
{
    $myparam = param( $this->get_class_id('myparam'), 'string' );

	if( empty($myparam) )
	{    // The field is empty, let's display an error and reject the comment
		$this->msg( 'You must enter the value', 'error' );
	}
}

6 Mar 22, 2012 02:19

Thanks for the example.


Form is loading...