The Joomla! Forum ™



Forum rules


Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.



Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Fri Feb 24, 2012 7:54 pm 
User avatar
Joomla! Enthusiast
Joomla! Enthusiast

Joined: Sat Mar 04, 2006 10:55 am
Posts: 152
Location: Perth, WA, Australia
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.


Top
 Profile  
 
PostPosted: Fri Feb 24, 2012 8:08 pm 
Joomla! Enthusiast
Joomla! Enthusiast

Joined: Mon Oct 03, 2011 6:52 pm
Posts: 204
Location: Canada
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.


Top
 Profile  
 
PostPosted: Sat Feb 25, 2012 5:07 am 
User avatar
Joomla! Enthusiast
Joomla! Enthusiast

Joined: Sat Mar 04, 2006 10:55 am
Posts: 152
Location: Perth, WA, Australia
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:
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.


Top
 Profile  
 
PostPosted: Sat Feb 25, 2012 8:06 am 
Joomla! Enthusiast
Joomla! Enthusiast

Joined: Mon Oct 03, 2011 6:52 pm
Posts: 204
Location: Canada
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.


Top
 Profile  
 
PostPosted: Mon May 12, 2014 8:24 pm 
User avatar
Joomla! Enthusiast
Joomla! Enthusiast

Joined: Sat Mar 04, 2006 10:55 am
Posts: 152
Location: Perth, WA, Australia
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:
$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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 



Who is online

Users browsing this forum: No registered users and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group