• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using the Validation Library

#1
[eluser]Walt Kirspel[/eluser]
I just want to see if I am in the ballpark here.

I've figured out how the rules work. And how set_fields works to repopulate the post data.

My question refers to how I best provide default data for the fields the first time around.
Code:
$data['title']            = 'Setup';
              
$rules['sourcepath']    = "required";
$rules['destpath']        = "required";
$this->validation->set_error_delimiters('<div class="form_errors">','</div>');
$this->validation->set_rules($rules);
            
$fields['sourcepath']    =    "Source Path";
$fields['destpath']        =    "Destination Path";
$this->validation->set_fields($fields);

if (!$this->validation->sourcepath) {
    $this->validation->sourcepath = 'c:/webserver/htdocs/codeigniter';
}
            
if ($this->validation->run() == FALSE) {
    $this->load->view('main', $data);        
} else {
        echo $_POST['sourcepath'].br();
    echo $_POST['destpath'].br();
}

I pass the $data variable into the form for things like titles and stuff. But for the input data in the form I use $this->validation->sourcepath and $this->validation->destpath.

My question is really in regards to how I handle the default data I want populated the first time through. What I did was check the $this->validation->sourcepath field after I had executed set_fields. Because I was assuming that if set_fields didn't have anything to populate from the post I could drop the default data into the fields.

But I also realize that although it semi "works", if the user has cleared the field it will still look to me as the first pass thru. So I'm not sure I've handled it the best way possible.

Is there some fairly "standard" approach folks utilize?

Thanks.

Walt

#2
[eluser]Colin Williams[/eluser]
I think one thing CI is certainly lacking is a handler for this exact usage case. This comes up on every project, and I usually come up with a more clever way to handle it each time. I know someone has already extended the Validation library to handle this, I think with something like $this->validation->set_defaults($defaults) [.. just an example of how I think it's implemented.]

Try searching this forum or the Wiki. I know it's been done.

#3
[eluser]thinkigniter[/eluser]
This may help!

#4
[eluser]Dagobert Renouf[/eluser]
Your post is great cjunky, however I don't think this is what Walt is looking for.

Here's what Colin is talking about : http://ellislab.com/forums/viewthread/47337/#228374

#5
[eluser]Walt Kirspel[/eluser]
[quote author="Dagobert Renouf" date="1214331918"]Your post is great cjunky, however I don't think this is what Walt is looking for.

Here's what Colin is talking about : http://ellislab.com/forums/viewthread/47337/#228374[/quote]

Thanks. Helps a lot!


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.