Welcome Guest, Not a member yet? Register   Sign In
Validate an edit form
#1

[eluser]EEssam[/eluser]
Hi,

I need to validate an edit form, the logic as follow:

1. Load the data into text boxes from the database.

2. If the user submit the form with an empty field(s), the form show its details exactly as it was submitted.

I was able to use the Validation class for adding records, but the thing is how can I populate the form before the it is submitted from the database and then populate it using the validation class after first submission?

Please help.
#2

[eluser]EEssam[/eluser]
Is that not feasible? >:-(
#3

[eluser]bavander[/eluser]
Hi,

I populate the validation values from the database if the form is initially loaded to edit (e.g. nothing was posted) so they show up in the inputs. Then when the form is posted, the values come from the validator, so any changes the user made are reflected.

Not sure if this is a recommended approach, but has worked for me.

Cheers,

Bruce
#4

[eluser]EEssam[/eluser]
Thanks Bruce but do you have two views to handle this? If not, how do your edit view looks?
#5

[eluser]nanda[/eluser]
Here's the solution I use for my edit forms, it seems to work pretty good so far and does what I think you are asking for (without using 2 views).

Code:
<?php
if (!$this->validation->name && isset($event->name)) {
echo $event->name;
} else {
echo $this->validation->name;
}
?>

This checks if the validation variable is set first (if it is then the form must have been submitted, so this picks up changes), if it hasn't it takes whatever was stored in the database.

I normally have the code all on one line, but I've broken it up below to be easier to read. I pass a $data array in the controller to the view, the mysql result for my event data is stored in $data['event'] so you get the $event object as referenced below.

I'm probably going to put this in a plugin or helper for my own use as I like cleaner code in my views.
#6

[eluser]ontguy[/eluser]
Here is another approach:

in your controller:
Code:
//if the form has not been submitted
if (empty($_POST))
{      
  $row = $this->catalog_lib->get_category($this->data->id);

  //validated fields
  $this->validation->name = $row->name;
  $this->validation->desc= $row->desc;
}

in your view:
Code:
<?=$this->validation->name ?>
#7

[eluser]EEssam[/eluser]
Thanks guys, I'll use ontguy solution.
#8

[eluser]lostsock[/eluser]
Update: Found the solution elsewhere, http://ellislab.com/forums/viewthread/69084/P15/#452197


Hey ontguy, I'm looking to use a solution like this in my app. Unfortunately the host I'm with at the moment only has PHP 4 and the solution you have offered doesn't seem to work.

It seems to let me set $this->validation->name = "Anything" in the controller and I can even reference it later in the controller but when I echo $this->validation->name in the view that is loaded I get the following error:

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: name

Filename: views/test.php

Line Number: 5

Here is the code:

controllers/test.php
Code:
function test() {
    
    // Set validation rules
    $rules['name'] = "trim|required";
    $this->validation->set_rules($rules);
    
    // Set validation fields
    $fields['name'] = 'Name';
    $this->validation->set_fields($fields);
    
    if (empty($_POST)) {      
        $this->validation->name = "Test name";
    }

    if ($this->validation->run() == FALSE) {
        // FAILED OR NOT LOADED YET
        $this->load->view('test');
    } else {
        // PASSED
    }
}
views/test.php
Code:
<?php
echo $this->validation->name;
?>


Any thought would be very much appreciated.
#9

[eluser]m4rw3r[/eluser]
The new form_validation class (in SVN 1.7) has some neat functions for this.
#10

[eluser]Phil_B[/eluser]
I usually use this approach.

Controller
Code:
function test($id=0)
{
  $this->validation->set_fields(array(
    'txtFirstName'=>'First name',
    'txtLastName'=>'Last name'
  ));

  if ($_SERVER['REQUEST_METHOD']=='POST')
  {
    $this->validation->set_rules(array(
      'txtFirstName'=>'trim|required',
      'txtLastName'=>'trim|required'
    ));

    if ($this->validation->run())
    {
      $this->somemodel->save($id);
      redirect('elsewhere');
    }
  }
  else
  {
    $data = $this->somemodel->get($id);

    $this->validation->txtFirstName = $data->first_name;
    $this->validation->txtLastName = $data->last_name;
  }

  $this->load->view('someview');
}

View
Code:
<form method="post" action="<?=$_SERVER['REQUEST_URI'];?>">

<p><label>First name</label>
&lt;input type="text" name="txtFirstName" value="&lt;?=$this-&gt;validation->txtFirstName;?&gt;" /></p>

<p><label>Last name</label>
&lt;input type="text" name="txtLastName" value="&lt;?=$this-&gt;validation->txtLastName;?&gt;" /></p>

&lt;/form&gt;

Works well.




Theme © iAndrew 2016 - Forum software by © MyBB