Welcome Guest, Not a member yet? Register   Sign In
Initialization and Form Validation
#1

[eluser]Eric Cope[/eluser]
Here is the flow. I have data in the database. It needs editing. I want to populate a form, then let the user submit changes (if any), validate those changes and update the database.

How can I initialize $this->validation->formElementName?
Here is some pseudocode

Code:
if first load
    $this->validation->formElement1 = "someData";
else
    call validation sequence;

Any ideas or comments are appreciated.
#2

[eluser]Eric Cope[/eluser]
EDIT: This does not jive with set_select or the other validation functions.

So, I have a solution. Its not what I would call optimal, but it seems to work.
To start, I have a uri segment to indicate which row of database data to initialize the form with.
Code:
if(valid_uri($this->uri->segment(3)))
{
    $formData = $data_from_database;
    $this->load->view('...');
}
else if(form_validation_function == FALSE)
{
    $formData = $this->validation->{forms};
}
else
{
    update_database();
}

I use $formData as a 2:1 multiplexer between my initialization data and the validated data within the uri segment controlling the mux.

Anyone have something more elegant?
#3

[eluser]Philip Ramirez[/eluser]
I handle it similarly, except I use a field in the form (submit or anything else) to determine whether it should be initialized or not.

Here's some summarized code from a recent project:

Code:
public function edit($edit_id=null)
{
    if (empty($edit_id) || !is_int($edit_id))
    {
        redirect('somewhere');
    }
    
    if ($this->validation->run())
    {
        update_database();
        // set flashdata, then redirect
    }
    else
    {
        if (!$this->input->post('edit-submit'))
        {
            // first run, assuming <input name="edit-submit">...
            // do appropriate security checks to ensure access and all that jazz (most important part!)
            $edit_data = $this->blah_model->getWhere('id', $edit_id);
        
            foreach ($edit_data as $k => $v) // assuming keys match validation field names
            {
                $this->validation->$k = $v;
            }
        }
        $this->load->view('...');
    }
}

I'd also like to hear some nicer ways of handling this!
#4

[eluser]Eric Cope[/eluser]
I found a work around. I extended the Validation class.
Code:
function initializeFormElement($element,$val)
{
    $this->$element = $val;
    return TRUE; // I can add debug later  
}
This still does not improve the set_select, but at least I can use the $this->validation->element to set the areas without an intermediary variable.

Digging around the Validation Class, I found how the set_select function works which confuses me. For completeness, here it is:
Code:
function set_select($field = '', $val)
{
    if($field == '' OR $value == '' or !isset(_POST[$field]))
        return '';
    
    if($_POST[$field] == $val)
        return ' selected="selected"';  
}
Why are we comparing $_POST[$field]? That is an unclean variable. All clean variables are $this->validation->*. So, I made my own as below:
Code:
function set_select($field = '', $val)
{
    if($field == '' OR $value == '' or !isset($this->$field))
        return '';
    
    if($this->$field == $val)
        return ' selected="selected"';  
}
That makes more sense to me. Maybe one of the Ellis Lab folks can fill us in on the decision to code the set_select in that manner, but my way seems to make more sense. What do you (the community) think?
#5

[eluser]Yash[/eluser]
yea nice post.I'm also having this kind of problems.




Theme © iAndrew 2016 - Forum software by © MyBB