0
votes

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?

1
the question is: why do you want a default value? or do you mean to have a placeholder instead? - Vickel
if you want to set a default value, that value would normally come from the database, with data for the current record, or be empty if new record - Vickel

1 Answers

0
votes

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.