I'm using CodeIgniter! I have 3 radio buttons - to choose role. If you choose one of the three radio buttons, you see different dropdowns. I want to make validation rules - to echo validation errors only on these dropdowns for the radio button you have chosen. I tried with this but it doesn't show me any validation errors.
First, my radio buttons are:
<input type="radio" name="role_id[]" onClick='showHide(this, true)' id="radio1" value="1" />
$data=array(
'name' => 'role_id[]',
'value' => '2',
'id' => 'radio2',
'onclick' => 'showHide(this, true)'
);
echo form_radio($data);
$data=array(
'name' => 'role_id[]',
'value' => '5',
'id' => 'radio5',
'onclick' => 'teachers_show(this, true)'
);
echo form_radio($data);
My controller is:
public function register()
{
if ($this->input->post('role_id[]') === 1){
$this->form_validation->set_rules('first_name', First name', 'trim|required');
$this->form_validation->set_rules('last_name', 'Last name', 'trim|required');
$this->form_validation->set_rules('username', 'username', 'trim|required|min_length[6]|max_length[12]|is_unique[users.username]');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[6]');
$this->form_validation->set_rules('password2', 'Confirm password', 'trim|required|matches[password]');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|is_unique[users.email]');
$this->form_validation->set_rules('location', 'Location', 'trim|required');
$this->form_validation->set_rules('school[]', 'School', 'required');
$this->form_validation->set_rules('class[]', 'Class', 'required');
$this->form_validation->set_rules('role_id[]', 'Role', 'required');
$this->form_validation->set_rules('class_divisions[]', 'Class division', 'required');
$this->form_validation->set_rules('region', 'Region', 'required');
$this->form_validation->set_rules('teacher[]', 'teacher', 'required');
}
elseif ($this->input->post('role_id[]') === 2){
$this->form_validation->set_rules('first_name', First name', 'trim|required');
$this->form_validation->set_rules('last_name', 'Last name', 'trim|required');
$this->form_validation->set_rules('username', 'username', 'trim|required|min_length[6]|max_length[12]|is_unique[users.username]');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[6]');
$this->form_validation->set_rules('password2', 'Confirm password', 'trim|required|matches[password]');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|is_unique[users.email]');
$this->form_validation->set_rules('location', 'Location', 'trim|required');
$this->form_validation->set_rules('school[]', 'School', 'required');
$this->form_validation->set_rules('class[]', 'Class', 'required');
$this->form_validation->set_rules('role_id[]', 'Role', 'required');
$this->form_validation->set_rules('class_divisions[]', 'Class division', 'required');
$this->form_validation->set_rules('region', 'Region', 'required');
}
elseif ($this->input->post('role_id[]') === 5 ){
$this->form_validation->set_rules('all_teachers_show', 'ALL Teachers', 'required');
}
if ($this->form_validation->run()==FALSE)
{
$this->signup();
}
else
{
//register
}
}
If I use something like this:
if ($this->input->post('role_id[]') < 2){
// validation rules
}
if ($this->input->post('role_id[]') >4 ){
// validation rules
}
It shows me validation errors but for role_id=1. For role_id=5 shows me validation errors that are for role_id=1.
Could you help me? :) Thanks!
You are passing string 'value' => '2' and trying to compare if identical === to integer 2 which evaluate to FALSE. Cast your input to specified integer and see what have you got after. I.e. (int)$this->input->post('role_id[]') === 2.
public function radiobutton()
{
echo form_open('test/passingthrough');
echo '<input type="radio" name="myradio" value="1" ' . set_radio('myradio', '1', TRUE) . ' />';
echo '<input type="radio" name="myradio" value="2" ' . set_radio('myradio', '2') . ' />';
echo '<input type="radio" name="myradio" value="5" ' . set_radio('myradio', '5') . ' />';
echo form_submit('mysubmit', 'Submit Radio button!');
}
public function passingthrough()
{
$this->form_validation->set_rules('mysubmit', '', 'required');
$this->form_validation->set_rules('myradio', '', 'required');
if ($this->form_validation->run() == FALSE) {
redirect('test/radiobutton', 'refresh');
} else {
echo '<pre>', var_dump($this->input->post('myradio'));
}
}
In this example passed value (and since it is radio, there will be one passed value for sure) will be something you are looking for. But also, if I get you right, you need NULL, FALSE or what ever value for non passed values. So you would have array of all values in your action controller or model, and check it in foreach loop for it:
$possible_values = array(1, 2, 5);
foreach ($possible_values as $p_v) {
if ($this->input->post('role_id') == $p_v) {
//do what do you want with TRUE 'role_id'
} else {
//do what do you want with FALSE 'role_id'
}
}
Related
My problem is validating the records if already exists in database. So i created a Officials Form using Gii Generator in yii2. It contains name_id,position,fname,mname,lname. If the admin wants to create a new data, and that data is already exists it will show a pop-up message "This data already exists".
How can i achieve that? Any ideas?
This is my model:
class Name extends \yii\db\ActiveRecord
{
public static function tableName()
{
return 'name';
}
public function rules()
{
return [
[['position', 'fname', 'lname'], 'required'],
[['position', 'fname', 'mname', 'lname'], 'string', 'max' => 50],
];
}
public function attributeLabels()
{
return [
'name_id' => 'Name ID',
'position' => 'Position',
'fname' => 'First Name',
'mname' => 'Middle Name',
'lname' => 'Last Name',
];
}
}
This is my controller:
public function actionCreate()
{
$model = new Name();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['report/create', 'id' => $model->name_id]);
} else {
return $this->renderAjax('create', [
'model' => $model,
]);
}
}
And this is my _form:
<div class="name-form">
<?php yii\widgets\Pjax::begin(['id' => 'sign-up']) ?>
<?php $form = ActiveForm::begin(['id' => 'form-signup', 'options' => ['data-pjax' => true]]); ?>
<?= $form->field($model, 'position')->textInput(['maxlength' => true,'style'=>'width:500px','placeholder' => 'Enter a Position....']) ?>
<?= $form->field($model, 'fname')->textInput(['maxlength' => true,'style'=>'width:500px','placeholder' => 'Enter a First Name....']) ?>
<?= $form->field($model, 'mname')->textInput(['maxlength' => true,'style'=>'width:500px','placeholder' => 'Enter a Middle Name....']) ?>
<?= $form->field($model, 'lname')->textInput(['maxlength' => true,'style'=>'width:500px','placeholder' => 'Enter a Last Name....' ]) ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-success','style' => 'padding:10px 60px; width:100%;']) ?>
</div>
<?php ActiveForm::end(); ?>
<?php yii\widgets\Pjax::end() ?>
So i assume you want a unique validator, you can try this in your model :
public function rules()
{
return [
[['position', 'fname', 'lname'], 'required'],
[['position', 'fname', 'mname', 'lname'], 'string', 'max' => 50],
[['fname','lname'], 'unique', 'targetAttribute' => ['fname', 'lname'], 'message' => 'This data already exists']
];
}
The above rules will make the combination of fname and lname attributes to be unique, you can modify what attribute or combination of attributes you want to be unique by adding or removing the field name / attributes to the validation rules.
You can create your custom validation , Take any attribute name and define rule :
public function rules()
{
return [
[['position', 'fname', 'lname'], 'required'],
[['position', 'fname', 'mname', 'lname'], 'string', 'max' => 50],
[['position', 'fname', 'lname'], 'checkUniq'], //add this line
];
}
-> custom function in model :
public function checkUniq($attribute, $params)
{
$user = self::find()->where(['fname'=>$this->fname,'lname'=>$this->lname,'position'=>$this->position])->one();
if (isset($user) && $user!=null)
$this->addError($attribute, 'User already added.');
}
Controller I have form_ctrl code is below
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class form_ctrl extends CI_Controller {
public function index()
{
//$this->load->view('welcome_message');
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
//$this->form_validation->set_rules('name', 'Username', 'required');
$this->form_validation->set_rules('name', 'name','required|min_length[5]|max_length[12]');
$this->form_validation->set_rules('pass', 'Password', 'required',
array('required' => 'You must provide a %s.')
);
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('mobile', 'Mobile', 'required');
$this->form_validation->set_rules('address', 'Address','required|min_length[5]');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('table');
}
else
{
$this->load->view('results');
$name=$this->input->post('name');
$pass=$this->input->post('pass');
$email=$this->input->post('email');
$mobile=$this->input->post('mobile');
$address=$this->input->post('address');
$data = array(
'name' =>$name ,
'pass' => $pass,
'email' => $email,
'mobile' => $mobile,
'address' => $address
);
$this->db->insert('form', $data);
}
}
}
View I have result.php code
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<?php echo validation_errors(); ?>
<?php echo form_open(); ?>
<table >
<tr>
<td colspan=2 align="center"><h3>User Details</h3></td>
</tr>
<tr>
<td>
<?php echo form_label('Name'); ?>
</td>
<td>
<?php echo form_input(array('id' => 'name', 'name' => 'name')); ?>
</td>
</tr>
<tr>
<td>
<?php echo form_label('Pass'); ?>
</td>
<td>
<?php echo form_password(array('id' => 'pass', 'name' => 'pass')); ?>
</td>
</tr>
<tr>
<td><?php echo form_label('Email'); ?>
</td>
<td><?php echo form_input(array('id' => 'email', 'name' => 'email')); ?></td>
</tr>
<tr>
<td><?php echo form_label('Mobile'); ?>
</td>
<td><?php echo form_input(array('id' => 'mobile', 'name' => 'mobile')); ?>
</td>
</tr>
<tr>
<td><?php echo form_label('Address'); ?>
</td>
<td><?php echo form_input(array('id' => 'address', 'name' => 'address')); ?>
</td>
</tr>
<tr>
<td colspan="2" align="center"><?php echo form_submit(array('id' => 'submit', 'value' => 'Submit')); ?>
</td>
</tr>
<?php echo form_close(); ?>
</table>
</body>
</html>
In this code I want to include model to insert the data instead of controller.
The code is working properly for insert data into database but I want this through model not from controller. I tried so many times but I didn't get the desirable result.
commonModel.php
class CommonModel extends CI_Model {
function __construct() {
parent::__construct ();
}
public function insert($tableName,$data){
return $this->db->insert($tableName, $data);
}
}
replace your controller code like this
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class form_ctrl extends CI_Controller {
public function index()
{
//$this->load->view('welcome_message');
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->load->model('commonModel');
//$this->form_validation->set_rules('name', 'Username', 'required');
$this->form_validation->set_rules('name', 'name','required|min_length[5]|max_length[12]');
$this->form_validation->set_rules('pass', 'Password', 'required',
array('required' => 'You must provide a %s.')
);
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('mobile', 'Mobile', 'required');
$this->form_validation->set_rules('address', 'Address','required|min_length[5]');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('table');
}
else
{
$this->load->view('results');
$name=$this->input->post('name');
$pass=$this->input->post('pass');
$email=$this->input->post('email');
$mobile=$this->input->post('mobile');
$address=$this->input->post('address');
$data = array(
'name' =>$name ,
'pass' => $pass,
'email' => $email,
'mobile' => $mobile,
'address' => $address
);
$this->commonModel->insert('form', $data);
}
}
}
class form_ctrl extends CI_Controller {
public function index()
{
//$this->load->view('welcome_message');
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
//$this->form_validation->set_rules('name', 'Username', 'required');
$this->form_validation->set_rules('name', 'name','required|min_length[5]|max_length[12]');
$this->form_validation->set_rules('pass', 'Password', 'required',
array('required' => 'You must provide a %s.')
);
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('mobile', 'Mobile', 'required');
$this->form_validation->set_rules('address', 'Address','required|min_length[5]');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('table');
}
else
{
$this->load->view('results');
$name=$this->input->post('name');
$pass=$this->input->post('pass');
$email=$this->input->post('email');
$mobile=$this->input->post('mobile');
$address=$this->input->post('address');
$data = array(
'name' =>$name ,
'pass' => $pass,
'email' => $email,
'mobile' => $mobile,
'address' => $address
);
$this->load->model ( 'user_model' );
$this->user_model->insert('form', $data);
}
}
}
model
class User_model extends CI_Model
{
public function insert($table,$data)
{
$this->db->insert ( $table, $data );
}
}
class form_ctrl extends CI_Controller {
public function index(){
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'name','required|min_length[5]|max_length[12]');
$this->form_validation->set_rules('pass', 'Password', 'required',
array('required' => 'You must provide a %s.')
);
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('mobile', 'Mobile', 'required');
$this->form_validation->set_rules('address', 'Address','required|min_length[5]');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('table');
}
else
{
$data = $this->input->post();
$this->load->view('results',$data);
$this->load->model ( 'user_model' );
$this->user_model->insert('form', $this->input->post());
}
}
}
and your model looks like below.
public function insert($table, $data) {
$param = array(
'name' => $data['name'],
'pass' => $data['pass'],
'email' => $data['email'],
'mobile' => $data['mobile'],
'address' => $data['address']
);
$this->db->insert($table, $param);
}
It's always best practices that your controller part will be light weight and have less code.
**controller**
$postData = $_POST;
$result = $this->batch->addBatch($postData);
**Batch Model**
class Batch_model extends MY_Model {
public function __construct() {
parent::__construct();
}
function addBatch($postData) {
$this->_table = TBL_BATCH;
$result = $this->add($postData);
return $result;
}
}
**My Model**
public $_table;
public $_fields;
public $_where;
protected $_except_fields = array();
protected $soft_delete = TRUE;
function add($PostData) {
$postArray = $this->getDatabseFields($PostData);
$query = $this->db->insert($this->_table, $postArray);
if ($this->db->affected_rows() > 0)
return $this->db->insert_id();
else
return '';
}
protected function getDatabseFields($postData, $tableName = '') {
if (empty($tableName))
$tableName = $this->_table;
$table_fields = $this->getFields($tableName);
$final = array_intersect_key($postData, $table_fields);
return $final;
}
Controller I have form_ctrl code is below
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class form_ctrl extends CI_Controller {
public function index()
{
//$this->load->view('welcome_message');
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
//$this->form_validation->set_rules('name', 'Username', 'required');
$this->form_validation->set_rules('name', 'name','required|min_length[5]|max_length[12]');
$this->form_validation->set_rules('pass', 'Password', 'required',
array('required' => 'You must provide a %s.')
);
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('mobile', 'Mobile', 'required');
$this->form_validation->set_rules('address', 'Address','required|min_length[5]');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('table');
}
else
{
$this->load->view('results');
$name=$this->input->post('name');
$pass=$this->input->post('pass');
$email=$this->input->post('email');
$mobile=$this->input->post('mobile');
$address=$this->input->post('address');
$data = array(
'name' =>$name ,
'pass' => $pass,
'email' => $email,
'mobile' => $mobile,
'address' => $address
);
$this->your_model->insert_data($data)
}
}
}
Here is your model your_model..........
class your_model extends CI_Model
{
function insert_data($data) {
$this->db->insert('your_table', $data);
}
}
** You must load your_model to controller or in autoload
I'm sorry to ask again, I already check answered questions about this but I really can't solve this problem. Any help please. Thank you!
Autoload: $autoload['helper'] = array('form', 'url');
View:
<?php
echo form_open('members/update_password_validation/'.$id, array('role' => 'form'));
echo validation_errors();
echo '<div class="form-group">';
echo form_label('Password:', 'password');
echo form_password(array('name' => 'password', 'id' => 'password', 'class' => 'form-control'));
echo '</div>';
echo '<div class="form-group">';
echo form_label('Confirm Password:', 'cpassword');
echo form_password(array('name' => 'cpassword', 'id' => 'cpassword', 'class' => 'form-control'));
echo '</div>';
echo '<div class="form-group">';
echo form_submit(array('name' => 'update_password_submit', 'value' => 'Update Password', 'class' => 'btn btn-default'));
echo '</div>';
echo form_close();
?>
Controller:
class Members extends CI_Controller {
public function index()
{
//some code here
}
public function update_password_validation($id)
{
$this->load->library('form_validation');
$this->form_validation->set_rules('password', 'Password', 'required|trim');
$this->form_validation->set_rules('cpassword', 'Confirm Password', 'required|trim|matches[password]');
if ($this->form_validation->run())
{
//success
}
else
{
//fail
}
}
}
Why does $this->form_validation->run() always return false?
if ($this->form_validation->run() == FALSE)
{
//success
}
else
{
//fail
}
I’ve a problem with implementing recaptcha in a CodeIgniter application.
The problem is that the recapctha_challenge_field and recaptcha_response_field do not get posted, however, the recapctcha (and those fields) is visible on the page (within the form).
The the recapctha_challenge_field and recaptcha_response_field are appearing in the HTML of the page, but not in the header when I post the form.
I’ve downloaded de recaptcha library and added it as an helper in CI.
Within the form in my view I echo the recaptcha_get_html($publickey) (with the public key set).
In my controller, I load the recaptchalib_helper and add set a form validation rule for the recapctha_challenge_field.
This is my view:
<h1>Register</h1>
<fieldset>
<legend>Personal information</legend>
<?php
echo form_open('login/create_user');
echo form_label('First name:', 'first_name');
echo form_input(
array(
'name' => 'first_name',
'id' => 'first_name',
'value' => set_value('first_name')
)
);
echo form_label('Last name:', 'last_name');
echo form_input(
array(
'name' => 'last_name',
'id' => 'last_name',
'value' => set_value('last_name')
)
);
echo form_label('Birth date:', 'birth_date');
echo form_input(
array(
'name' => 'birth_date',
'id' => 'birth_date',
'value' => set_value('birth_date')
)
);
echo form_label('E-mail:', 'email');
echo form_input(
array(
'name' => 'email',
'id' => 'email',
'value' => set_value('email')
)
);
?>
</fieldset>
<fieldset>
<legend>Login information</legend>
<?php
echo form_label('Username:', 'username');
echo form_input(
array(
'name' => 'username',
'id' => 'username',
'value' => set_value('username')
)
);
echo form_label('Password:', 'password1');
echo form_password(
array(
'name' => 'password1',
'id' => 'password1',
'value' => set_value('password1')
)
);
echo form_label('Confirm password:', 'password2');
echo form_password(
array(
'name' => 'password2',
'id' => 'password2',
'value' => set_value('password2')
)
);
$publickey = "mypublickey"; // here I entered my public key
echo recaptcha_get_html($publickey);
echo form_label(nbs(1), 'submit');
echo form_submit(
array(
'name' => 'submit',
'id' => 'submit',
'value' => 'Registreren'
)
);
echo form_close();
?>
<?php echo validation_errors('<p class="error">'); ?>
</fieldset>
</div>
and this is a part of my controller:
function create_user() {
print_r($_POST);//for debugging
$this->load->library('form_validation');
$this->load->model('user');
$this->form_validation->set_rules('recaptcha_challenge_field', 'Captcha', 'callback_validate_captcha');
$this->form_validation->set_rules('first_name', 'First name', 'trim|xss_clean|required');
$this->form_validation->set_rules('last_name', 'Last name', 'trim|xss_clean|required');
$this->form_validation->set_rules('email', 'E-mail', 'trim|xss_clean|valid_email|callback_is_email_available|required');
$this->form_validation->set_rules('username', 'Username', 'trim|xss_clean|min_length[5]|callback_is_username_available|required');
$this->form_validation->set_rules('password1', 'Password', 'trim|xss_clean|min_length[8]|max_length[32]|required');
$this->form_validation->set_rules('password2', 'Confirm password', 'trim|xss_clean|matches[password1]|required');
if ($this->form_validation->run() == FALSE) {
$this->signup();
} else {
if ($this->user->create_user($this->input->post('username'),$this->input->post('password1'),$this->input->post('email'),$this->input->post('first_name'),$this->input->post('last_name'),$this->input->post('birth_date'))) {
$data['main_content'] = 'login/signup_successful';
$this->load->view('includes/template', $data);
} else {
$this->load->view('login/signup_form');
}
}
}
public function validate_captcha($recaptcha_challenge_field) {
$this->load->helper('recaptchalib');
$privatekey = "myprivatekey";//this is et to my private key
$resp = recaptcha_check_answer ($privatekey,
$this->input->ip_address(),
$this->input->post("recaptcha_challenge_field"),
$this->input->post("recaptcha_response_field"));
if (!$resp->is_valid) {
$this->form_validation->set_message('validate_captcha', 'Invalid Capctha code entered.');
return FALSE;
} else {
return TRUE;
}
}
The capctha fields are not set in the HTTP headers:
Form data:
csrf_test_name:8cc3f2391784867df2d46f193a65a317
first_name:Myfirstname
last_name:Mylastname
birth_date:10-12-2012
email:myemail#adres.com
username:username
password1:password
password2:password
submit:Register
What am I doing wrong?
Your sincerely,
Alwin
Does the form tag not begin within a table, tbody or tr?
When the form doesn't get run because some of the fields are missing the form validator redirects back to current controller/method by default. This means that if I've set a route for 'signup' that routes to auth/register I want it to redirect back to the route path not the actual controller path.
Any suggestions?
function register() {
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Name', 'required|xss_clean');
$this->form_validation->set_rules('description', 'Description', 'required|xss_clean');
$this->form_validation->set_rules('contact', 'Contact', 'required|xss_clean');
$this->form_validation->set_rules('email', 'Email Address', 'required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'required|min_length[' . $this->config->item('min_password_length', 'ion_auth') . ']|max_length[' . $this->config->item('max_password_length', 'ion_auth') . ']|matches[password_confirm]');
$this->form_validation->set_rules('password_confirm', 'Password Confirmation', 'required');
if ($this->form_validation->run() == true) {
// Do some stuff here
} else {
$data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));
$data['name'] = array('name' => 'name',
'id' => 'name',
'type' => 'text',
'value' => $this->form_validation->set_value('name'),
);
$data['description'] = array('name' => 'description',
'id' => 'description',
'type' => 'textarea',
'value' => $this->form_validation->set_value('description'),
'class' => 'form-textarea',
);
$data['email'] = array('name' => 'email',
'id' => 'email',
'type' => 'text',
'value' => $this->form_validation->set_value('email'),
);
$data['contact'] = array('name' => 'contact',
'id' => 'contact',
'type' => 'text',
'value' => $this->form_validation->set_value('contact'),
);
$data['password'] = array('name' => 'password',
'id' => 'password',
'type' => 'password',
'value' => $this->form_validation->set_value('password'),
);
$data['password_confirm'] = array('name' => 'password_confirm',
'id' => 'password_confirm',
'type' => 'password',
'value' => $this->form_validation->set_value('password_confirm'),
);
$this->load->view('organisations/create', $data);
}
}
I would set the default behavior to what you want if the form is not submitted or if there are validation errors and then redirect if the user if the form is successfully processed.
i.e.
function register() {
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Name', 'required|xss_clean');
$this->form_validation->set_rules('description', 'Description', 'required|xss_clean');
$this->form_validation->set_rules('contact', 'Contact', 'required|xss_clean');
$this->form_validation->set_rules('email', 'Email Address', 'required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'required|min_length[' . $this->config->item('min_password_length', 'ion_auth') . ']|max_length[' . $this->config->item('max_password_length', 'ion_auth') . ']|matches[password_confirm]');
$this->form_validation->set_rules('password_confirm', 'Password Confirmation', 'required');
if ($this->form_validation->run()) {
// Do some stuff here to process the form
// Maybe set the success flash message
// Then redirect
redirect('organizations/login');
} else {
$data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));
$data['name'] = array('name' => 'name',
'id' => 'name',
'type' => 'text',
'value' => $this->form_validation->set_value('name'),
);
$data['description'] = array('name' => 'description',
'id' => 'description',
'type' => 'textarea',
'value' => $this->form_validation->set_value('description'),
'class' => 'form-textarea',
);
$data['email'] = array('name' => 'email',
'id' => 'email',
'type' => 'text',
'value' => $this->form_validation->set_value('email'),
);
$data['contact'] = array('name' => 'contact',
'id' => 'contact',
'type' => 'text',
'value' => $this->form_validation->set_value('contact'),
);
$data['password'] = array('name' => 'password',
'id' => 'password',
'type' => 'password',
'value' => $this->form_validation->set_value('password'),
);
$data['password_confirm'] = array('name' => 'password_confirm',
'id' => 'password_confirm',
'type' => 'password',
'value' => $this->form_validation->set_value('password_confirm'),
);
}
$this->load->view('organisations/create', $data);
}
This may require you to change the way you structure some things now, but I've found this to be the easiest way to handle this situation.