Welcome Guest, Not a member yet? Register   Sign In
XHTML Presentation of froms
#1

[eluser]breaddes[/eluser]
I'd like to define my formfields (including html-type and attributes) in my controller and then go through them and output them in a common from view. I have a form view that has always the same structure, only the form and the fields are different.

How can I achieve that? Something with foreach?
#2

[eluser]zwippie[/eluser]
You could store all formfields in an array.

Controller:
Code:
$data['fields']['lastname'] = '<input type="text" value="Jones">';
$data['fields']['address'] = '<input type="text" value="Mainstreet">';
$this->load->view("form", $data);
View
Code:
<? foreach ($fields as $field) : ?>
    <?= $field; ?>
<? endforeach; ?>
#3

[eluser]breaddes[/eluser]
thanks for your reply. That's an idea, but can I combine that with the Validation or Form class? I validate those fields anyway. Of course I can now use the validation process as normal, but is there a way to combine it, to reduce code?
#4

[eluser]Phil Sturgeon[/eluser]
Code:
$data['fields']['lastname'] = '<input type="text" value="Jones">';
$rules['lastname'] = 'required|xss_clean;

$data['fields']['address'] = '<input type="text" value="Mainstreet">';
$rules['address'] = 'required|xss_clean;

$this->validation->set_rules($rules);

$this->load->view("form", $data);

If you are interested in setting fields too, I reccomend you set a language file with the same keynames, then do a FOREACH to loop through the rules array_keys() and grab the lang from the file and chuck it in a $fields array.
#5

[eluser]breaddes[/eluser]
Thanks, that's a great idea.
#6

[eluser]zwippie[/eluser]
edit: nvm Smile
#7

[eluser]Référencement Google[/eluser]
[quote author="thepyromaniac" date="1202496270"]If you are interested in setting fields too, I reccomend you set a language file with the same keynames, then do a FOREACH to loop through the rules array_keys() and grab the lang from the file and chuck it in a $fields array.[/quote]

I have difficulties imagining how to proceed exactly, do you have a small piece of code on how to make that exactly?
#8

[eluser]breaddes[/eluser]
ok, this is the way i am doing my form now. What do you think about it?

Controller
Code:
$data['headline'] = $this->lang->line('centers_add');
$data['action_url'] = 'centers/add';
$data['reset_url'] = 'centers';
$data['fields'] =& $fields;
$data['submit_val'] = $this->lang->line('add');
        
$fields['place'] = 'text';
$fields['address'] = 'text';
$fields['postcode'] = 'text';
$fields['city'] = 'text';
$fields['phone'] = 'text';
$fields['web'] = 'text';
        
$basic_rule = "htmlspecialchars|trim|required|min_length[2]";
        
$rules['name'] = "max_length[1]";
$rules['place'] = $rules['address'] = $rules['city'] = $basic_rule;
$rules['county'] = $rules['phone'] = $basic_rule;
$rules['postcode'] = $basic_rule."|numeric|max_length[5]";
$rules['web'] = "htmlspecialchars|trim|min_length[2]";
        
$this->validation->set_fields($fields);
$this->validation->set_rules($rules);
        
$this->validation->set_message('required', $this->lang->line('short'));
$this->validation->set_message('min_length', $this->lang->line('short'));
$this->validation->set_message('max_length', $this->lang->line('long'));
$this->validation->set_message('numeric', $this->lang->line('numeric'));

View
Code:
<form action="<?=base_url().$action_url?>" id="form" method="post">
        <fieldset>
            &lt;?php foreach($fields as $field => $type): ?&gt;
                &lt;?php $field_error = $field."_error"; ?&gt;
                
                <label>&lt;?=$this->lang->line($field)?&gt; <span class="error">&lt;?=$this->validation->$field_error?&gt;</span></label><br />
                
                &lt;?php switch($type):
                    default: ?&gt;
                    &lt;?php break; ?&gt;
                    &lt;?php case 'text': ?&gt;
                        &lt;input type="text" name="&lt;?=$field?&gt;" value="&lt;?=$this-&gt;validation->$field ?&gt;" /><br />
                    &lt;?php break; ?&gt;
                    &lt;?php case 'textarea': ?&gt;
                        &lt;textarea name="&lt;?=$field?&gt;"&gt;&lt;?=$this->validation->$field ?&gt;&lt;/textarea&gt;<br />
                &lt;?php endswitch ?&gt;
            &lt;?php endforeach ?&gt;

            <br />
            &lt;input type="submit" class="button" value="&lt;?=$submit_val?&gt;" /&gt;
        </fieldset>
    &lt;/form&gt;




Theme © iAndrew 2016 - Forum software by © MyBB