Welcome Guest, Not a member yet? Register   Sign In
Populating a form from a database then validating it
#1

[eluser]the future darlo manager[/eluser]
Trying to sort this out but struggling a little. I want to just use the one view to show the form which is causing me the problem. I want to query the databse, populate the form and then when the form is processed validate it and redisplay it if need be.

Any solutions... this is what I've got so far but can't get it working. Obvioulsy '$row = $article->row();' is causing the problem in the view but I'm stumped how to get round it.

Code:
function editarticle()
    {
        //Finds out if the user is logged in by checking for a session
        if ($this->session->userdata('userid') == TRUE)
        {
            
            //Gets the article id from the url
            $article_id = $this->uri->segment(3, NULL);
            
            //Loads the Articles model
            $this->load->model('Articles_model');
            
            //Loads the article query
            $data['article'] = $this->Articles_model->getArticle($article_id);
            
            //Checks to see if exactly one record was found
            if ($data['article']->num_rows() == 1)
            {
                //Tells the view not to show the validation error - it will be empty
                $data['show_validation'] = "N";
                    
                //Ensures all the data can be used by the views
                $this->load->vars($data);
                
                //Loads the views
                $this->load->view('inner_page_header');
                $this->load->view('sidebar');
                $this->load->view('edit_article_page');
                $this->load->view('footer');
                                
            } else {
                echo "Error - More than one article found";
            }
            
        }
        else
        {
            //Redirect the user to the login screen if they are not logged in
            redirect('myarea/login', 'refresh');
        }            
        
    }

///////////////////////////////////////////////////////////////////////////////////////////////////////

    function editarticleprocess()
    {
        //Sets validation rules
        $this->form_validation->set_rules('article_id', 'Article ID', 'trim|required|');
        $this->form_validation->set_rules('headline', 'Headline', 'trim|required|max_length[40]|xss_clean');
        
        //Runs the validation check
        if ($this->form_validation->run() == FALSE)
        {    
            //If the validation check fails reload the views for the login form
            //Tells the view to show the validation error
            $data['show_validation'] = "Y";
            
            //Sets a custom error message
            $data['custom_error_message'] = "<p>Please try again</p>";
            
            //Ensures all the data can be used by the views
            $this->load->vars($data);
            
            //Loads the views
            $this->load->view('inner_page_header');
            $this->load->view('sidebar');
            $this->load->view('edit_article_page');
            $this->load->view('footer');
        }
        
    }

Then the view...

Code:
&lt;?php
$row = $article->row();
?&gt;

<div id="container" class="page">  
    
<h1>Edit News</h1>
<p class="meta">&nbsp;</p>
    
    <p>From this form you can edit the news you have previously submited to the intranet home page for others to read. Please complete the form fully and hit the submit button. Remember you can also use (X)HTML in these text areas.</p>
    
    &lt;?
    if ($show_validation == "Y") {
        echo "<div class=\"flash\" id=\"error\">";
            echo validation_errors();
            echo $custom_error_message;
        echo "</div>";
    }
    ?&gt;
    
    &lt;? echo form_open('myarea/editarticleprocess');?&gt;

        &lt;input type="hidden" name="article_id" value="&lt;?php echo set_value('article_id'); ?&gt;" /&gt;
        <p><label>Headline:</label>&lt;input type="text" name="headline" value="&lt;?php echo set_value('headline'); ?&gt;" maxlength="40"&gt;&lt;/p>
        <p><label>Article Type:</label>&lt;input type="text" name="article_type_id" value="News" readonly="readonly" /&gt;&lt;/p>
        <p><label>Teaster Text:</label>&lt;textarea class="teaser_textarea" name="teaser_text"&gt;&lt;?php echo set_value('teaser_text'); ?&gt;&lt;/textarea&gt;&lt;/p>
        <p><label>Article Text:</label>&lt;textarea class="article_textarea" name="article_text"&gt;&lt;?php echo set_value('article_text'); ?&gt;&lt;/textarea&gt;&lt;/p>
        <p><label></label>&lt;input type="submit" name="edit_submit" class="button" value="Edit News" /&gt;&lt;/p>

    &lt;? echo form_close();?&gt;    

<p class="commentLink">
    &lt;? echo "<a >Log Out of My Area</a>" ?&gt; - This will end your current session in 'My Area'.
</p>

<div class="clearing">&nbsp;</div>

</div>&lt;!-- /container --&gt;
#2

[eluser]the future darlo manager[/eluser]
Anyone able to help?
#3

[eluser]xzela[/eluser]
can you model your model? more specifically the Articles_model ?
#4

[eluser]the future darlo manager[/eluser]
I've left it at work unfortunately so would have to do it tomorrow morning.

Its just a standard MySQL to get my article (SELECT * from articles WHERE article_id = $article_id LIMIT 1) type thing. That query is working as I use it to pull an article up with it elsewhere in my app and I can get it too populate the form here. Its when I submit it and need to redisplay it with validation that I'm stumped on as I have to use the validation variables.

