Welcome Guest, Not a member yet? Register   Sign In
Ab_Wizard: base controller for creating multi-page forms
#1

[eluser]Jelmer[/eluser]
Base controller for creating a multi-page form. It will take care of:

- Creates form fields with default values or saved in session values
- Saves the user's input to a session or to a cookie
- Checks whether all required values up to the current page have been entered
- Will validate the input using form_validation
- But it won't create your views or save the contents to the database, that's up to you.

How it works

You define the pages and the questions through the functions $this->__add_page($pagename) and $this->__add_question($pagename, $question_name, $question_settings) in the class constructor.

- Here's a example controller

After that you'll need to add a view "wrapper" to your views directory that will load another view based on the value of $page_id and will give that view as it's data a variable called $questions. In the subview you can use the question names as variable names and simply add them as "<?php echo $username; ?>". Always submit the forms to the page itself.

- And a wrapper example
- A subview example

Installation

It requires my AugmentedCI helper because I won't take the time to rewrite this every time to make it public, you can get that helper here.
To use Ab_Wizard you'll need to add the file to an autoloaded location (if you're using a PHP autoloader) or add it using require/include before you start with "class Some_class extends Ab_Wizard {".

Overwriting default behavior

If you need a page that needs more then just the normal form you can overwrite the default page by adding a page like normal (example $this->__add_page(5)) and create a function page_5(). There still will be a check if all required fields up to that page have been filled out and you can still define fields for the page if you need required fields for that page that are checked on the following pages.

Here's the link:
http://hg.mijnpraktijk.com/ab_wizard/wiki/Home
#2

[eluser]7amza[/eluser]
a demo should be good .
and thanks for sharing.
#3

[eluser]Jelmer[/eluser]
It's a very abstract class (not just in the programming sense) and there's no graphical user interface with it (that's up to you). There's some examples within the wiki (linked above), I won't post any more then that. I might expand the documentation if I ever get round to it, but I probably won't post more examples then there are already on Bitbucket.

Also it's already commented pretty well within the file and it's not that long that it's impossible to take a quick look through the code to understand it.
#4

[eluser]Bainzy[/eluser]
seems like a very useful idea ! so thanks for sharing and the effor put into it. I may take a look myself as i do want split page registration forms ... thanks again !
#5

[eluser]codeninja[/eluser]
Trying to use this library but geting the following errors.

Fatal error: Access to undeclared static property: CI::$ci in /www/application/helpers/augmentedci_helper.php on line 36

I have the helper in my "helpers" folder and loading it in autoload.php (under config).

what else do i need to do? (running php5 already)
#6

[eluser]Jelmer[/eluser]
@codeninja

That's strange. The Ci::$ci property is declared on line 40 so I can't really imagine why it wouldn't work. You could try moving the contents of line 36:
Code:
Ci::$ci = CI_Base::get_instance();
To below the Ci class. But as far as I know and have experienced it's not problem to use a class in a file before it's been declared (though I've never used PHP 5.0/5.1, I moved immediately to 5.2 after 4).
#7

[eluser]codeninja[/eluser]
Cool got it working.

But I am having a hard time to figure out how exactly does this work.. i followed your example to setup my controller just like your example controller but then how does the view work and how does it know which views to load.. a bit more documentation would definitely help.

Thanks
#8

[eluser]Jelmer[/eluser]
It loads a default view (a wrapper in which you can include a header & footer) and passes to it a variable called $page_id which is an integer in my examples but can also be a string.

In the example wrapper the $page_id is changed to 'page_4' if only 4 was passed in this piece of code (it's unchanged when a string is passed):
Code:
if (is_numeric($page_id))
    $page_id = 'page_'.$page_id;
Ci::view('nvlf/'.$page_id, $questions);

There's also a variable $questions passed to the wrapper which contains the fields of the page already as HTML (filled in if the page was loaded and submitted/validated before). The $questions array is passed along to the subview. In the subview the $questions keys become the variable names (as always when you pass an array to a view). And are used like below:
Code:
<tr>
    <td>Your desired username</td>
    <td>&lt;?php echo $username; ?&gt;</td>
</tr>

I don't have the time to write full documentation I'm afraid, juggling 4 projects at the moment.
#9

[eluser]codeninja[/eluser]
Thanks got it working but still a few questions / comments:

1. How are the error messages displayed?
2. The label's for the questions are not passed along in the "$questions" variable. How do we handle that?
3. Is there a better way to handle the initialization? Can you explain a bit about that?
#10

[eluser]Jelmer[/eluser]
1. How are the error messages displayed?
It uses Form_validation, so you can display error messages like always using validation_errors().

2. The label’s for the questions are not passed along in the “$questions” variable. How do we handle that?
At this point they're not passed along, if you got a suggestion I'd happily include it.

3. Is there a better way to handle the initialization? Can you explain a bit about that
Better compared to what? Compared to how it currently works in the example?
What you're really doing is defining all questions as object variables. But defining them directly as object variables is very unclear and messy. By doing it this way you get a massive constructor but it's very clear what happens and you can be sure the 3-dimensional internal $__questions array is formatted correctly.




Theme © iAndrew 2016 - Forum software by © MyBB