Part of the EllisLab Network
   
 
Form validation & default values
Posted: 23 May 2008 06:27 PM   [ Ignore ]  
Summer Student
Avatar
Total Posts:  21
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 08:18 PM   [ Ignore ]   [ # 1 ]  
Lab Assistant
RankRank
Total Posts:  239
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 Authentication 2 Beta

Redux Authentication is a basic authentication package for CodeIgniter that helps you setup a membership system in minutes, even faster if you use our pre made package which has all the views and controllers set up for you.

Profile
 
 
Posted: 23 May 2008 08:27 PM   [ Ignore ]   [ # 2 ]  
Summer Student
Avatar
Total Posts:  21
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 09:38 PM   [ Ignore ]   [ # 3 ]  
Summer Student
Avatar
Total Posts:  21
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 12:58 AM   [ Ignore ]   [ # 4 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  786
Joined  02-05-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
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 719, on June 06, 2008 10:16 AM
Total Registered Members: 77571 Total Logged-in Users: 20
Total Topics: 101562 Total Anonymous Users: 2
Total Replies: 544409 Total Guests: 176
Total Posts: 645971    
Members ( View Memberlist )
Newest Members:  Idril616tonybernardcarterstarksColeJLinskitnealsemperjrawhallshiusbozzlynobluff