• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
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;

#2
[eluser]InsiteFX[/eluser]
Try this: Also make sure you are loading the Form_Validation libray!

Code:
public function save()
{
    // you do not need to pass in people/save it knows it!
    if ($this->form_validation->run() == 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

InsiteFX

#3
[eluser]kirkaracha[/eluser]
I get the same results. I'm loading the form validation library in the constructor:
Code:
public function __construct() {
    parent::__construct();
    $this->load->model(array('People_model'));
    $this->load->helper(array('date','people'));
    $this->load->library('Form_validation');
}

#4
[eluser]Cristian Gilè[/eluser]
Quote: } else {
redirect('people'); // test to see if it's passing validation
}

Do you want to redirect in the admin area or outside? It should be:

Code:
} else {
        redirect('admin/people'); // test to see if it's passing validation
    }

to redirect in the admin area.


Cristian Gilè

#5
[eluser]kirkaracha[/eluser]
Where it's redirecting isn't the problem. The problem is I submit the add form without a required field and it isn't being caught by the validation. The same validation rules work if I include them in my controller.
Code:
// added to save function above:
$config = $this->_add_edit_person_form();
$this->form_validation->set_rules($config);

private function _add_edit_person_form(){
    $config = array(
        array(
        'field' => 'first_name',
        'label' => 'first name',
        'rules' => 'trim|required'
        )
    );
    return $config;
} // _add_edit_person_form

#6
[eluser]Cristian Gilè[/eluser]
The array index should be admin/people/save but....
https://bitbucket.org/ellislab/codeignit...ocate-rule



Cristian Gilè

#7
[eluser]Chimpski[/eluser]
I was able to get it to work by using in the controller:

$this->form_validation->set_rules($this->config->item('validation_rule_name', 'form_validation'));


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.