Changeset 15

Show
Ignore:
Timestamp:
12/30/09 17:24:32 (2 years ago)
Author:
edsonk
Message:

#4 - Simples blog com CodeIgniter com validação de formulário

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • tutorial/framework/codeigniter/blog/segundo/system/application/config/config.php

    r14 r15  
    1212| 
    1313*/ 
    14 $config['base_url']     = "http://localhost/Tutorial/Blog/Tutorial 001/"; 
     14$config['base_url']     = "http://localhost/Tutorial/Blog/Tutorial 002/"; 
    1515 
    1616/* 
  • tutorial/framework/codeigniter/blog/segundo/system/application/controllers/blog.php

    r14 r15  
    22 
    33class Blog extends Controller 
    4 { 
     4{       
    55        function Blog() 
    66        { 
     
    88                $this->load->helper('url'); 
    99                $this->load->helper('form'); 
     10                $this->load->library('validation'); 
    1011                $this->load->scaffolding('entries'); 
    1112        } 
     
    2627                $this->db->where('entry_id', $this->uri->segment(3)); 
    2728                $data['query'] = $this->db->get('comments'); 
    28                                  
    29                 $this->load->view('comment_view', $data); 
     29                 
     30                $rules['body']          = "trim|required|xss_clean"; 
     31                $rules['author']        = "trim|required|max_length[100]"; 
     32                $this->validation->set_rules($rules); 
     33                 
     34                $this->validation->set_error_delimiters('<div class="error">', '</div>'); 
     35                if ($_POST) 
     36                { 
     37                        $fields['body']         = 'body'; 
     38                        $fields['author']       = 'author'; 
     39                } else { 
     40                        $fields['body']         = ''; 
     41                        $fields['author']       = ''; 
     42                } 
     43                $this->validation->set_fields($fields); 
     44                 
     45                if ($this->validation->run() == FALSE) 
     46                {        
     47                        $this->load->view('comment_view', $data); 
     48                } else { 
     49                        $this->comment_insert($_POST); 
     50                } 
    3051        } 
    3152         
    32         function comment_insert(
     53        function comment_insert($_POST
    3354        { 
    3455                $this->db->insert('comments', $_POST); 
  • tutorial/framework/codeigniter/blog/segundo/system/application/views/comment_view.php

    r14 r15  
    33<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    44<title><?php echo $title; ?></title> 
     5<style type="text/css" media="screen"> 
     6.error { 
     7        color: #FF0000; 
     8} 
     9</style> 
    510</head> 
    611<body> 
    712<h1><?php echo $heading; ?></h1> 
     13 
     14<?php echo $this->validation->error_string; ?> 
    815 
    916<?php if($query->num_rows() > 0) :?> 
     
    2229<p><?php echo anchor('blog', 'Back to Blog');?></p> 
    2330 
    24 <?php echo form_open('blog/comment_insert');?> 
     31<?php echo form_open('blog/comments/' . $this->uri->segment(3));?> 
    2532 
    2633<?php echo form_hidden('entry_id', $this->uri->segment(3));?> 
    2734 
    28 <p><textarea name="body" rows="10"></textarea></p> 
    29 <p><input type="text" name="author" /></p> 
     35<p>Body:<br /><?php echo $this->validation->body_error; ?><textarea name="body" rows="10"><?php echo $this->validation->body;?></textarea></p> 
     36<p>Author:<br /><?php echo $this->validation->author_error; ?><input type="text" name="author" value="<?php echo $this->validation->author;?>" /></p> 
    3037<p><input type="submit" value="Submit Comment" /></p> 
    3138