Welcome Guest, Not a member yet? Register   Sign In
best practice with form,validation,error,success,action order
#1

[eluser]sikkle[/eluser]
Hi to this community again Smile

i just want to advise, this is maybe a stupid one but let's go.

We all know, a lot of our work consist to manage the input data, show form, error, validation etc. etc.

so i'm trying to figure what is barely the best practice if i give you this example :

You get a controller, we will call it MyLovelyController.php.

indeed we get the same structure as usual, constructor, index.

imagine this controller is just to do this :

show a form to be fill out by the user, let imagine like old good example, this describe a car.

So

field :

Model (select comin from db model indeed)
Color
Motorhp


So we have to show the form, do the validation, re-show the form with current data until the validation as been accepted And after show a little success bouton to do any action, maybe continu, maybe getback now whatever.


So we get back to our controller :

what is the best practice order, i mean


if data in form, do the validation ?


if validation fail
show error
reload view ?

Else

show the blank form ?

else again

show the success form ?



I know it's not the way to go, but this is a example of what i'm looking for.

someone could be kind enough to give a good explaination of barely best practice with form,validation,error,success,action order ?

Many thanks for this again Smile
#2

[eluser]BravoAlpha[/eluser]
I do something like this:
Code:
function whatever()
{
    // whatever

    if ($this->validation->run())
    {
        // process the validated input (e.g. database stuff)
        // redirect
    }

    // load view
}

However, you could also follow the example in the user guide:
Code:
function index()
{
    $this->load->library('validation');

    if ($this->validation->run() == FALSE)
    {
        $this->load->view('myform');
    }
    else
    {
        $this->load->view('formsuccess');
    }
}
#3

[eluser]sikkle[/eluser]
ok pretty simple at this stage, perfect Smile

if you add the fact, that you have to put this data in the database.

you will have to show a third view if you get an error ?

like, validation is OK, but mysql fail, so you didnt go to the formsuccess.

What will be the way to handle it ?

many thanks for your time.
#4

[eluser]Michael Wales[/eluser]
Controller (Class: Car, Function: add):
Code:
<?php
$this->load->library('validation');
$rules['model'] = 'trim|required';
$rules['color'] = 'trim|required';
$rules['hp'] = 'trim|required|numeric';
$this->validation->set_rules($rules);
$fields['model'] = 'model';
$fields['color'] = 'color';
$fields['hp'] = 'horsepower';
$this->validation->set_fields($fields);

if ($this->validation->run()) {
  $this->db->insert('cars', $_POST);
  if ($this->db->affected_rows() != 1) {
    show_error('We encountered an unexpected error adding your car, please use the back button and try again.');
    exit;
  }
  $this->load->view('car_added');
} else {
  $this->load->view('add_car');
}
?>

No Validation View (this means the validation failed or it's first load):
Code:
<body>
  <?php if (isset($this->validation->error_string)) { ?>
    <div class="error">&lt;?= $this->validation->error_string; ?&gt;</div>
  &lt;?php } ?&gt;
  &lt;?= form_open('car/add'); ?&gt;
  <label for="model">Model:</label>
    &lt;input type="text" name="model" id="model" value="&lt;?= $this-&gt;validation->model; ?&gt;" /><br />
  <label for="color">Color:</label>
    &lt;input type="text" name="color" id="color" value="&lt;?= $this-&gt;validation->color; ?&gt;" /><br />
  <label for="hp">Horsepower:</label>
    &lt;input type="text" name="hp" id="hp" value="&lt;?= $this-&gt;validation->hp; ?&gt;" /><br />
  &lt;input type="submit" value="Add Car" /&gt;
  &lt;?= form_close(); ?&gt;
&lt;/body&gt;

Good Validation View (means validation passed)
Code:
&lt;body&gt;
<strong>OMG yay - car was added</strong>
&lt;/body&gt;
#5

[eluser]Michael Wales[/eluser]
Just a quick over of the code above:

I use the Validation class and determine if the validation is good or not. If so, I try to add the information to the database. If that fails, then I'll just use the show_error function. I would normally move all of this to a model, which would return TRUE/FALSE as to whether the query performed correctly or not.

If it works, the view will be loaded (in this example, car_added).

If there is no validation, or the validation failed - it will load my form (in this example, add_car). Within that form I've added the code to display errors and pre-populate the form in case of an error.
#6

[eluser]sikkle[/eluser]
Code:
if ($this->validation->run()) {
  $this->db->insert('cars', $_POST);
  if ($this->db->affected_rows() != 1) {
    show_error('We encountered an unexpected error adding your car, please use the back button and try again.');
    exit;
  }
  $this->load->view('car_added');
} else {
  $this->load->view('add_car');
}
?&gt;


This is exaclty where i have to go, i'll add to this, if you want to manage better the fact that the operation doesnt work for the moment and try again later for the part of the show_error, we get any better way to handle this then show_error basicly?

Many thanks.
#7

[eluser]Michael Wales[/eluser]
You could do whatever you wanted - redirect to another controller, dump some data into a log file or the database, put a calendar up with the death dates of past presidents - whatever you program up there
#8

[eluser]sikkle[/eluser]
So i'm right to think i can build up a kind of :

Code:
if ($this->db->affected_rows() != 1) {
$data['mysql_error_number'] = "#118";
$this->load->view('mysql_error_management', $data);
}

And a view :

Code:
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;We're sorry, our server is drunk for now try later.&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
<h1>&lt;?php echo $mysql_error_number;?&gt;, please contact the administration with this error number if you see it again</h1><br><br>
&lt;input type="submit" name="submit" value="Get back to my panel control NOW!" /&gt;
&lt;/body&gt;
&lt;/html&gt;

Does it seem a good way to handle mysql error and be able to create a follow up error number chart for follow-up with user ?

Many thanks.
#9

[eluser]Michael Wales[/eluser]
From a possible to program stand-point, yeah - it's definitely feasible.

From a usability and user experience site, don't make your users do your work for you. I'd make a database table that dumped an error code, time of occurence, and the user's session info (user agent, variables, etc). Then just tell the user you experienced a problem, it's automagically been forwarded to the admins and they'll get it taken care of. Then offer some useful suggestions as to where they can go from that point.

After an error like this - your primary goal is to keep them on the site, most will just say forget it and jump ship.
#10

[eluser]Zeldinha[/eluser]
[quote author="walesmd" date="1187721485"]From a usability and user experience site, don't make your users do your work for you. I'd make a database table that dumped an error code, time of occurence, and the user's session info (user agent, variables, etc). [/quote]

Hehe, well, if there's a database problem you wouldn't be able to populate that table with the errors Wink But sending an automatic email with this info would do the trick, it's a very nice suggestion.

And I'm just being picky now (sorry), but I saw in some of the examples posted here the $_POST array thrown directly to the insert. I'd suggest to use the Validation to first clean the user input, and then use $this->input->post to call the variables. Never ever use the $_POST/$_GET values directly, it's a headache Smile

Regards,




Theme © iAndrew 2016 - Forum software by © MyBB