Part of the EllisLab Network
   
 
Form validation & default values
Posted: 23 May 2008 07:27 PM   [ Ignore ]  
Summer Student
Avatar
Total Posts:  26
Joined  05-20-2008

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:

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://codeigniter.com/user_guide/libraries/validation.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!

Profile
 
 
Posted: 23 May 2008 09:18 PM   [ Ignore ]   [ # 1 ]  
Lab Assistant
RankRank
Total Posts:  248
Joined  11-08-2007

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.

 Signature 

Redux Auth is no longer maintained.

Profile
 
 
Posted: 23 May 2008 09:27 PM   [ Ignore ]   [ # 2 ]  
Summer Student
Avatar
Total Posts:  26
Joined  05-20-2008

Well yeah, but

<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?

Profile
 
 
Posted: 23 May 2008 10:38 PM   [ Ignore ]   [ # 3 ]  
Summer Student
Avatar
Total Posts:  26
Joined  05-20-2008

I’m currently solving the problem using this 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…

Profile
 
 
Posted: 24 May 2008 01:58 AM   [ Ignore ]   [ # 4 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  770
Joined  02-06-2007

Yup, there’s a better way. Simply set the validation field default if the form hasn’t been posted yet:

if (empty($_POST))
{
   $this
->validation->email 'your database username';
 Signature 

“I am the terror that flaps in the night”

Profile