Welcome Guest, Not a member yet? Register   Sign In
Form Validation Bug?
#1

I have been trying to figure this one out for a while, and I believe it breaks down to there just being no code for it in the source. So hear me out.

I have the following in my Validation config file:
PHP Code:
public $auctionCode = [
    
'code' => [
        
'label' => 'Auction Code',
        
'rules' => 'required|max_length[6]|alpha_numeric',
        
'errors' => [
            
'required' => 'Please enter an {field}',
            
'max_length' => 'The {field} entered is too long.',
        ]
    ]
]; 
When my form validation errors, for example: no code is entered, so the required rule triggers, it displays the default required error message "The Auction Code field is required.", instead of my specified "Please enter an Auction Code."

In the docs, it says that you can either do it the way I'm doing it, as it puts it "pass all settings in an array:"
https://codeigniter4.github.io/userguide...your-rules

The other way to declare custom error messages, as specified by the docs is to make another public property called $auctionCode_errors and place your errors in there, which DOES work.

I looked at the source, where it sets the Rule Group (framework/system/Validation/Validation.php Line: 475 (setRuleGroup())
and there is no code that handles the messages being set inside of the main array, instead it only checks for the $group . '_errors';
Reply
#2

Sounds like CI isn't looking at your rule.

You don't show how you call/invoke the rule, or the field entry on the page... so it's impossible to tell.

The input field's name should be 'code' in your case, 'auctionCode' is the rule's name (not the field's name).
Reply
#3

(04-15-2020, 03:25 AM)Gary Wrote: Sounds like CI isn't looking at your rule.

You don't show how you call/invoke the rule, or the field entry on the page... so it's impossible to tell.

The input field's name should be 'code' in your case, 'auctionCode' is the rule's name (not the field's name).

The rule works fine, what doesn't work is the custom error message. The rules I set in the config file work correctly, if I set required, it shows the required error, and for my signup form, when I set the email to have to be unique in the db, it works correctly, but the error message that is displayed isn't what is set in the config file, in the errors key in the "public $auctionCode" array. It does work, however, when I set the custom error messages with the "public $auctionCode_errors".

I went for a gander in the source, and I can see where it grabs the _errors array from the validation config, but there isn't anything there that checks to see if the errors key is in the main "public $auctionCode".

This same thing happens for other rule sets I have, and does it for all of the fields. I have resorted to adding the _errors arrays, but it looks so much cleaner just having the errors inside of the ruleset itself.
Reply
#4

(This post was last modified: 04-25-2020, 02:12 PM by Gary.)

I've not looked at the framework code... though, mine's been working fine, so I've not needed to.

Your rule config looks ok, so it'll be somewhere else... what does the rest of your code for the input, invoking the rule, and fetching the error look like?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB