Welcome Guest, Not a member yet? Register   Sign In
[Solved] Custom Form Validation Only Showing The Number '1'
#1

[eluser]riwakawd[/eluser]
I am making my own form validation as arrays but for some reason will not display messages and also show the number '1' in the input. I also am using hmvc.

Not sure what I missed. Any Suggestions

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Step_3 extends Controller {
private $error = array();

public function __construct() {
      parent::__construct();
      $this->load->helper('url');
      $this->load->helper('file');
      $this->load->helper('form');
      $this->load->helper('array');
      $this->load->library('form_validation');
      $this->lang->load('english', 'english');
   }

   public function index() {
    if(($this->input->server('REQUEST_METHOD') == 'POST') && $this->validate()) {
     $data['db_hostname'] = $this->input->post('db_hostname');
   $data['db_username'] = $this->input->post('db_username');
   $data['db_password'] = $this->input->post('db_password');
   $data['db_database'] = $this->input->post('db_database');
   $data['db_driver'] = $this->input->post('db_driver');
   $data['db_prefix']  = $this->input->post('db_prefix');

   $data  = $this->load->view('template/configuration/database', $data, true);
   write_file(dirname(FCPATH) . '/admin/application/config/database.php', $data, 'r+');
   write_file(dirname(FCPATH) . '/catalog/config/database.php', $data, 'r+');
   write_file(FCPATH . '/application/config/database.php', $data, 'r+');

   //$this->load->library('migration');
   //$this->migration->current();
   redirect('step_4');
    }

    $data['heading_step_3'] = $this->lang->line('heading_step_3');
  $data['heading_step_3_small'] = $this->lang->line('heading_step_3_small');

  $data['text_license'] = $this->lang->line('text_license');
  $data['text_installation'] = $this->lang->line('text_installation');
  $data['text_configuration'] = $this->lang->line('text_configuration');
  $data['text_finished'] = $this->lang->line('text_finished');
  $data['text_db_connection'] = $this->lang->line('text_db_connection');
  $data['text_db_administration'] = $this->lang->line('text_db_administration');
  $data['text_mysqli'] = $this->lang->line('text_mysqli');

  $data['entry_db_driver'] = $this->lang->line('entry_db_driver');
  $data['entry_db_hostname'] = $this->lang->line('entry_db_hostname');
  $data['entry_db_username'] = $this->lang->line('entry_db_username');
  $data['entry_db_password'] = $this->lang->line('entry_db_password');
  $data['entry_db_database'] = $this->lang->line('entry_db_database');
  $data['entry_db_prefix'] = $this->lang->line('entry_db_prefix');
  $data['entry_username'] = $this->lang->line('entry_username');
  $data['entry_password'] = $this->lang->line('entry_password');
  $data['entry_email'] = $this->lang->line('entry_email');
  
  $data['button_continue'] = $this->lang->line('button_continue');
  $data['button_back'] = $this->lang->line('button_back');



  if (!empty($this->error['db_hostname'])) {
   $data['error_db_hostname'] = $this->error['db_hostname'];
  } else {
   $data['error_db_hostname'] = '';
  }

  if (!empty($this->error['db_username'])) {
   $data['error_db_username'] = $this->error['db_username'];
  } else {
   $data['error_db_username'] = '';
  }

  if (!empty($this->error['db_database'])) {
   $data['error_db_database'] = $this->error['db_database'];
  } else {
   $data['error_db_database'] = '';
  }

  if (!empty($this->error['db_prefix'])) {
   $data['error_db_prefix'] = $this->error['db_prefix'];
  } else {
   $data['error_db_prefix'] = '';
  }

  $data['action'] = site_url("step_3");

  if (!empty($this->input->post('db_driver'))) {
   $data['db_driver'] = $this->input->post('db_driver');
  } else {
   $data['db_driver'] = '';
  }

  if (!empty($this->input->post('db_hostname'))) {
   $data['db_hostname'] = $this->input->post('db_hostname');
  } else {
   $data['db_hostname'] = 'localhost';
  }

  if (!empty($this->input->post('db_username'))) {
   $data['db_username'] = $this->input->post('db_username');
  } else {
   $data['db_username'] = '';
  }

  if (!empty($this->input->post('db_password'))) {
   $data['db_password'] = $this->input->post('db_password');
  } else {
   $data['db_password'] = '';
  }

  if (!empty($this->input->post('db_database'))) {
   $data['db_database'] = $this->input->post('db_database');
  } else {
   $data['db_database'] = '';
  }

  if (!empty($this->input->post('db_prefix'))) {
   $data['db_prefix'] = $this->input->post('db_prefix');
  } else {
   $data['db_prefix'] = '';
  }
  
  $data['back'] = site_url('step_2');

  $this->load->view('template/step_3', $data);
   }

   private function validate() {
  if (!$this->input->post('db_hostname')) {
   $this->error['db_hostname'] = 'Hostname required!';
  }

  if (!$this->input->post('db_username')) {
   $this->error['db_username'] = 'Username required!';
  }

  if (!$this->input->post('db_database')) {
   $this->error['db_database'] = 'Database Name required!';
  }

  if ($this->input->post('db_prefix') && preg_match('/[^a-z0-9_]/', $this->input->post('db_prefix'))) {
   $this->error['db_prefix'] = 'DB Prefix can only contain lowercase characters in the a-z range, 0-9 and "_"!';
  }

  if ($this->input->post('db_driver') == 'mysqli') {
   $mysql = @new mysqli($this->input->post('db_hostname'), $this->input->post('db_username'), $this->input->post('db_password'), $this->input->post('db_database'));

   if ($mysql->connect_error) {
    $this->error['warning'] = 'Error: Could not connect to the database please make sure the database server, username and password is correct!';
   } else {
    $mysql->close();
   }
  }
}
}
#2

[eluser]riwakawd[/eluser]
This seem to fix it.
Code:
if (array_key_exists('db_hostname', $this->error)) {
$data['error_db_hostname'] = $this->error['db_hostname'];
} else {
$data['error_db_hostname'] = '';
}

if (array_key_exists('db_username', $this->error)) {
$data['error_db_username'] = $this->error['db_username'];
} else {
$data['error_db_username'] = '';
}

if ("" == trim($this->input->post('db_hostname'))) {
$data['db_hostname'] = $this->input->post('db_hostname');
} else {
$data['db_hostname'] = 'localhost';
}

if ("" == trim($this->input->post('db_username'))) {
$data['db_username'] = $this->input->post('db_username');
} else {
$data['db_username'] = '';
}
#3

[eluser]treenef[/eluser]
Looks to me like you are writing some kind of installer.

The way to do it properly is to test a db connection with native PHP functions like mysqli_connect(), then write to codeigniter database config file.

Search google for codeigniter installer. You will find a sample link to github of a simpler installer.
#4

[eluser]riwakawd[/eluser]
[quote author="treenef" date="1403307889"]Looks to me like you are writing some kind of installer.

The way to do it properly is to test a db connection with native PHP functions like mysqli_connect(), then write to codeigniter database config file.

Search google for codeigniter installer. You will find a sample link to github of a simpler installer.[/quote]

I have got it done working now. I all ready tried the db connection stuff non of it work.

And then found own way of getting it done so all fixed.




Theme © iAndrew 2016 - Forum software by © MyBB