I guess I could create two views (one for validation and the other for loading the original content into) but it seems a bit silly when I might be able to do it with on.
#5

[eluser]xzela[/eluser]
Hi,

So i have rewritten your code to what i usually do. This is just a personal preference. You can take it or modify it to your liking:

by the way i haven't tested any of this, but it should work.

Article_model
Code:
//Model stuff
class Articles_model extends Model {
    function __construct() {
        parent::Model();
        $this->load->database(); //load database library
    }
    
        //get article by id
    function getArticle($id) {
        $this->db->from('articles');
        $this->db->where('article_id', $id);
        $data = array();
        $query = $this->db->get();
                $data['num_rows'] = $query->num_rows(); //incase you want to do something with the num_rows value
        if ($query->num_rows() > 0) {
            foreach($query->result_array() as $row) {
                $data = $row;
            }
        }
        return $data;
    }
}

Controller_name
Code:
//Controller stuff
class Controller_name extends Controller {
    function __construct() {
        parent::Controller();
        $this->load->library('Authentication'); //custom authentication library you'll need to write one or just comment it out
    }
    /**
    * Edit a given article
    *  @param [int] $id = article id;
    * @return null;
    */
    function editarticle($id) {
        $this->authentication->checkLoggedInUser(); //this checks to see if the user is logged in;  optional, comment it out if not needed.
        $this->load->helper('form'); //load the form helper
        $this->load->library('form_validation'); //load the form_validation library
        $this->load->model('Articles_model'); //load the model;
        $data['article'] = $this->Articles_model->getArticle($id);
        //Sets validation rules and format
        $this->form_validation->set_error_delimiters('<div id="error" class="flash">', '</div>'); //load the error formatting here, not in the view
        $this->form_validation->set_rules('article_id', 'Article ID', 'trim|required|');
        $this->form_validation->set_rules('headline', 'Headline', 'trim|required|max_length[40]|xss_clean'); //load a call_back here
        if($this->form_validation->run() != true) { //changed the run() to test for 'is not true' because by default is will be false, just a personal choice
            $fields = array(); //load the columns into an array
            $fields['article_id'] = set_value('article_id');
            $fields['headline'] = set_value('headline');
            //..ect
            $this->Articles_model->updateArticle($id, $fields);
            redirect('some/other/page', 'refresh');
        }
        else {
            $this->load->view('inner_page_header');
            $this->load->view('sidebar');
            $this->load->view('edit_article_page', $data); //load the data into this view
            $this->load->view('footer');
        }
    }
}

View
Code:
<div id="container" class="page">    
    <h1>Edit News</h1>
    <p class="meta">&nbsp;</p>
    <p>From this form you can edit the news you have previously submited to the intranet home page for others to read. Please complete the form fully and hit the submit button. Remember you can also use (X)HTML in these text areas.</p>
    &lt;?
    //no need to check for $data['show_validation'] it will show the error messages it when it needs to
    echo validation_errors();
    //echo $custom_error_message; // this is not need, check out the call_back in the form_validation library to include custom messages
    ?&gt;
    &lt;? //echo form_open('myarea/editarticleprocess'); //this is no longer needed
        echo form_open('myarea/editarticle'); //loaded into one controller function, the second function is not needed
    ?&gt;
        &lt;input type="hidden" name="article_id" value="&lt;?php echo set_value('article_id'); ?&gt;" /&gt;
        <p><label>Headline:</label>&lt;input type="text" name="headline" value="&lt;?php echo set_value('headline'); ?&gt;" maxlength="40"&gt;&lt;/p>
        <p><label>Article Type:</label>&lt;input type="text" name="article_type_id" value="News" readonly="readonly" /&gt;&lt;/p>
        <p><label>Teaster Text:</label>&lt;textarea class="teaser_textarea" name="teaser_text"&gt;&lt;?php echo set_value('teaser_text'); ?&gt;&lt;/textarea&gt;&lt;/p>
        <p><label>Article Text:</label>&lt;textarea class="article_textarea" name="article_text"&gt;&lt;?php echo set_value('article_text'); ?&gt;&lt;/textarea&gt;&lt;/p>
        <p><label></label>&lt;input type="submit" name="edit_submit" class="button" value="Edit News" /&gt;&lt;/p>
    &lt;? echo form_close();?&gt;    
<p class="commentLink">&lt;? echo "<a >Log Out of My Area</a>" ?&gt; - This will end your current session in 'My Area'.</p>
<div class="clearing">&nbsp;</div>
</div>&lt;!-- /container --&gt;

Let me know if you have any questions. I'll try my best to explain stuff.

good luck on your site.
#6

[eluser]the future darlo manager[/eluser]
Thanks for the help. It was much appreciated. Managed to rework your idea along with the idea presented in the link here (http://ellislab.com/forums/viewthread/103419/) to get a fully working system using just one form.

Very happy indeed. Hopefully I can crack on with finishing the site now. Its actually already built without a framework but its become large enough now to justifty a move.




Theme © iAndrew 2016 - Forum software by © MyBB