Sahil Purav
Sahil Purav

Reputation: 1354

Joomla - Custom error message from form invalid fields

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.

Upvotes: 1

Views: 2401

Answers (1)

Sahil Purav
Sahil Purav

Reputation: 1354

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 fields, invalid = [], valid = true, label, error, i, l;
        fields = jQuery('form.form-validate').find('input, textarea, select');
        if (!document.formvalidator.isValid(jQuery('form'))) {
            for (i = 0, l = fields.length; i < l; i++) {
                if (document.formvalidator.validate(fields[i]) === false) {
                    valid = false;
                    invalid.push(fields[i]);
                }
            }

            // Run custom form validators if present
            jQuery.each(document.formvalidator.custom, function (key, validator) {
                if (validator.exec() !== true) {
                    valid = false;
                }
            });

            if (!valid && invalid.length > 0) {
                error = {"error": []};
                for (i = invalid.length - 1; i >= 0; i--) {
                    label = jQuery.trim(jQuery(invalid[i]).data("label").text().replace("*", "").toString());
                    if (label) {
                        if(label === 'Subject') {                            
                            error.error.push('Please Enter Subject');
                        }
                        if(label === 'Description') {
                            error.error.push('Please Enter Description');
                        }
                        if(label === 'Priority') {
                            error.error.push('Please Select Priority');
                        }
                        if(label === 'Email CCs') {
                            error.error.push('Please Enter proper Emails in CC section');
                        }
                        if(label === 'Email BCCs') {
                            error.error.push('Please Enter proper Emails in BCC section');
                        }
                    }
                }
            }
            Joomla.renderMessages(error);
        }
        e.preventDefault();
    });

Upvotes: 1

Related Questions