Welcome Guest, Not a member yet? Register   Sign In
Form_validation isn't using rules from config file (CI2)
#1

[eluser]kirkaracha[/eluser]
My form validation isn't using the rules in a config file in CodeIgniter 2. I'm accessing the form at /admin/people/add and /admin/people/edit/id and submitting to /admin/people/save. When I submit the add form it just reloads add without reporting any validation errors (my form views will display validation errors if $this->form_validation->_error_array is not empty; this is working on my login form). When I submit the edit form I get a 404 error at /admin/people/save even though that URI works with add.

application/config/form_validation.php:
Code:
<?php  if(!defined('BASEPATH')) exit('No direct script access allowed');

$config = array(
    'people/save' => array(
        array(
            'field' => 'first_name',
            'label' => 'first name',
            'rules' => 'trim|required'
        )
    ) // people/save
);

/* End of file form_validation.php */
/* Location: /application/config/form_validation.php */

application/controllers/admin/people.php:
Code:
public function add() {
    $fields = $this->db->list_fields('people');
    /* set_defaults makes an object with null values for the fields in the database */
    $person = $this->form_validation->set_defaults($fields);
    $data = array(
        'action' => 'add',
        'person' => $person,
        'button_text' => 'Add Person'
    );
    $data['page_type'] = 'form';
    $this->layouts->set_title('Add a Person');
    $this->layouts->view('people/add_edit_person_form',$data);
} // add

public function save(){
    if($this->form_validation->run('people/save') == FALSE){
        if(is_numeric($this->input->post('person_id'))){
            $this->edit();
        } else {
            $this->add();
        }
    } else {
        redirect('people'); // test to see if it's passing validation
    }
} // save

application/views/add_edit_person_form.php:
Code:
<?php
$attributes = array(
    'class' => 'block',
    'id' => 'add_edit_person'
);
echo form_open('admin/people/save');
?>
<div class="required&lt;?php echo form_error('first_name')?' error':'';?&gt;">
    <label for="first_name">First name:</label>
    &lt;input name="first_name" id="first_name" type="text" value="&lt;?php echo set_value('first_name',$person-&gt;first_name); ?&gt;" size="75" maxlength="255" />
</div>

&lt;input name="person_id" id="person_id" type="hidden" value="&lt;?php echo set_value('id',$person-&gt;id); ?&gt;" />
<button>&lt;?php echo $button_text; ?&gt;</button>

&lt;/form&gt;


Messages In This Thread
Form_validation isn't using rules from config file (CI2) - by El Forum - 01-16-2011, 07:32 PM



Theme © iAndrew 2016 - Forum software by © MyBB