Welcome Guest, Not a member yet? Register   Sign In
validation class with multiple pages
#1

[eluser]Unknown[/eluser]
hi, i am new to CodeIgniter.
i am using Validation class with 4pages forms like this

page1 -> page2 -> page3 -> page 4

Code:
$this->load->library('validation');

if ($this->validation->run() == FALSE) {
//show errors
} else {
//to next page
$this->page2();
}

function page2(){...}
function page3(){...}
function page4(){...}

it works fine with page1, but not page2 nor page3, page4
when i use redirect insteed of $this->page2(), validation works fine but $_POST[] returns "NULL";

Code:
redirect('page2', 'location');

is there any way i can use Validation class with multiple pages??

please help me!!
thanks
#2

[eluser]xwero[/eluser]
If you want to redirect your page you have to process the $_POST data before you redirect

Code:
$this->load->library('validation');

if ($this->validation->run() == FALSE) {
//show errors
} else {
// process $_POST page 1
//to next page
$this->page2();
}

You could create a function to check if the user can proceed to the next page or not

Code:
function page1(){
$_POST['page'] = 1;
$_POST['redirect'] = 2;
_nextpage($_POST);
}
function page2(){
$_POST['page'] = 2;
$_POST['redirect'] = 3;
_nextpage($_POST);
}
function page3(){
$_POST['page'] = 3;
$_POST['redirect'] = 4;
_nextpage($_POST);
}
function page4(){
$_POST['page'] = 4;
$_POST['redirect'] = 4;
_nextpage($_POST);
}

function _nextpage($post)
{
$this->load->library('validation');
// validation rules
switch($post['page'])
{

}

if ($this->validation->run() == FALSE) {
//show errors
switch($post['page'])
{

}
} else {
// process $_POST
switch($post['page'])
{

}
//to next page
redirect('page'.$post['redirect'], 'location');
}
}
it's a quick idea how you would get this working but it's far from finished, for instance it will be difficult to show the errors. Maybe the function should output an empty string or an error string which you check in the page functions and do the redirect there.
#3

[eluser]Michael Wales[/eluser]
I find the easiest way to do multiple page forms is treat them as completely seperate forms.

psuedo-code
Code:
// Page 1
validation shizzle
if (val->run()) {
  insert into db
  redirect to next page
} else {
  show errors
}

Code:
// Page 2
validation shizzle
if (val->run()) {
  update record from page 1
  redirect to next page
} else {
  show errors
}
#4

[eluser]Unknown[/eluser]
thanks xwero
thanks walesmd

so there is no way i can reset Validation class like,

Code:
$this->validation->run() = FALSE

i will use Database to save the Form data this time

thank you for your help!!




Theme © iAndrew 2016 - Forum software by © MyBB