Implementing alternative error messages in validation rule

For Joomla! 2.5 Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general
Note: All 1.6, 1.7 and 3.5 releases have reached end of life and should be updated to 3.x.

Moderator: ooffick

Forum rules
Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.
Locked
User avatar
spauldingsmails
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 156
Joined: Sat Mar 04, 2006 10:55 am
Location: Perth, WA, Australia
Contact:

Implementing alternative error messages in validation rule

Post by spauldingsmails » Fri Feb 24, 2012 7:54 pm

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.
You know it's easy to grin when your ship comes in and you've got the stockmarket beat. But  the man worthwhile is the man who can smile when his shorts aren't too tight in the seat.

GlobeOfGeek
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 204
Joined: Mon Oct 03, 2011 6:52 pm
Location: Canada
Contact:

Re: Implementing alternative error messages in validation ru

Post by GlobeOfGeek » Fri Feb 24, 2012 8:08 pm

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.
Names Kyle, pleasure to be a service to the joomla community!
I work with PhP, Database Management, and of course, Joomla stuff of all kinds.
My goal(s):
- To ensure that every legit question is answered, and to become an elite Joomla developer.

User avatar
spauldingsmails
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 156
Joined: Sat Mar 04, 2006 10:55 am
Location: Perth, WA, Australia
Contact:

Re: Implementing alternative error messages in validation ru

Post by spauldingsmails » Sat Feb 25, 2012 5:07 am

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.
You know it's easy to grin when your ship comes in and you've got the stockmarket beat. But  the man worthwhile is the man who can smile when his shorts aren't too tight in the seat.

GlobeOfGeek
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 204
Joined: Mon Oct 03, 2011 6:52 pm
Location: Canada
Contact:

Re: Implementing alternative error messages in validation ru

Post by GlobeOfGeek » Sat Feb 25, 2012 8:06 am

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.
Names Kyle, pleasure to be a service to the joomla community!
I work with PhP, Database Management, and of course, Joomla stuff of all kinds.
My goal(s):
- To ensure that every legit question is answered, and to become an elite Joomla developer.

User avatar
spauldingsmails
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 156
Joined: Sat Mar 04, 2006 10:55 am
Location: Perth, WA, Australia
Contact:

Re: Implementing alternative error messages in validation ru

Post by spauldingsmails » Mon May 12, 2014 8:24 pm

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');
You know it's easy to grin when your ship comes in and you've got the stockmarket beat. But  the man worthwhile is the man who can smile when his shorts aren't too tight in the seat.

pannerrammer
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Thu Dec 04, 2008 11:14 am

Re: Implementing alternative error messages in validation ru

Post by pannerrammer » Sun Jul 27, 2014 3:11 pm

I've been trying to get custom messages with no joy. Thanks for posting your recent solution. Works for me.


Locked

Return to “Joomla! 2.5 Coding”