Page 1 of 1

Implementing alternative error messages in validation rule

Posted: Fri Feb 24, 2012 7:54 pm
by spauldingsmails
I have a validation rule for my file upload field which extends JFormRule and functions correctly when validating.

My next step is to output an error based on which rule has failed; I.e. file type is incorrect, the size is too large or some other error has been encountered. So for example if the type is incorrect I could return the message "Incorrect content type detected" or for size "The file size is too large".

However, while I can return true or false from my overloaded JFormRule::test() method, I cannot set an error message based on the error which has occurred.

Is there some method by which my JFormRule could output a message based on the error encountered rather than having a generalized message such as "There is a problem with the uploaded file; please check the size and type."?

Any help much appreciated.

Re: Implementing alternative error messages in validation ru

Posted: Fri Feb 24, 2012 8:08 pm
by GlobeOfGeek
I assume you would have directly edit the called method in this case, possibly running an if statement that would decide which error output to display. I don't have "too" much experience with component coding yet, however I'm getting started. I would assume however that by editing the code manually, you could change the methods functionality all together and display a variety of error messages.

Re: Implementing alternative error messages in validation ru

Posted: Sat Feb 25, 2012 5:07 am
by spauldingsmails
Thanks for the reply.

So here's what I have done:

I have created a new custom rule, extending the JFormRule class and overridden the test method:

Code: Select all

class JFormRuleMyCustom extends JFormRule 
{
	public function test(&$element, $value, $group = null, &$input = null, &$form = null)
	{
		if ($thisUsernameDoesntExist) {
			// I would like to prompt for another username.
			return false;
		} else if ($thisEmailIDoesntExist) {
			// I would like to prompt for the correct email.
			return false;		
		} else {
			return true;
		}	
	}
}
So because I cannot attach multiple validators to a single field, I would like to validate the field and return a different message based on what has failed.

Hope this makes sense.

Re: Implementing alternative error messages in validation ru

Posted: Sat Feb 25, 2012 8:06 am
by GlobeOfGeek
Yeah that seems like a method to fixing your problem, I guess the best thing to try is simply putting such an attempt across on your site and checking to see if it works.

Use Xampp possibly if you want to run the site locally in order to keep bugs away from your users though.

Re: Implementing alternative error messages in validation ru

Posted: Mon May 12, 2014 8:24 pm
by spauldingsmails
An old post but I believe I've found quite an elegant solution for setting a custom message from the JFormRule validator.

To specify a custom message I add a 'message' attribute to the passed $element param:

Code: Select all

$element->addAttribute('message', 'Custom Error Message');

Re: Implementing alternative error messages in validation ru

Posted: Sun Jul 27, 2014 3:11 pm
by pannerrammer
I've been trying to get custom messages with no joy. Thanks for posting your recent solution. Works for me.