Welcome Guest, Not a member yet? Register   Sign In
Validation class - checkbox [] workaround -- one small question
#1

[eluser]tinawina[/eluser]
Hello everyone -

I know there have been a lot of woes discussed re: the validation class and posting arrays. I've had my share! So, this a.m. I worked up my own workaround on some legacy code.

I have this all worked out except for one thing -- I can't get my validation error message to show up on the screen. Frankly since this is a small matter I'm ready to go live with this as is, but thought I'd run my code by the good folks on this forum to see if you can catch what I'm guessing is a small mis-step on my part.

I've got a tester up and running. I'm doing everything in the controller right now so just echoing form fields, etc., to the screen. Will split this up into a view form in the end. To contextualize this: we are a social policy research clearinghouse and so when people add research we ask them to let people know what audience the research is meant for.

Controller:
Code:
function form()    
{
    $rules['title'] = 'required|trim|prep_for_form|xss_clean';
    $rules['audience'] = 'required|callback__checkboxes';
    $this->validation->set_rules($rules);

    $fields['title']    = 'Title';
    $fields['audience']    = 'Audience';
    $this->validation->set_fields($fields);

    $this->validation->set_error_delimiters('<span class="error">', '</span>');

    if ($this->validation->run() == false)
    {
        $audience_items = array(
            'Advocates',
            'College/University Professors',
            'General Public',
            'Legislators/Legislative Aids',
            'Parents',
            'Policy Professionals',
            'Researchers',
            'Teachers-elementary',
            'Teachers-middle school',
            'Teachers-high school'
        );

        echo '&lt;form action="/tester/form" method="POST" accept-charset="utf-8"&gt;';
        echo 'Title: ' . $this->validation->title_error . '<br />&lt;input type="text" name="title" value="' . $this-&gt;validation->title . '">';

        if ($this->input->post('audience'))
        {
            $audience = '';
            foreach ($this->input->post('audience') AS $check)
            {
                $audience .= $check . '; ';
            }
            $audience = rtrim($audience, '; ');
        }
        else
        {
            $audience = '';
        }

        $content = '';
        foreach ($this->audience_items AS $aud)
        {
            if (ereg($aud, $audience))
            {
                $content .= '&lt;input type="checkbox" name="audience[]" value="' . $aud . '" checked /&gt;' . $aud . '<br />';
            }
            else
            {
                $content .= '&lt;input type="checkbox" name="audience[]" value="' . $aud . '" /&gt;' . $aud . '<br />';
            }
        }
        echo '<p>Choose at least one audience type: ' . $this->validation->audience_error . '<br />' . $aud_content . '</p>';

        echo '<p>&lt;input type="submit" name="submit" value="submit" /&gt;&lt;/form></p>';
    }
    else
    {
        // Made it through validation. The following just echo's data to the screen. Real app saves to a database.
        // We do just save a semi-colon separated string to the database which is why I'm mimicking that functionality above and here.

        echo '<p>' . $this->input->post('title') . '</p>';

        $audience_input = '';
        foreach ($this->input->post('audience') AS $audience)
        {
            $audience_input .= $audience . '; ';
        }
        $new_audience = rtrim($audience_input, '; ');
        echo $new_audience;
    }
}

Callback function:

Code:
function _checkboxes()
{
    if ($this->input->post('audience') == '')
        {
            $this->validation->set_message('_checkboxes', 'You must select at least one audience type.');
            return false;
        }
        else
        {
            return $this->input->post('audience');
        }
    }

I made a change to the Validation library which got rid of an irritating Notice-level error message re: array-to-string conversion (or vice versa -- can't remember):

Code:
function prep_for_form($data = '')
{
    if (is_array($data))
    {
        foreach ($data as $key => $val)
        {
            $data[$key] = $this->prep_for_form($val);
        }
        return $data; // <<< added this return
    }

    if ($this->_safe_form_data == FALSE OR $data == '')
    {
        return $data;
    }
    return str_replace(array("'", '"', '<', '>'), array("'", "&quot;", '&lt;', '&gt;'), stripslashes($data));
}


As I said, this is all working great -- the checkboxes repopulate as expected when I've chosen an audience. If I don't fill in "title" and do or do not check off audience types everything works as expected too. The only problem is: if I leave audience checkboxes unchecked the form is displayed again - but no error message is shown as defined in my callback for checkboxes. Why is this? When I don't fill in anything for "title" the error appears as expected. Any help is greatly appreciated. Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB