server-side validation rule for custom field

General questions relating to Joomla! 2.5. Note: All 1.6 and 1.7 releases have reached end of life and should be updated to 2.5. There are other boards for more specific help on Joomla! features and extensions.

Moderator: General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting.
Forum Post Assistant - If you are serious about wanting help, you should use this tool to help you post.
Locked
dc10
Joomla! Intern
Joomla! Intern
Posts: 95
Joined: Sun Jan 30, 2011 6:01 pm

server-side validation rule for custom field

Post by dc10 » Sat Apr 02, 2011 4:19 pm

Hello,

I've added a custom field numdocument in the com_users component. I've modified some files and I can select/insert/modify/delete this new field in the component. But i'm trying to validate this new field server-side, and validation requires it to be unique. So following this joomla documentation:
http://docs.joomla.org/Developing_a_Mod ... _-_Part_11
I've added to the profile and registration .xml's , after <form tag the addrulepath="/components/com_users/models/rules" and added the folder rules and the numdocument.php inside.

When I try to submit the button, a message appears:

"Validation rule missing:"

What I'm not doing correctly?

Thank you!

 
dc10
Joomla! Intern
Joomla! Intern
Posts: 95
Joined: Sun Jan 30, 2011 6:01 pm

Re: server-side validation rule for custom field

Post by dc10 » Sun Apr 03, 2011 5:15 pm

Anyone?

cempe
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Fri Apr 01, 2011 10:49 am

Re: server-side validation rule for custom field

Post by cempe » Tue May 24, 2011 12:05 pm

hi dc10,
I have the same problem as you ..
but I think I've solved it

let's see at the helloworld example in
http://docs.joomla.org/Developing_a_Mod ... _-_Part_11

please look at the admin/models/forms/helloworld.xml

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<form
	addrulepath="/administrator/components/com_helloworld/models/rules"
>
	<fieldset>
		<field
			name="id"
			type="hidden"
		/>
		<field
			name="greeting"
			type="text"
			label="COM_HELLOWORLD_HELLOWORLD_GREETING_LABEL"
			description="COM_HELLOWORLD_HELLOWORLD_GREETING_DESC"
			size="40"
			class="inputbox validate-greeting"
			validate="greeting" 
			required="true"
			default=""
		/>
	</fieldset>
</form>
I erase the validate="greeting" line, and everything works fine.
So, the code will be something like :

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<form
	addrulepath="/administrator/components/com_helloworld/models/rules"
>
	<fieldset>
		<field
			name="id"
			type="hidden"
		/>
		<field
			name="greeting"
			type="text"
			label="COM_HELLOWORLD_HELLOWORLD_GREETING_LABEL"
			description="COM_HELLOWORLD_HELLOWORLD_GREETING_DESC"
			size="40"
			class="inputbox validate-greeting"
			required="true"
			default=""
		/>
	</fieldset>
</form>
Please let me know if your problem solved too ..
and sorry for my bad English ..


Diaz
Solo, Indonesia

izharaazmi
Joomla! Apprentice
Joomla! Apprentice
Posts: 15
Joined: Sun Apr 10, 2011 9:31 am

Re: server-side validation rule for custom field

Post by izharaazmi » Thu Jun 02, 2011 3:45 pm

NO... you should not delete that line. It just says joomla to make a validation over that field before save and to use the "greeting" rule for the validation.

Now, question is from where this rule comes!!

Well, the answer is: from the path

Code: Select all

"/administrator/components/com_helloworld/models/rules"
that you place in the form xml file

Code: Select all

<form
   addrulepath="/administrator/components/com_helloworld/models/rules"
>
You need to create a rule there. Create a file named as:

Code: Select all

"/administrator/components/com_helloworld/models/rules/greeting.php"
and add your validation method in the body of the function. For your reference, I am adding my mobile number validation rule file. I have saved this as

Code: Select all

"/administrator/components/com_componentname/models/rules/mobile.php"
Here goes the code:

Code: Select all

<?php
/**
* @version        $Id: mobile.php 20196 2011-01-09 02:40:25Z ian $
* @package        Joomla.Framework
* @subpackage     Form
*/

defined('JPATH_BASE') or die;

jimport('joomla.form.formrule');

/**
* Form Rule class for the Joomla Framework.
*
* @package        Joomla.Framework
* @since          1.6
*/
class JFormRuleMobile extends JFormRule
{
    /**
    * Method to test the username for uniqueness.
    *
    * @param    object    $element    The JXMLElement object representing the <field /> tag for the
    *                                 form field object.
    * @param    mixed     $value      The form field value to validate.
    * @param    string    $group      The field name group control value. This acts as as an array
    *                                 container for the field. For example if the field has name="foo"
    *                                 and the group value is set to "bar" then the full field name
    *                                 would end up being "bar[foo]".
    * @param    object    $input      An optional JRegistry object with the entire data set to validate
    *                                 against the entire form.
    * @param    object    $form       The form object for which the field is being tested.
    *
    * @return   boolean               True if the value is valid, false otherwise.
    * @since    1.6
    * @throws   JException on invalid rule.
    */
    public function test(& $element, $value, $group = null, & $input = null, & $form = null)
    {
        /* 
         * Here we match the value with a specific format. You may also use any kind of validation,
         * If you need a value of another field as well from the same form then use the following method:
         * $userId = ($input instanceof JRegistry) ? $input->get('id') : '0';
         * this gived you the value of the Id field
         */
        return preg_match("/^\+{0,1}[0-9]{6,14}$/",$value);
    }
}
Though I am bit late, Hope this helps atleast someone!

Lund
Joomla! Apprentice
Joomla! Apprentice
Posts: 10
Joined: Tue Mar 23, 2010 2:05 pm

Re: server-side validation rule for custom field

Post by Lund » Fri Mar 02, 2012 7:51 am

@izharaazmi

it definitely helped me :)
I forgot the addrulepath="" in my xml

(Could be more clear in the component tutorial: http://docs.joomla.org/Developing_a_Mod ... _-_Part_11)

Thanks!

fade2black
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Tue Nov 20, 2012 5:48 am

Re: server-side validation rule for custom field

Post by fade2black » Sat Dec 22, 2012 9:23 pm

izharaazmi's post helped me too.
Thanx a lot dude. ;-)

izharaazmi
Joomla! Apprentice
Joomla! Apprentice
Posts: 15
Joined: Sun Apr 10, 2011 9:31 am

[Solved] server-side validation rule for custom field

Post by izharaazmi » Sun Dec 23, 2012 10:26 am

Could the author or a moderator mark this as solved, so that the needful ones can follow.

 

Locked

Return to “General Questions/New to Joomla! 2.5”