开发者

Form_validation isn’t using rules from config file (CodeIgniter 2)

开发者 https://www.devze.com 2023-02-05 08:57 出处:网络
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

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 when I initially go to the edit page. I'm loading the form validation library in the constructor.

application/config/form_validation.php:

<?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:

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(
开发者_Go百科        '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:

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

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

</form> 


After you've defined your $config array in application/config/form_validation.php, you'll need to call the following function in order to set the rules:

$this->form_validation->set_rules($config);

Reference: Setting Rules Using an Array

0

精彩评论

暂无评论...
验证码 换一张
取 消