Search code examples
phpvalidationcodeigniter-3

Codeigniter form validation - how to reject default values


I have a form with default values set, e.g.:

 <input type="text" id="mail" name="mail" value="<?php echo set_value('mail', '[email protected]'); ?>" />

I validate the form using Codeigniter's form_validation class. For this field:

$config = array(
        array(
            'field' =>  'user',
            'label' =>  'Naam',
            'rules' =>  'trim|required|valid_email'
        ));

Here's my problem. When the user leaves the field untouched and submits it's default value '[email protected]', I want to reject this value. I know I can use a callback, but that would mean I have to write a callback for every field of the form. Not nice!

I've looked into the validation rules. 'differs' looked promising, but it only checks whether one field is different from another field.

Now what's the best way to deal with default values in Codeigniter?


Solution

  • Sounds like you should be using a placeholder instead of a default value. Otherwise what you are asking for doesn't make much sense.

    <input type="text" id="mail" name="mail" value="<?php echo set_value('mail'); ?>" placeholder="[email protected]" />
    

    Otherwise you could try the built in validation rule "in_list" e.g. in_list[red,blue,green]

    $config = array(
            array(
                'field' =>  'user',
                'label' =>  'Naam',
                'rules' =>  'trim|required|valid_email|in_list[[email protected]]'
            ));
    

    EDIT: Actually you would want the opposite of the in_list rule, i think that would require a callback, but you would only need to write one callback to use for each field.