Welcome Guest, Not a member yet? Register   Sign In
[split] O-O Forms?
#1

(This post was last modified: 06-29-2016, 01:39 AM by ciadmin. Edit Reason: Trying to hijack announcement; and tl;dr )

Hello!

A suggestion for forms: You could make the form creation and validation more object oriented.
Like you could build in the forms like objects so it oculd be managged from the controller and default views could be rendered to elements.

Functions like I tough:

$form = new Form_model();
$form->setParameters('', array( 'class' => "form-horizontal" ), array( 'd_id' => $d_id ), true);
$form->addElement('checkbox', is_active', $is_active);
$form->addElement('input', 'name', $name, array(), array( 'class' => 'form-control' ));
$form->addElement('select', 'lang', $lang, explode(':', AVAILABLE_LANG), array( 'class' => 'form-control' ));
$form->addElement('file', 'background', $background, array(), array('class' => 'form-control' ));
$form->addElement('checkbox', 'remove_pic');

then you just need to echo the form out in the view.


I have created the form clas like this:

Code:
class Form_model extends CI_Model {

       public $action;

       public $attributes;

       public $hidden;

       public $multipart;

       private $elements = array();

       private $translation = array();

       public function setParameters($action = "", $attributes = array(), $hidden = array(), $multipart = false) {
           $this->action       = $action;
           $this->attributes   = $attributes;
           $this->hidden       = $hidden;
           $this->multipart    = $multipart;
       }

       public function addElement($type, $name, $default_value = "", $options = array(), $extras = array()) {
           $form_element     = new Formelement_model();
           $form_element->setParameters($type, $name, $default_value, $options, $extras);
           $this->elements[] = $form_element;
       }

       public function addTranslation($lang, $type, $name, $default_value = "", $options = array(), $extras = array()) {
           $form_element     = new Formelement_model();
           $form_element->setParameters($type, $name, $default_value, $options, $extras);
           if(!isset($this->translation[$lang])){
               $this->translation[$lang] = array();
           }
           $this->translation[$lang][] = $form_element;
       }

       public function getElements() {
           return $this->elements;
       }

       public function getTranslationCount() {
           return count($this->translation);
       }

       public function getTranslations($lang) {
           return (isset($this->translation[$lang]))? $this->translation[$lang] : false;
       }

       public function render($open = true) {
           if($open) {
               if ($this->multipart) {
                   return form_open_multipart($this->action, $this->attributes, $this->hidden);
               }
               else {
                   return form_open($this->action, $this->attributes, $this->hidden);
               }
           } else {
               return form_close();
           }
       }

And the formelement model like this:

Code:
class Formelement_model extends CI_Model {

       private $type;

       private $name;

       private $default_value;

       private $options;

       private $extras;

       public function setParameters($type, $name, $default_value = "", $options = array(), $extras = array()) {
           $this->type          = $type;
           $this->name          = $name;
           $this->default_value = $default_value;
           $this->options       = $options;
           $this->extras        = $extras;
       }

       public function render($modelname) {
           $value = set_value($this->name,$this->default_value);

           switch($this->type) {
               case 'select':
                   $this->load->view('form/select', array(
                       'name'      => $this->name,
                       'value'     => $value,
                       'options'   => $this->options,
                       'extra'     => $this->extras,
                       'modelname' => $modelname
                   ));
                   break;
               case 'multiselect':
                   $this->load->view('form/multiselect', array(
                       'name'      => $this->name,
                       'value'     => $value,
                       'options'   => $this->options,
                       'extra'     => $this->extras,
                       'modelname' => $modelname
                   ));
                   break;
               case 'file':
                   $this->load->view('form/file', array(
                       'name'      => $this->name,
                       'value'     => $value,
                       'extra'     => $this->extras,
                       'modelname' => $modelname
                   ));
                   break;
               case 'textarea':
                   $this->load->view('form/textarea', array(
                       'name'      => $this->name,
                       'value'     => $value,
                       'extra'     => $this->extras,
                       'modelname' => $modelname
                   ));
                   break;
               case 'input':
                   $this->load->view('form/input', array(
                       'name'      => $this->name,
                       'value'     => $value,
                       'extra'     => $this->extras,
                       'modelname' => $modelname
                   ));
                   break;
               case 'checkbox':
                   $this->load->view('form/checkbox', array(
                       'name'      => $this->name,
                       'value'     => $value,
                       'extra'     => $this->extras,
                       'modelname' => $modelname
                   ));
                   break;
               default:
                   $this->load->view('form/checkbox', array());
           }
       }


   }

Thanks for the possibility ! Smile Have a nice day.
Reply


Messages In This Thread
[split] O-O Forms? - by Riko - 06-29-2016, 12:29 AM
RE: [split] O-O Forms? - by PaulD - 06-29-2016, 05:19 AM
RE: [split] O-O Forms? - by kilishan - 06-29-2016, 06:11 AM
RE: [split] O-O Forms? - by cartalot - 06-29-2016, 01:42 PM
RE: [split] O-O Forms? - by ivantcholakov - 06-29-2016, 06:36 AM
RE: [split] O-O Forms? - by kilishan - 06-29-2016, 01:54 PM
RE: [split] O-O Forms? - by InsiteFX - 06-29-2016, 06:14 PM
RE: [split] O-O Forms? - by kilishan - 06-29-2016, 07:27 PM



Theme © iAndrew 2016 - Forum software by © MyBB