Welcome Guest, Not a member yet? Register   Sign In
form validation question
#6

[eluser]cideveloper[/eluser]
Your view looks fine. Nothing messy about it.

Where did you move the validation rules to now? A config file? That is a good way to do it. Cleans up the controller. However if you dont validate the dropdown then it wont re-populate the dropdown on validation error. even if you just do a 'trim|required'.

What SitesByJoe is saying is that it is common practice to redirect after a post submit. That way the user cannot hit refresh or F5 and then be prompted to submit the form again and then do a double submit. Redirect the user to the page that was before the page that they write the blog post. On that page put a notification bar at the top with

Code:
<?=$this->session->flashdata('message');?>

Controller

Code:
public function write () {
        if ($this->form_validation->run('write_blog_post') === FALSE)
        {
        $data = array(
            'categories' => $this->blog_model->get_categories(),
            'message' => (validation_errors()) ? validation_errors() : $this->session->flashdata('message')
        );
        $this->_view('admin/blog_write_post', $data);
        }
        else
        {
        $result_id = $this->blog_model->insert_post()
        if ($result_id){
            $this->session->set_flashdata('message', 'Blog Entry Submitted');        
            redirect('admin/previous_page');
        } else {
            $this->session->set_flashdata('message', 'An Error Occured Submitting the Post');
            redirect('admin/blog/write');
        }
        }
    }

Model
Code:
public function insert_post() {
        $data = array(
            'title'        => $this->input->post('title'),
            'content'    => $this->input->post('content'),
            'user_id'    => '1',
            'posted'    => date('Y-m-d H:i:s'),
            'category'    => $this->input->post('category')
        );
        $this->db->insert('blog_posts', $data);
        if ($this->db->affected_rows()>0){
            return TRUE;
        } else {
            return FALSE;
        }
    }

View
Code:
<h3>Write a blog post</h3>
&lt;?php echo $message;?&gt;
&lt;?php
$data = array(
    'id' => 'write_post'
);
echo form_open('admin/blog/write', $data);
$data = array(
    'name' => 'title',
    'placeholder' => 'Title',
    'value' => set_value('title')
);
echo form_input($data);
$data = array(
    'name' => 'content',
    'placeholder' => 'Content',
    'value' => set_value('content')
);
echo form_textarea($data);
echo form_label('Category', 'category');
echo form_dropdown('category', $categories, set_value('category'));
echo form_submit('submit', 'Submit Post');
?&gt;

This is very subjective but I find once you have the pages set up properly it simplifies coding a lot

and for you order_by try this

Code:
$this->db->order_by("default", "desc");
$this->db->order_by("name", "asc");

P.S. Nothing is "stupid" We are all always learning and finding better ways to do things


Messages In This Thread
form validation question - by El Forum - 06-09-2011, 01:40 AM
form validation question - by El Forum - 06-09-2011, 09:09 AM
form validation question - by El Forum - 06-09-2011, 09:26 PM
form validation question - by El Forum - 06-09-2011, 10:48 PM
form validation question - by El Forum - 06-10-2011, 01:47 AM
form validation question - by El Forum - 06-10-2011, 08:19 AM
form validation question - by El Forum - 06-13-2011, 02:06 AM
form validation question - by El Forum - 06-13-2011, 07:35 AM
form validation question - by El Forum - 06-13-2011, 11:22 AM
form validation question - by El Forum - 06-14-2011, 01:14 AM
form validation question - by El Forum - 06-14-2011, 01:33 AM
form validation question - by El Forum - 06-15-2011, 09:11 AM
form validation question - by El Forum - 06-15-2011, 10:23 AM
form validation question - by El Forum - 06-16-2011, 03:09 AM



Theme © iAndrew 2016 - Forum software by © MyBB