`
天梯梦
  • 浏览: 13651087 次
  • 性别: Icon_minigender_2
  • 来自: 洛杉矶
社区版块
存档分类
最新评论

codeigniter:简单创建表单验证 Simple Form Generation in CodeIgniter

阅读更多

If you are using CodeIgniter, as well as any other PHP framework, you may notice that building CRUD forms is one of the most bother and routine tasks. Probably 80% of general web applications uses CRUD (create/read/update/delete).

In CodeIgniter, you can use any of the form generation libraries .

One of my preferred libraries is Form Generation Library by Frank Michel (see @macigniter’s thread here ). It allows you to create clean XHTML forms with CodeIgniter. See demo here .

Using Form Generation Library

So, in order to use this library, you need to download it from here (library files only is enough if you don’t want the entire site structure).

 

Make sure to follow the installation steps, as shown here . That means unzipping the package, configuring config/form.php and moving config/form.php and libraries/form.php to the right directories.

Let’s assume we want to create a simple form for my Time tracking utility.

timetracker

This simple form has:

  • Project name text input
  • Description text input
  • Notes text area
  • Submit button

So, I created a Project controller class with an edit() function, and added the following method:

 

    function edit() 
    { 
        $this->load->library('form'); 
 
        $this->form->open('project/edit', 'project_edit_form') 
                    ->fieldset('Project') 
                    ->text('name', 'Project Name', 'max_length[40]') 
                    ->text('description', 'Description', 'max_length[40]') 
                    ->textarea('notes', 'Notes', 'trim', "Write your project notes here") 
                    ->indent(150) 
                    ->submit('Submit', 'sub') 
                    ->onsuccess('redirect', 'project/index') 
                    ->nobr(); 
 
        $this->data['form'] = $this->form->get(); 
        $this->data['errors'] = $this->form->errors; 
 
        $this->load->view('project/edit', $this->data ); 
    } 
 

 

Then, we need to create the view. For that purpose, I created the edit.php file under application/view/project folder.

 

<?php 
Header('Cache-Control: no-cache'); 
Header('Pragma: no-cache'); 
 
$this->load->view('header', $data); 
?> 
 
<h2
>
Project</h2>
 
 
<?php echo $form ?> 
 
<?php 
$this->load->view('footer', $data); 
?>
 

 

The important code line there is only <?php echo $form ?> since it displays the form we previously generated. However, the other pieces of code display a generic header and footer part, you can discard that for your tests.

just need to open your browser, for example at: http://localhost/project/edit to see the results.

By using this Form Generation library you can create advanced forms, too. For example you can create forms that use checkboxes, radio buttons, multiple lists, etc. I’d recommend to download the library including CI and user guide, from here and look for views/welcome.php example to see an advanced version.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics