How to : Make a custom form and process/validate/update data

edited December 2010 in Modules
Hi folks, Hi David,

I'm here again to ask your help. I have to say first that i'm kinda new to this MVC stuff and to Fuel and Codeigniter.

My questions is the following :
- How can i make a form in my advanced module ?

Does something like that is the correct way ?
<?php
class Quiz extends CI_Controller {

	function __construct()
	{
		parent::__construct();
	}
	
	function create()
	{
	    $this->load->library('form_builder');
		
		$fields['language_id'] = array('type' => 'input', 'label' => 'Language Id', 'required' => TRUE);
		$fields['category_id'] = array('type' => 'input', 'label' => 'Category Id', 'required' => TRUE);
                $this->form_builder->set_fields($fields);
		
		 // will set the values of the fields if there is an error... must be after set_fields
		$this->form_builder->display_errors = TRUE;
		$this->form_builder->form_attrs = array('method' => 'get', 'action' => 'submit');
		$vars['form'] = $this->form_builder->render();
		$this->load->view('create_quiz', $vars);
	}
	
	function submit()
	{
		
	}
}
If correct and it seems so, i managed to display a form when i go to
http://localhost/fuelcms/quiz/create

My next question is how do i retrieve the infos now ?
I mean with what variable do i get back the values entered and how do I push them to the database ?

Thanks in advance

Lionel

Comments

  • edited 9:21AM
    Does the form map directly to a model? If so, if you are extending the base_module_model, you have access to a form_fields() method that you can modify to create your form fields, and you may not need to create a controller at all. If you haven't already, I'd recommend reading the following links that may help get you started:

    http://www.getfuelcms.com/user_guide/modules/tutorial
    http://www.getfuelcms.com/user_guide/modules/simple
    http://www.getfuelcms.com/user_guide/libraries/base_module_model
    http://www.getfuelcms.com/user_guide/libraries/my_model
  • edited 9:21AM
    Thanks again for your time... Really appreciated
    I've already read these several times but as i'm a beginner in learning mode ;)
  • edited 9:21AM
    Here is what i managed to do so far :

    I created a model for my quiz details
    <?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
    
    require_once(FUEL_PATH.'models/base_module_model.php');
    
    class Quiz_details_model extends Base_module_model {
    	
    	function __construct()
    	{
    		parent::__construct('fuel_quiz_details', QUIZ_FOLDER); // table name
    	}
    }
    
    class Quiz_detail_model extends Base_module_record {
    	
    }
    I can edit that in the admin backend.

    Now I wanted to make that form on my website for end-users
    Now I'm looking for examples using form_fields()

    I searched the blog code but all i could find was
    	function form_fields()
    	{
    		$fields = parent::form_fields();
    		$CI =& get_instance();
    		return $fields;
    	}
    And that doesn't talk to me much
  • edited 9:21AM
    Check out blog_comments_model.php for a more thorough form_fields() example as the functionality seems similar to what you're trying to achieve.

    I'm learning as I go too. I get confused between what's available/useable in the admin side vs public code.

    Seems it would be very easy for you to continue on your initial post track. Example below. If it was done that way is on_before_save() and the like still available David?



    Example. Not really taking advantage of Fuel though. Would make for a useable tut (imo) taking something like that and fuel-ifying it.

    <pre> <?php class Quiz extends CI_Controller { function __construct() { parent::__construct(); } function create() { $this->load->library('form_builder'); $fields['language_id'] = array('type' => 'input', 'label' => 'Language Id', 'required' => TRUE); $fields['category_id'] = array('type' => 'input', 'label' => 'Category Id', 'required' => TRUE); $this->form_builder->set_fields($fields); // will set the values of the fields if there is an error... must be after set_fields $this->form_builder->display_errors = TRUE; $this->form_builder->form_attrs = array('method' => 'get', 'action' => site_url('quiz/submit')); $vars['form'] = $this->form_builder->render(); $this->load->view('create_quiz', $vars); } function submit() { $insert['lang_id'] = $this->input->post('language_id'); $this->db->insert('table_name', $insert); } } </pre>
  • edited December 2010
    I think you are on the right track. If you are wanting the information saved to the quiz model, you could modify the Quiz_details_model form_fields() method. That method should return an array that form_builder can use to generate the form:
    http://www.getfuelcms.com/user_guide/libraries/form_builder

    For example, the fuel/modules/blog/models/blog_posts_model.php has a pretty advanced example of what you can do, but just remember, all you are trying to do is create that array that can be handed back to form_builder.

    For your controller, you could then load your model and grab the fields array from it's form_fields() method like below:
    
    ...
    $fields = $this->quiz_details_model->form_fields();
    ...
    
    You can also add your validation to the model by specifying the required fields property or using the add_validation() method on the model. Validation is run on save so if it doesn't validate, the save method on the model will return FALSE. For example on your create method, you could add something like:
    
    ...
    if (!empty($_POST))
    {
        
         if ($this->quiz_details_model->save($_POST))
         {
              // ...put your extra processing code here like a redirect to your thank you page
         }
         else
         {
              //... put your error information here
         }
    
    }
    ...
    
    Lastly, to Lance's point, you still have access to all the model hooks (e.g. on_before_save()) for you to do any extra cleaning or validating etc. I hope that helps clarify some.
  • edited 9:21AM
    Thanks again, i'm going to study your post.
  • edited 9:21AM
    To validate my data, should I use add_validation() or am I supposed to use the validator class ?
    http://www.getfuelcms.com/user_guide/libraries/validator
  • edited December 2010
    I tried to use add_validation() and i managed to make it work.

    With that done, how am i supposed to know what validation step failed and how can I display back the form with error messages ?

    Is there something special to be in the view for error messages to appear ?
  • edited December 2010
    I think I have my answer but please confirm

    To get the list of errors back you must use :
    $vars['errors'] = $this->quiz_details_model->get_errors();

    Is that correct ?

    Or should i use :
    $vars['error'] = $this->quiz_details_model->get_errors();
    $vars['notifications'] = $this->load->view('_blocks/notifications', $vars, TRUE);
  • edited 9:21AM
    That is correct. Either should work but it requires you to have a notification block created in your application folder or I suppose you could load the notification block from the fuel module like so:
    $this->load->module_view(FUEL_FOLDER, '_blocks/notifications, $vars, TRUE);
Sign In or Register to comment.