Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @version $Id$
- * @package cn_reports
- * @subpackage
- * @copyright
- * @license GNU General Public License version 2 or later.
- */
- // Creating a server side valadition for joomla
- // file name: orderid.php
- // this is a sample rule for a Joomla Component Table Field. This example overrides the test method from the base class
- // and checks to see if the input via $value is a valid number and that it exist in a an order table of the database
- // in your form you mus implement the path in the such as
- // <fieldset addrulepath="/administrator/components/com_cn_reports/models/rules">
- // your field must implement validate="orderid" in you table xml file.
- // This rule file must be saved in your joomla component folder such as
- // /administrator/components/com_cn_reports/models/rules/orderid.php
- // No direct access
- defined('_JEXEC') or die;
- // import Joomla formrule library
- jimport('joomla.form.formrule');
- use Joomla\Registry\Registry;
- /**
- * Form Rule class for the Joomla Framework.
- */
- class JFormRuleOrderid extends JFormRule
- {
- // protected $regex = '/-?[0-9]+/';
- /**
- * Method to test the url for a valid parts.
- *
- * @param SimpleXMLElement $element The SimpleXMLElement 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 Registry $input An optional Registry object with the entire data set to validate against the entire form.
- * @param JForm $form The form object for which the field is being tested.
- *
- * @return boolean True if the value is valid, false otherwise.
- *
- * @since 11.1
- */
- public function test(SimpleXMLElement $element, $value, $group = null, Registry $input = null, JForm $form = null)
- {
- // If the field is empty and not required, the field is valid.
- $required = ((string) $element['required'] == 'true' || (string) $element['required'] == 'required');
- if (!$required && empty($value))
- {
- return true;
- }
- $regxNum = '/^\d+$/';
- // Test the value against the regular expression.
- if (preg_match($regxNum, $value) == false)
- {
- JFactory::getApplication()->enqueueMessage(JText::_('COM_CN_REPORTS_OVERRIDES_FORM_ERR_ORDER_ID_WHOLENUMBER'),'warning');
- return false;
- }
- // now tha we have a valid integer lets check to see if the orderid actuall exist in the database as a valid order
- $db = JFactory::getDbo();
- $query = $db->getQuery(true);
- $query
- ->select('Count(order_id)')
- ->from('oc_order')
- ->where('order_id = ' . $value);
- $db->setQuery($query);
- $dbResult = $db->loadResult();
- if ((int)$dbResult == 0)
- {
- JFactory::getApplication()->enqueueMessage(JText::_('COM_CN_REPORTS_OVERRIDES_FORM_ERR_ORDER_ID_NON_EXIST_ORDER'),'warning');
- return false;
- }
- /*
- * @see http://www.nanpa.com/
- * @see http://tools.ietf.org/html/rfc4933
- * @see http://www.itu.int/rec/T-REC-E.164/en
- *
- * Regex by Steve Levithan
- * @see http://blog.stevenlevithan.com/archives/validate-phone-number
- * @note that valid ITU-T and EPP must begin with +.
- */
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement