Welcome Guest, Not a member yet? Register   Sign In
Custom error message for all rules
#1

Hi there!
I was looking for a way to use a single error message in the validation class for all rules. I managed to do it with few changes in system/Validation/Validation.php

The following lines in function setRules can be modified like this:

 -- Original -- 
PHP Code:
                if (array_key_exists('errors'$rule)) {
                    $this->customErrors[$field] = $rule['errors'];
                    unset($rule['errors']);
                

-- Modified --
PHP Code:
                if (array_key_exists('errors'$rule) && is_array($rule['errors'])) {
                    $this->customErrors[$field] = $rule['errors'];
                    unset($rule['errors']);
                } elseif(array_key_exists('error'$rule) && is_string($rule['error'])) {
                    $this->customErrors[$field] = $rule['error'];
                    unset($rule['error']);
                

And also function getErrorMessage:

-- Original --

PHP Code:
        // Check if custom message has been defined by user
        if (isset($this->customErrors[$field][$rule])) {
            $message lang($this->customErrors[$field][$rule]);
        } elseif (null !== $originalField && isset($this->customErrors[$originalField][$rule])) {
            $message lang($this->customErrors[$originalField][$rule]);
        } else {
            // Try to grab a localized version of the message...
            // lang() will return the rule name back if not found,
            // so there will always be a string being returned.
            $message lang('Validation.' $rule);
        

-- Modified --

PHP Code:
        // Check if custom message has been defined by user
        if (isset($this->customErrors[$field][$rule])) {
            $message lang($this->customErrors[$field][$rule]);
        } elseif (null !== $originalField && isset($this->customErrors[$originalField][$rule])) {
            $message lang($this->customErrors[$originalField][$rule]);
        } elseif(is_string($this->customErrors[$field])) { 
            $message $this->customErrors[$field];
        } else {
            // Try to grab a localized version of the message...
            // lang() will return the rule name back if not found,
            // so there will always be a string being returned.
            $message lang('Validation.' $rule).'kur';
        

I hope you will find this useful and this future find its way to the official release.
Reply
#2

(05-23-2024, 10:38 PM)semprom Wrote: Hi there!
I was looking for a way to use a single error message in the validation class for all rules. I managed to do it with few changes in system/Validation/Validation.php

The following lines in function setRules can be modified like this:

 -- Original -- 
PHP Code:
                if (array_key_exists('errors'$rule)) {
                    $this->customErrors[$field] = $rule['errors'];
                    unset($rule['errors']);
                

-- Modified --
PHP Code:
                if (array_key_exists('errors'$rule) && is_array($rule['errors'])) {
                    $this->customErrors[$field] = $rule['errors'];
                    unset($rule['errors']);
                } elseif(array_key_exists('error'$rule) && is_string($rule['error'])) {
                    $this->customErrors[$field] = $rule['error'];
                    unset($rule['error']);
                

And also function getErrorMessage:

-- Original --

PHP Code:
        // Check if custom message has been defined by user
        if (isset($this->customErrors[$field][$rule])) {
            $message lang($this->customErrors[$field][$rule]);
        } elseif (null !== $originalField && isset($this->customErrors[$originalField][$rule])) {
            $message lang($this->customErrors[$originalField][$rule]);
        } else {
            // Try to grab a localized version of the message...
            // lang() will return the rule name back if not found,
            // so there will always be a string being returned.
            $message lang('Validation.' $rule);
        

-- Modified --

PHP Code:
        // Check if custom message has been defined by user
        if (isset($this->customErrors[$field][$rule])) {
            $message lang($this->customErrors[$field][$rule]);
        } elseif (null !== $originalField && isset($this->customErrors[$originalField][$rule])) {
            $message lang($this->customErrors[$originalField][$rule]);
        } elseif(is_string($this->customErrors[$field])) { 
            $message $this->customErrors[$field];
        } else {
            // Try to grab a localized version of the message...
            // lang() will return the rule name back if not found,
            // so there will always be a string being returned.
            $message lang('Validation.' $rule).'kur';
        

I hope you will find this useful and this future find its way to the official release.
The official way to get this considered is to submit a PR in GitHub.
Reply
#3

(05-24-2024, 03:26 AM)Bosborne Wrote:
(05-23-2024, 10:38 PM)semprom Wrote: Hi there!
I was looking for a way to use a single error message in the validation class for all rules. I managed to do it with few changes in system/Validation/Validation.php

The following lines in function setRules can be modified like this:

 -- Original -- 
PHP Code:
                if (array_key_exists('errors'$rule)) {
                    $this->customErrors[$field] = $rule['errors'];
                    unset($rule['errors']);
                

-- Modified --
PHP Code:
                if (array_key_exists('errors'$rule) && is_array($rule['errors'])) {
                    $this->customErrors[$field] = $rule['errors'];
                    unset($rule['errors']);
                } elseif(array_key_exists('error'$rule) && is_string($rule['error'])) {
                    $this->customErrors[$field] = $rule['error'];
                    unset($rule['error']);
                

And also function getErrorMessage:

-- Original --

PHP Code:
        // Check if custom message has been defined by user
        if (isset($this->customErrors[$field][$rule])) {
            $message lang($this->customErrors[$field][$rule]);
        } elseif (null !== $originalField && isset($this->customErrors[$originalField][$rule])) {
            $message lang($this->customErrors[$originalField][$rule]);
        } else {
            // Try to grab a localized version of the message...
            // lang() will return the rule name back if not found,
            // so there will always be a string being returned.
            $message lang('Validation.' $rule);
        

-- Modified --

PHP Code:
        // Check if custom message has been defined by user
        if (isset($this->customErrors[$field][$rule])) {
            $message lang($this->customErrors[$field][$rule]);
        } elseif (null !== $originalField && isset($this->customErrors[$originalField][$rule])) {
            $message lang($this->customErrors[$originalField][$rule]);
        } elseif(is_string($this->customErrors[$field])) { 
            $message $this->customErrors[$field];
        } else {
            // Try to grab a localized version of the message...
            // lang() will return the rule name back if not found,
            // so there will always be a string being returned.
            $message lang('Validation.' $rule).'kur';
        

I hope you will find this useful and this future find its way to the official release.
The official way to get this considered is to submit a PR in GitHub.
 Well not according to GitHub.
Quote:Please submit feature requests to our [forum](https://forum.codeigniter.com/forum-29.html).

We use github issues to track bugs and planned work.
Reply
#4

(This post was last modified: 05-27-2024, 09:37 PM by ozornick.)

Right. If this is a discussion, then go to the forum. If you create a feature, test it and describe why you need it, you send a PR on github.
And please don't quote half-page posts.

Your wish can be done differently. For example, after validation in the controller, you can check "if there are errors", clear them and send a new error as setError()
Simple CI 4 project for beginners codeigniter-expenses
Reply




Theme © iAndrew 2016 - Forum software by © MyBB