Welcome Guest, Not a member yet? Register   Sign In
Custom validation (callback) for blank field value
#1

[eluser]Dan Dar3[/eluser]
Hi,

I'd like to have a custom validation message for a particular field when left blank, and for that I've tried defining a custom callback function instead of the "required" rule.

Now, it looks like the custom validation function is not called when the field is blank, only when there is a value in the field! If I attach the "required" next to my custom callback function, I get the validation behaviour and the standard "required" message.

Any thoughts, is this by design or some sort of a bug?

Thanks,
Dan
#2

[eluser]hvalente13[/eluser]
Hi,

You can set a custom message for that rule, like this:

Code:
$this->validation->set_message('required', 'Your custom message here');
#3

[eluser]Dan Dar3[/eluser]
Hi,

I believe that would set the 'required' message to all required fields on the form, rather than allowing for specific messages for each of the required fields, isn't that correct?

Thanks,
Dan
#4

[eluser]drewbee[/eluser]
Unfortunately (very much so), currently if the required rule is not set and the value is blank, no further rules will be processed. Whats even worse is that it does not process them in order, but rather it does a global check of the rules for the 'required' rule, instead of running 1 after the other when they are normally being processed.
#5

[eluser]Dan Dar3[/eluser]
Thanks drewbee,

that confirms my finding, just wondering if there are any plans to change this behaviour?
Or maybe somehow allow for custom messages for each 'required' rule, I think there is a thread on that already out there. "The %s field is required." message is not very helpful to the casual web users or any message that is mentioning the field name.

Thanks,
Dan
#6

[eluser]drewbee[/eluser]
I had something similiar along this lines as an issue, and it appears in the run() method. I had to fix it by extending and overloading the run() method in Validation class. It was basically a copy & paste of the method with the slight adjustment in it for this fix.
#7

[eluser]Dan Dar3[/eluser]
Drewbee,

Would you mind sharing (attach) your changed code?

Thanks,
Dan
#8

[eluser]drewbee[/eluser]
I can't get to my server right now, but the following line is your culprit in Validation.php (around line 197). This kicks it out of the current iteration and moves on to the next fields rules. Commenting it out should do the trick, or not including it in the overloaded method.

Code:
// Is the field required?  If not, if the field is blank  we'll move on to the next test
            if ( ! in_array('required', $ex, TRUE))
            {
                if ( ! isset($_POST[$field]) OR $_POST[$field] == '')
                {
                    continue;
                }
            }

Since this is in the SVN, it looks like we are not looking forward to a change any time soon.
#9

[eluser]Dan Dar3[/eluser]
Thanks drewbee, much appreciated, I'll have a look tonight. If you have a nicer extension / overloading way than changing CodeIgniter code, please attach when you get a chance.

Hopefully some CodeIgniter developer / admin will stumbles upon this post and have a think about.

Thanks,
Dan
#10

[eluser]drewbee[/eluser]
Changing the core is the greatest fun of CI! Big Grin Though just make sure it is always done through extensions / inheritance. Keeps things a bit cleaner, all while making upgrades no longer the fear of my life.

I'd show you my overloaded run() method, but I've added so many different 'toys' and 'helpers' ex tokenization to prevent double posting that it probably would not do you any good.




Theme © iAndrew 2016 - Forum software by © MyBB