Welcome Guest, Not a member yet? Register   Sign In
CI form 'best practices'
#1

[eluser]Maglok[/eluser]
So when we make a form within a CI application we have several options. To use the Form Validation library, and the form helper.

I have been working with the helper for a while, but to me it generates a lot of code and not that much effect. It is very easy to populate a dropdown menu for instance. Doing it through basic HTML will create some serious code. It seems to mess up my MVC when I use it though. I get pieces of code like this:

Code:
$data['aantallen_settings'] = array(
                      'name' => 'aantallen',
                      'id' => 'aantallen',
                      'rows' => '10',
                      'cols' => '75',
                      'class' => 'editor'
                );

In the Controller. And the size and class of a input field is kind of a View thing. What is the best practice there?

I have heard a bunch of CI veterans say 'dont use the form helper to begin with', true?

Then take form validation. Should I put a rule on each field just so I can repopulate it? Is that the best practice? Sounds overly complicated for fields that are not interesting to check, like text areas in which I allow HTML.

On that subject: Is there no extended base of custom made validations, like date, out there?
#2

[eluser]Chicken's Egg[/eluser]
I Don't understand your first question, so I will start with the second one.

I don't see any reason to avoid the form helper. Yes, creating forms in html isn't that much work for an experienced html-writer, but the form_validation library works well together with form helper.

As for your third question. Yes, you should put a rule (remember: empty rules are rules too) on each field to get it repopulated when the form validates to false. For textareas you might consider using xss_clean and trim.

If you would like to validate file uploads too, you might also be interested in this extension of the library:
http://ellislab.com/forums/viewthread/123651/
But there are others (and more extensive ones) out there. However, if you don't feel the need for an extension, you could use a callback to check the validity of a date.
#3

[eluser]Chad Fulton[/eluser]
Here's a simple, possible solution:

Controller
Code:
// ...

$data['form_element'] = array(
    'name' => 'MyName',
    'id' => 'id'
);

$this->load->view('display_forms', $data);

display_forms View
Code:
// ...

form_textarea(array_merge($attributes, array(
    'rows' => '10',
    'cols' => '75',
    'class' => 'editor',
));
#4

[eluser]Maglok[/eluser]
First question clarification: MVC dictates we handle any view related stuff in the view. Stuff like the color of something, the class (for CSS), etc. Yet the array of attributes we give a field in the Controller includes stuff that is View related.

@Chicken's egg: Yet the validation class user_guide uses normal html to show you how to do it. Smile You got a point there though.

Putting an empty rule on a field kind of seems redundant to me. Sure I can do it, but it's a tad bit strange.

@chad Fulton: Yeah possible, but I wonder if there is not an easier way. Is that really best practice? To split it and then merge it in the view? It sounds a bit like a long workaround that only adds even more code and complexity.
#5

[eluser]skunkbad[/eluser]
You make it sounds like you're in an MVC prison! I'd hate to show you some of my code. I think there are ideals, and I try to have my forms in views. I try not to use the form helper, but I do use the form validation class. Form validation obviously goes in a controller. I have some language files that hold form related error messages and confirmation messages. To me (because I was coding purely procedural until about 6 months ago) I see there being a point where there are just too many files to make something simple happen.
#6

[eluser]Yash[/eluser]
Dude the best practice with CI is more you code more you get new way to code .... so chill enjoy CI
#7

[eluser]Maglok[/eluser]
Haha I am not in a 'prison' I just like to have my code clear and readable. The application expands rapidly and anything to make it clearer, MVC helps with that o course.
#8

[eluser]bretticus[/eluser]
Well, I'm a relatively new CI user. Perhaps I was used to doing code similar to Derek, but I have had no qualms using form helper within my views and particularity with dynamically-populated input/controls. I too think that having to make blank rules for inputs that I do not need to validate seems like bloat but considering some of my form validation classes from the past, I'm actually a little bit lighter with CI. I NEVER have html meta data in my controller that gets passed to my view (I'd be interested to see a case for that.) And if I have a commonly used validation, I ALWAYS extend the validation class as to include it in my pipe lists of rules.

Here's a recent example:
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/*** Brett: Thu Apr 23 16:48:00 GMT 2009
* Extension for adding abitrary error messages and extra rules ***/


class MY_Form_validation extends CI_Form_validation {
    
    function MY_Form_validation($rules=array())
    {
        parent::CI_Form_validation($rules);
    }
    
    function create_arbitrary_error($alias, $message) {
        // Save the error message
        $this->_field_data[$alias]['error'] = $message;
        if ( ! isset($this->_error_array[$alias]))
        {
            $this->_error_array[$alias] = $message;
        }
    }
    
    /**
     * lowercase
     *
     * @access    public
     * @param    string
     * @return    bool
     */    
    function lower($str)
    {
        return ( preg_match("/([A-Z])$/", $str)) ? FALSE : TRUE;
    }
    
    /**
     * lowercase
     *
     * @access    public
     * @param    string
     * @return    bool
     */    
    function phonenumber($str)
    {
        return ( preg_match("/^\(?[2-9][0-8][0-9]\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}$/", $str)) ? TRUE : FALSE;
    }
}




Theme © iAndrew 2016 - Forum software by © MyBB