• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] O-O Forms?

#1
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

#2
Not sure that is such a good idea. Why would I create the HTML elements in my controller? What if I want my form split over 3 columns? Of if I want some of the elements conditional on other data? Surely all this belongs in the view as it currently is.
Reply

#3
I see these types of things a lot, honestly. Every time that I use them I find that work for the simplest, straight-forward cases, but always become a frustration whenever you need to do anything outside of the initial design. For example, what if need to switch from Bootstrap to Foundation, so my div.form-groups are now un-needed, but there's different wrappers elsewhere. Or, how to have it style correctly between a horizontal and vertical form which, depending on the CSS framework used have different wrapping markup around them.

To me, they are only problematic and requires lot of care and design to take care of enough edge cases to make it useful, while just complicating the code. So me - not a fan.

Then again, I don't see a point in the HTML helpers in general, honestly... I've only ever heard one valid reason (switching CSS frameworks and writing a new class to modify the markup) and that's one that is still a waste of time, unless you've got 1000's of views. So - I might not be the target audience. Smile
Support Development  • Practical CodeIgniter 3  •
Myth:AuthVulcan - CLI Tools for CI4
Reply

#4
I've proposed once (2011) something similar, based on PEAR's package Quick_Form. Our designer got horrified of the implementation and refused to have something to do with it. And she was right. Better give the designers straight HTML.
Reply

#5
(06-29-2016, 06:11 AM)kilishan Wrote: Then again, I don't see a point in the HTML helpers in general, honestly... I've only ever heard one valid reason (switching CSS frameworks and writing a new class to modify the markup) and that's one that is still a waste of time, unless you've got 1000's of views. So - I might not be the target audience. Smile

Do you mean like the codeigniter form helper? The point of the form helper is so you don't have to mix html code and php code just to make a form field. And it becomes even more helpful when using a css framework like bootstrap because you can easily put / modify the bootstrap code in the form helper. Its a huge timesaver when making large forms. It could be improved by making it a clearer part of codeigniter form validation and set_value().

Otherwise, theres many different ways of creating form fields in a model. Especially with forms that have 3 different potential set values - initially from the database, from a failed form submission, and from the successful database update. I'm continually shocked at how people dismiss new ideas out of hand. I figured out a better way to create form fields in a model because i couldn't give a good answer to someones question on this forum. I realized from that question there had to be a different solution then what i was doing. There is no "best" solution but just consider that some of us are working with limited budgets and no designers helping with the html. So yes if we can figure out a faster way to build huge complex forms that also include the bootstrap css code we need - without having to code any html by hand to do it - its extremely valuable.
Reply

#6
(06-29-2016, 01:42 PM)cartalot Wrote:
(06-29-2016, 06:11 AM)kilishan Wrote: Then again, I don't see a point in the HTML helpers in general, honestly... I've only ever heard one valid reason (switching CSS frameworks and writing a new class to modify the markup) and that's one that is still a waste of time, unless you've got 1000's of views. So - I might not be the target audience. Smile

Do you mean like the codeigniter form helper? The point of the form helper is so you don't have to mix html code and php code just to make a form field. And it becomes even more helpful when using a css framework like bootstrap because you can easily put / modify the bootstrap code in the form helper. Its a huge timesaver when making large forms. It could be improved by making it a clearer part of codeigniter form validation and set_value().

Otherwise, theres many different ways of creating form fields in a model. Especially with forms that have 3 different potential set values - initially from the database, from a failed form submission, and from the successful database update. I'm continually shocked at how people dismiss new ideas out of hand. I figured out a better way to create form fields in a model because i couldn't give a good answer to someones question on this forum. I realized from that question there had to be a different solution then what i was doing. There is no "best" solution but just consider that some of us are working with limited budgets and no designers helping with the html. So yes if we can figure out a faster way to build huge complex forms that also include the bootstrap css code we need - without having to code any html by hand to do it - its extremely valuable.

Not dismissing it out of hand because it's a new idea. I've used these before and I've always ran into issues with them, honestly. Maybe I just like too much control over my HTML, I don't know, but I've done a number of pretty large/complex projects over the years where I was pretty much the sole developer. As for the form helper, it doesn't seem to save much typing over over a standard HTML form. And you can make things a lot faster by taking advantage of your IDE, if you use one, or something TextExpander if you don't want the repetition.

Quote:without having to code any html by hand to do it


And why is this a bad thing?

I know everyone has different preferences. I was just saying mine. I prefer my HTML as HTML, my Javascript as javascript, quite often my SQL is raw, also. Partially due to control, partially because I can look at it and immediately know what's going on, and partially because I've butted heads with too many edge cases over the years to really enjoy finding the edges of the abstractions.

Now, that doesn't mean tools like that won't be in CI4 because I know there are people that use them. It's just not for me.
Support Development  • Practical CodeIgniter 3  •
Myth:AuthVulcan - CLI Tools for CI4
Reply

#7
With the form_helper most people only used the form_open all the rest was pure html form code even using jQuery form validation

no point to this
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#8
Hey Riko - Sorry if I came across brusk earlier. First off - welcome to the forums! While that form of library isn't the type of thing that I prefer, I know there are many others that would find it very useful. You should consider making it available on GitHub and telling people about it in the Addins forum. I think you'll get some interest from it there.

I have to be honest - the last couple of weeks during the day job, I've been banging my head to make a set of libraries that do similar things in the OctoberCMS platform do things they weren't meant to do. Every time I turn around thinking, yeah, that's a 5-minute job, and takes me 1/2-hour to figure out what special way they want me to do it instead of just letting me code it. So, I think my frustrations bled into my earlier answers and I started sounding like a grumpy old man waving his cane in the air and telling kids to get off his lawn. Smile Sorry about that.
Support Development  • Practical CodeIgniter 3  •
Myth:AuthVulcan - CLI Tools for CI4
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.