Welcome Guest, Not a member yet? Register   Sign In
Templating App Forms
#1

[eluser]xtremer360[/eluser]
I'm trying to develop a series of forms with my template that I am using for my Codeigniter application. I am using the form helper to put together my forms. I would like to cut down on a lot of code that is similar on all forms by making some sort of function that will run when a form is to be created.

For example:

Code:
<?php echo form_open('register', array('id' => 'register_form')); ?>
    <div class="row">
        <div class="col-md-12 padding-none border-right">
            <div class="innerLR">
                <div class="form-group">
                    &lt;?php
                    $attributes = array('class' => 'strong');
                    echo form_label('Username', 'username', $attributes);
                    $data = array('class' => 'input-block-level form-control', 'name' => 'username', 'placeholder' => 'Your Username');
                    echo form_input($data);
                    ?&gt;
                </div>
                <div class="form-group">
                    &lt;?php
                    $attributes = array('class' => 'strong');
                    echo form_label('Email', 'email', $attributes);
                    $data = array('class' => 'input-block-level form-control', 'name' => 'email_address', 'placeholder' => 'Your Email Address');
                    echo form_input($data);
                    ?&gt;
                </div>
                <div class="form-group">
                    &lt;?php
                    $attributes = array('class' => 'strong');
                    echo form_label('Password', 'password', $attributes);
                    $data = array('class' => 'input-block-level form-control', 'name' => 'password', 'placeholder' => 'Your Password');
                    echo form_password($data);
                    ?&gt;
                </div>
                <button class="btn btn-block btn-inverse" type="submit">Submit</button>
            </div>
        </div>
    </div>
&lt;?php echo form_close(); ?&gt;

What I want to figure out is how I can use a helper function to help create my forms. All forms include the divs that has classes and and forms with form inputs and labels that share the same classes.

Can someone show me the right direction as to how this could be accomplished?
#2

[eluser]CroNiX[/eluser]
I don't see how you could simplify it much. Yes you could make a helper that outputs the divs with the label and field, but you have other variables. Like you'd have to tell it what kind of field since you are using regular "input" and "password" fields, and probably want others that you aren't showing like textarea, dropdowns, checkboxes, etc. So by the time you create a helper you'd probably have just as much code in order to pass the data to the helper, so I'm not sure it would be worth it. And you'd still also have to pass the fields attributes as they change for each input.

One thing I would generally advise is to not create a variable unless it is going to be used more than once. It can be argued that an exception would be for readability, like a very long array or something. When a variable is created, it takes memory to store that variable and value.

For example,
Code:
$attributes = array('class' => 'strong');
echo form_label('Password', 'password', $attributes);

You aren't using $attributes anywhere else. You are just creating it to pass it, which takes more memory than:
Code:
echo form_label('Password', 'password', array('class' => 'strong'));
which is just as readable and takes less lines of code as well.
#3

[eluser]xtremer360[/eluser]
This is true and I will fix this. The reason I bring this question up is in the event in the future if I have themes that have different class names attached to elements or what not so that I can have a form that has the same code base that can be used across different themes.
#4

[eluser]CroNiX[/eluser]
Well, you can probably narrow it down a little by doing something like
Helper
Code:
//$labelText = Label to display
//$labelFieldName is the corresponging field name of the form element this is the label for
//$formElement is the raw html element for the individual field.
function form_group($labelText, $labelFieldName, $formElement)
{
  $html = '<div class="form-group">';
  $html .= form_label($labelText, $labelFieldName, array('class' => 'strong'));
  $html .= $formElement;
  $html .= '</div>';
  return $html;
}

Use like:
Code:
//create the form element
$data = array('class' => 'input-block-level form-control', 'name' => 'password', 'placeholder' => 'Your Password');
//pass the label text and field name along with the field.
echo form_group('Password', 'password', form_password($data));

which would replace this:
Code:
<div class="form-group">
&lt;?php
$attributes = array('class' => 'strong');
echo form_label('Password', 'password', $attributes);
$data = array('class' => 'input-block-level form-control', 'name' => 'password', 'placeholder' => 'Your Password');
echo form_password($data);
?&gt;
</div>
But you'd still have to create the individual form control and pass it to the helper, since that can change so much from field to field. I imagine you might want to add an css class to the form element if it fails validation and this would be easier to do that by keeping it outside of the helper.




Theme © iAndrew 2016 - Forum software by © MyBB