4

I am developing Custom Component for Joomla.

I did well in validating field by adding custom rule. But if entered value does not pass through my validation, it gives error as "Invalid Field: My Field Name"

I want to replace this with my own message.

I know I can use "JText::_('LANGUAGE_STRING'). But I'm not sure where do I need to add it.

I have custom rule called "validemails" which returns false in client side as well as server side validation.

My Client side Validation is: (components/com_helpdesk/models/forms/create.js)

jQuery(document).ready(function () {
    document.formvalidator.setHandler('validemail', function (value) {
        var emails = [value];
        if (value.indexOf(';') !== -1) {
            emails = value.split(';');
        }
        else if(value.indexOf(',') !== -1) {
            emails = value.split(',');
        }
        regex = /^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/;
        var result = false;
        emails.each(function (value) {
            result = regex.test(jQuery.trim(value));
            if (result === false) {
                return false;
            }
        });
        return result;
    });
});

My Server Side Validation is: (components/com_helpdesk/models/rules/validemail.php)

use Joomla\Registry\Registry;

JFormHelper::loadRuleClass('email');

class JFormRuleValidemail extends JFormRuleEmail {

    public function test(SimpleXMLElement $element, $value, $group = null, Registry $input = null, JForm $form = null) {
        $emails = array($value);
        if (strpos($value, ';') !== false) {
            $emails = explode(';', $value);
        }
        else if (strpos($value, ',') !== false) {
            $emails = explode(',', $value);
        }

        foreach ($emails as $email) {
            if (!parent::test($element, trim($email))) {
                return false;
                continue;
            }
        }

        return true;
    }

}

Please note that I'm developing Front-end view of Component not back-end.

2 Answers 2

4

Well fortunately, I got my server side validation working by this way:

use Joomla\Registry\Registry;

JFormHelper::loadRuleClass('email');

class JFormRuleValidemail extends JFormRuleEmail {

    public function test(SimpleXMLElement $element, $value, $group = null, Registry $input = null, JForm $form = null) {
        $emails = array($value);
        if (strpos($value, ';') !== false) {
            $emails = explode(';', $value);
        }
        else if (strpos($value, ',') !== false) {
            $emails = explode(',', $value);
        }

        foreach ($emails as $email) {
            if (!parent::test($element, trim($email))) {
                ***$element->addAttribute('message', JText::_('COM_HELPDESK_ERROR_EMAIL').' '.$value);***
                return false;
                continue;
            }
        }

        return true;
    }

}

I got the solution. May be it will be useful for other googlers. Below is a code snippet for client side validation. Put below code in your custom JS:

jQuery('.validate').click(function (e) {

    var msg = {"error": []};
    if (jQuery('#jform_priority').hasClass('invalid')) {
        msg.error.push(Joomla.JText._('COM_HELPDESK_PRIORITY_ERROR'));
    }
    if (jQuery('#jform_description').hasClass('invalid')) {
        msg.error.push(Joomla.JText._('COM_HELPDESK_DESCRIPTION_ERROR'));
    }
    if (jQuery('#jform_subject').hasClass('invalid')) {
        msg.error.push(Joomla.JText._('COM_HELPDESK_SUBJECT_ERROR'));
    }      
    if (jQuery('#jform_bcc').hasClass('invalid')) {
        msg.error.push(Joomla.JText._('COM_HELPDESK_BCC_ERROR'));
    }
    if (jQuery('#jform_cc').hasClass('invalid')) {
        msg.error.push(Joomla.JText._('COM_HELPDESK_CC_ERROR'));
    }
    Joomla.renderMessages(msg);
    e.preventDefault();
});
1
  • I am not sure whether I want client side validation after this. I'll just add server side validation and end the project. It does not hurt as much as writing workarounds for joomla bugs. God thanks this is a relative short project.
    – inf3rno
    Mar 6, 2017 at 1:42
1

I don't know for the JS side of things, but for the php you would do:

$app = JFactory::getApplication();
$app->enqueueMessage(JText::_('LANGUAGE_STRING'), 'warning');
1
  • That I certainly know. But I'm trying to find a way to represent it in Custom Rule - $element->addAttribute('message', JText::_('COM_HELPDESK_ERROR_EMAIL').' '.$value); This is the way. Apr 18, 2015 at 5:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.