Welcome Guest, Not a member yet? Register   Sign In
Form validation & default values
#1

[eluser]antiver[/eluser]
On the site I'm building, there is a user settings page. During the signup process, the user provides their email address. On the settings page, I need to show the user's current email address setting that's been stored in the database (which I have no idea how to do) so they can just edit it without needing to type the whole thing out.

An excerpt from the settings page:
Code:
Update Email Address
<input type="text" name="email" value="<?=$this->validation->email?>">
<?=$this->validation->email_error?>

I assume I need to either change something in the code above, along with a change in the controller, or just somehow set the value of $this->validation->email in the controller when the page is first loaded.

When building all the forms on the site, I followed the guide at http://ellislab.com/codeigniter/user-gui...ation.html , but don't see anything about setting a default value for a field.

I feel like ^ this ^ is kind of vague, so let me know if it needs clarification. Hopefully you're already familiar with the behavior I'm looking for Smile

Thanks!
#2

[eluser]Popcorn[/eluser]
This has nothing to do with validation. What you will want to do is use a SQL statement to select that information from your users table then simply echo it into position.
#3

[eluser]antiver[/eluser]
Well yeah, but
Code:
<input type="text" name="email" value="<?=$this->validation->email?>">
is already inserting a value, so I assumed the validation class should have a function to set an initial, default value. If not, how do I insert both (the initial, default value and the validation results)? I'm sure I could hack it together and make it work, but isn't there a standardized (or provided) way to do this?
#4

[eluser]antiver[/eluser]
I'm currently solving the problem using this code:

Code:
<?
function field_defaultvalue($field, $defaultvalue = '') {
    $ci =& get_instance();
    
    if(!isset($_POST[$field])) {
        return $defaultvalue;
    } else {
        return $ci->validation->$field;
    }
}
?>

<input type="text" name="search" value="<?=field_defaultvalue('search', 'Company, stock symbol...')?>">
<?=$this->validation->search_error?>

I hope there's a better way...
#5

[eluser]Rick Jolly[/eluser]
Yup, there's a better way. Simply set the validation field default if the form hasn't been posted yet:
Code:
if (empty($_POST))
{
   $this->validation->email = 'your database username';
}




Theme © iAndrew 2016 - Forum software by © MyBB