i have a problem with form validation, this is just standart form validation.
this is weird, even i set the condition, result always pass through the condition and run "$this->model_item_management->tambah_item($item);".
my data always saved without passing form validation. I tried with dummy input $user_name and passing the data to form_validation, but didn't work either. Thank in advanced. this is my code.
public function index(){
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
//this is dummy post
$user_name = $this->input->post('user_name');
//i try to pass an input into variable and use it on form validation
$this->form_validation->set_rules('$user_name', 'username', 'required|min_length[4]');
$this->form_validation->set_rules('namaitem', 'namaitem', 'required');
$this->form_validation->set_rules('specitem', 'specitem', 'required');
$this->form_validation->set_rules('masagaransi', 'masagaransi', 'required');
if ($this->form_validation->run() == FALSE) {
$this->load->view('template/header');
$this->load->view('template/navigation');
$this->load->view('pages/form_tambah_item');
$this->load->view('template/footer');
} else {
$this->load->view('pages/form_sukses_simpan_item');
}
$nama_item = $this->input->post('namaitem');
$spec_item = $this->input->post('specitem');
$garansi_item= $this->input->post('masagaransi');
$item = array(
'NAMA_ITEM'=>$nama_item,
'SPEC_ITEM'=>$spec_item,
'MASA_GARANSI'=>$garansi_item
);
$this->model_item_management->tambah_item($item);
}
<div class="container">
<?php
$attributes = array('class' => 'form-horizontal', 'id' => 'form_tambah_item');
echo form_open('kontrol_tambah_item',$attributes); ?>
<div id="legend">
<legend class="">Tambah Item Form</legend>
</div>
<?php echo form_label( 'username', 'user_name' ); ?>
<?php echo form_input( 'user_name' ); ?>
<div class="control-group">
<!-- Nama item -->
<label class="control-label" for="namaitem">Nama Item</label>
<div class="controls">
<input type="text" id="namaitem" name="namaitem" placeholder="" class="input-xlarge">
<p class="help-block">Masukkan nama dari items</p>
</div>
</div>
<div class="control-group">
<!-- spec item-->
<label class="control-label" >Spesifikansi Item</label>
<div class="controls">
<textarea class="input-xlarge" id="specitem" name="specitem" cols="40" rows="5"></textarea>
<p class="help-block">Isi Spesifikasi Item</p>
</div>
</div>
<div class="control-group">
<!-- Masa garansi -->
<label class="control-label" for="masagaransi">Masa garansi</label>
<div class="controls">
<input type="text" id="masagaransi" name="masagaransi" placeholder="" class="input-xlarge">
<p class="help-block">Isi Masa Garansi Item</p>
</div>
</div>
<div class="control-group">
<!-- Button -->
<div class="controls">
<button type="submit" class="btn btn-success">Tambah</button>
</div>
</div>
</div>
Current:-
$this->form_validation->set_rules('$user_name', 'username', 'required|min_length[4]');
Change to : -
$this->form_validation->set_rules('user_name', 'username', 'required|min_length[4]');
Things to know :
The above function takes three parameters as input:
1.The field name - the exact name you've given the form field.
2.A "human" name for this field, which will be inserted into the error message. For example, if your field is named "user" you might give it a human name of "Username". Note: If you would like the field name to be stored in a language file, please see Translating Field Names.
3.The validation rules for this form field.
Reference :
https://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#validationrules
Thanks
change
$this->form_validation->set_rules('$user_name', 'username', 'required|min_length[4]');
to
$this->form_validation->set_rules('user_name', 'username', 'required|min_length[4]');
this is so dumb, i forgot to add
<?php echo validation_errors(); ?>
in the view,
Related
codeigniter form validation is not working properly for GET method, it returns always false(even if all the input fields are correct) and not showing error messages. But when i tried with POST method, its working fine and showing error message if wrong input fields.
Below is HTML form with 3 input fields and all are mandatory required fields which is i'm checking in validation.
**<form action="" method="get" name="ak-api-log" id="ak-api-log-form" autocomplete="off">**
<div class="row">
<div class="col-md-3">
<div class="form-group">
<label class="bmd-label-floating">API</label>
**<select name="ak_api_type" class="form-control">
<option value="all">All</option>
<option value="admin">Admin</option>
<option value="editor">Editor</option>
</select>**
<?php echo form_error('ak_api_type', '<div class="error">', '</div>'); ?>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label class="bmd-label-floating">From</label>
**<input type="text" name="ak_api_from_date" class="form-control" id="ak-api-from-date"">**
<?php echo form_error('ak_api_from_date', '<div class="error">', '</div>'); ?>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label class="bmd-label-floating">To</label>
**<input type="text" name="ak_api_to_date" class="form-control" id="ak-api-to-date">**
<?php echo form_error('ak_api_to_date', '<div class="error">', '</div>'); ?>
</div>
</div>
<div class="col-md-3">
<button type="submit" id="userBtn" class="btn btn-primary pull-right"><i class="fa fa-search"></i> Search</button>
</div>
</div>
**</form>**
Below is my controller code. I've removed codes not necessary for the question.
public function api_log(){
$this->form_validation->set_rules('ak_api_type', 'ak_api_type', 'required');
$this->form_validation->set_rules('ak_api_from_date', 'ak_api_from_date', 'required');
$this->form_validation->set_rules('ak_api_to_date', 'ak_api_to_date', 'required');
if ($this->form_validation->run() == FALSE){
$this->template->view(strtolower(__CLASS__).'/'.__FUNCTION__);
}else {
echo 'true';exit;
}
}
Quick solutions will be really appreciated. Thanks in advance!
Codeigniter form-validation doesn't support GET method or any other method except POST.
You may want to go for an alternative solution as below.
// before creating the form validation rule.
$dataToValidate = $this->input->get();
// or you can set this data manually as below
$dataToValidate = array(
'username' => 'johndoe',
'password' => 'mypassword',
'passconf' => 'mypassword'
);
// followed by
$this->form_validation->set_data($dataToValidate);
You can set custom data easily in Codeigniter as above.
Read reference: https://codeigniter.com/user_guide/libraries/form_validation.html#validating-an-array-other-than-post
In this when i submit my form it shows errors first then reload the page and go to index page.. but i want is show only error message not reload page after.. here my controller is
class User extends CI_Controller {
public function index()
{
$this->load->view('index');
}
public function error()
{
/* Load form helper */
$this->load->helper(array('form'));
/* Load form validation library */
$this->load->library('form_validation');
/* Set validation rule for name field in the form */
$form_data = array('brand_name' => $this->input->post('brand_name'), 'your_name ' => $this->input->post('your_name'), 'mobile_no' => $this->input->post('Mobile No.'));
if($this->form_validation->run($form_data) == FALSE )
{
echo validation_errors();
}
else
{
$this->load->view('submit');
}
}
}
and my view file named index.php is
<script type="text/javascript">
$(document).ready(function(){
$('#get').click(function(){
$.post("<? base_url() . 'user/error' ?>",
$("form").serialize(),
function(result) {
$("#error_message").html(result).appendTo("form");
},"html");
});
});
</script>
<form role="form" action="" class="form-inline" name="form" method="POST" id >
<div id="error_message" style="color:red"></div>
<?php echo form_open('form'); ?>
<div class="col-md-3">
</div>
<div class="col-md-2">
<div class="form-group">
<input type="text" class="form-control" placeholder="Brand Name" name="brand_name" onfocus="this.placeholder = ''" onblur="this.placeholder='Brand Name'">
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<input type="text" class="form-control" placeholder="Your Name" name="your_name" onfocus="this.placeholder = ''" onblur="this.placeholder='Your Name'">
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<input type="text" class="form-control" placeholder="Mobile No" name="mobile_no" onfocus="this.placeholder = ''" onblur="this.placeholder='Mobile No'">
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<input type="submit" id="get" class="btn btn-success" name="submit" value="Proceed" ></input>
</div>
</div>
<div class="col-md-3">
</div>
</form>
i just want it shows error message only not reload page after please help... thanks in advance
You are trying to post form in AJAX so you will need to set the form data manually
Tested with CI 2
$form_data = array('brand_name' => $this->input->post('brand_name'), 'your_name ' => $this->input->post('your_name')); // other fields as required for validations
And validation for the same would be
if($this->form_validation->run($form_data) == TRUE ){
//validation passed
}else{
//validation fail
}
For CodeIgniter 3, as the Online documentation mentions try https://www.codeigniter.com/userguide3/libraries/form_validation.html#validating-an-array-other-than-post
Validating an Array (other than $_POST)
Sometimes you may want to validate an array that does not originate from $_POST data.
In this case, you can specify the array to be validated:
$data = array(
'username' => 'johndoe',
'password' => 'mypassword',
'passconf' => 'mypassword'
);
$this->form_validation->set_data($data);
I have a web app built on Codeigniter but I don't know which version it is. My job is just adding a simple form for this web app. The form should look like this:
And then here's my VIEW:
<div class="box">
<h4 class="white">ADD CIT Details</h4>
<div class="box-container">
<div class="modal-body form">
<form action="#" id="formcit" class="form-horizontal">
<?php echo form_open('insert_cit_ctrl'); ?>
<div class="form-body">
<?php if (isset($message)) { ?>
<CENTER><h3 style="color:green;">Data inserted successfully</h3></CENTER><br>
<?php } ?>
<div class="form-group">
<label class="control-label col-md-3">Terminal ID</label> <?php echo form_error('dterminalid'); ?>
<div class="col-md-9">
<?php echo form_input(array('id' => 'dterminalid', 'name' => 'dterminalid')); ?>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">CIT</label>
<div class="col-md-9">
<select class="form-control" id="selectcit" >
<option value="none">-- Select CIT --</option>
<option value="CMS">1. CMS</option>
<option value="ALPHA">2. ALPHA</option>
</select>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Branch</label> <?php echo form_error('dbranch'); ?>
<div class="col-md-9">
<?php echo form_input(array('id' => 'dbranch', 'name' => 'dbranch')); ?>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">COMM</label> <?php echo form_error('dcomm'); ?>
<div class="col-md-9">
<?php echo form_input(array('id' => 'dcomm', 'name' => 'dcomm')); ?>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Machine Type</label> <?php echo form_error('dmachinetype'); ?>
<div class="col-md-9">
<?php echo form_input(array('id' => 'dmachinetype', 'name' => 'dmachinetype')); ?>
</div>
</div>
<?php echo form_submit(array('id' => 'submit', 'value' => 'Submit')); ?>
</div>
<?php echo form_close(); ?>
</form>
</div>
</div><!-- end of div.box-container -->
</div> <!-- end of div.box -->
And then this is my controller:
function insert_cit_ctrl() {
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$this->form_validation->set_rules('dterminalid', 'Terminal ID', 'required');
$this->form_validation->set_rules('dcit', 'CIT', 'required');
$this->form_validation->set_rules('dbranch', 'Branch', 'required');
$this->form_validation->set_rules('dcomm', 'COMM', 'required');
$this->form_validation->set_rules('dmachinetype', 'Machine Type', 'required');
if ($this->form_validation->run() == FALSE) {
echo "<script>alert('Something wrong with your input');</script>";
} else {
//Setting values for tabel columns
$data = array(
'terminal_id' => $this->input->post('dterminalid'),
'cit' => $this->input->post('dcit'),
'branch' => $this->input->post('dbranch'),
'comm' => $this->input->post('dcomm'),
'machine_type' => $this->input->post('dmachinetype')
);
//Transfering data to Model
$this->cit_model->modeladdcit($data);
$data['message'] = 'Data Inserted Successfully';
//Loading View
$this->load->view('template', $data);
}
}
And here's my model:
<?php
class Cit_model extends CI_Model{
function __construct() {
parent::__construct();
}
function modeladdcit($data){
$this->load->database('database_office', 'TRUE');
$this->db->insert('tbl_cit', $data);
}
}
?>
And the result? Here's my database:
There's no error at all. It's just that after I submit the data, it won't appeared on my database, not a single data. Meaning, that data is not even INSERT INTO the database.
I don't understand. What is wrong with my code?
UPDATE:
Ok, so I followed your tips guys, like this:
<form action="<?php echo base_url("evangelion/insert_cit_ctrl"); ?>" id="formcit" method="POST" class="form-horizontal">
What actually happened was... NOTHING. I mean, the data still not inserted into the database. And worse, the page ruined, it called only the function, not the whole page like it used to.
add the action and method to your form
<form action="<?php echo base_url("controller_name/function_name"); ?>" id="formcit" method="POST" class="form-horizontal">
First try to display errors like this
you can find where you are wrong..
where you call controller in codeigniter you must call controller
action="controllername/actionname"
<?php
echo ini_get('display_errors');
if (!ini_get('display_errors')) {
ini_set('display_errors', '1');
}
echo ini_get('display_errors');
?>
i thought you did not mention method type in form
method ="POST"
Hi when I use required in my rules in ci form_validation, I know the value of this required input post and don't show any message .but this if ($this->form_validation->run()) is always false.
thanks for your help.
my view:
<form>
<div class="row">
<div class="form-group">
<div class="col-md-2"><label>Name</label></div>
<div class="col-md-10 col-md-pull-1">
<input name="Name" class="form-control" id="Name"
value="<?php echo set_value('Name')?>"
autocomplete="off"></input>
<?php echo form_error('Name'); ?>
</div>
</div>
</div>
</form>
my controller:
public function show_info()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('Name', 'Name', 'required');
$this->form_validation->set_message('required'," please enter %s");
if ($this->form_validation->run()== FALSE)
{
echo "false";
}
else redirect();
}
In your view file, you had wrote nmae except name
<div class="row">
<div class="form-group">
<div class="col-md-2"><label>Name</label></div>
<div class="col-md-10 col-md-pull-1">
<input name="Name" class="form-control" id="Name"
value="<?php echo set_value('Name')?>"
autocomplete="off"></input>
<?php echo form_error('Name'); ?>
</div>
</div>
</div>
correct it.
please load again view file after false form_validation
public function show_info()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('Name', 'Name', 'required');
$this->form_validation->set_message('required'," please enter %s");
if ($this->form_validation->run()== FALSE)
{
echo "false";
$this->load->view('view_file','',true);
}
else redirect();
}
I am making a simple registration form that then submits data to my database. However, the problem that I am running into is that the code that should submit the info to the database is not working.
Here's the form that I made using Twitter Bootstrap
<!DOCTYPE html>
<html>
<body>
<div class="modal-body">
<div class="well">
<div id="myTabContent" class="tab-content">
<div class="tab-pane active in" id="login">
<form method="POST" action='/adding_to_table' class="form-horizontal">
<fieldset>
<div id="legend">
<legend class="">Create Your Account</legend>
</div>
<div class="control-group">
<!-- Username -->
<label class="control-label" for="firstname">First Name</label>
<div class="controls">
<input type="text" id="first_name" name="firstname" placeholder="" class="input-xlarge">
</div>
</div>
<div class="control-group">
<!-- Username -->
<label class="control-label" for="lastname">Last Name</label>
<div class="controls">
<input type="text" id="last_name" name="lastname" placeholder="" class="input-xlarge">
</div>
</div>
<div class="control-group">
<!-- Username -->
<label class="control-label" for="email">E-mail</label>
<div class="controls">
<input type="text" id="e_mail" name="email" placeholder="" class="input-xlarge">
</div>
</div>
<div class="control-group">
<!-- Username -->
<label class="control-label" for="username">Username</label>
<div class="controls">
<input type="text" id="username" name="username" placeholder="" class="input-xlarge">
</div>
</div>
<div class="control-group">
<!-- Password-->
<label class="control-label" for="password">Password</label>
<div class="controls">
<input type="password" id="password" name="password" placeholder="" class="input-xlarge">
</div>
</div>
<div class="control-group">
<!-- Button -->
<div class="controls">
<button class="btn btn-primary">Submit</button>
</div>
</div>
</fieldset>
</form>
</div>
</div>
</div>
</div>
<script class="cssdeck" src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script class="cssdeck" src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.1/js/bootstrap.min.js"></script>
</body>
<html>
This is the route that I am passing to the action field in my form:
Route::get('/adding_to_table', function()
{
return View::make('create');
});
And this is the create.php file that the route (above) is supposed to load which takes care of
the database submission
DB::table('user_info')->insert(
array("First_name" => Input::post('firstname'),
"Last_name" => Input::post("lastname"),
"E-mail" => Input::post("email"),
"Username" => Input::post("username"),
"Password" => Input::post("password"),
)
);
echo "Successfully entered user information into data table";
However, Laravel doesn't like this and is throwing this error at me:
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
Open: /Users/brendanbusey/Desktop/php_site/laravelSite/bootstrap/compiled.php
}))->bind($request);
} else {
$this->methodNotAllowed($others);
}
}
protected function methodNotAllowed(array $others)
{
throw new MethodNotAllowedHttpException($others);
}
protected function check(array $routes, $request, $includingMethod = true)
I have double and triple checked to make sure that I am using the names and not the id's from my html form when I'm sending the data via POST and all my names from the columns in my database match the ones in my code. Any help would be greatly appreciated!
You need to have two routes defined, one for GET and one for POST. If you want to use the same adding_to_table url, it should be something like
Route::get('/adding_to_table', function() {
// code to display your form initially
});
Route::post('/adding_to_table', function() {
// code to process the form submission
});
Maybe I'm not understanding you correctly, but it looks like you are using View::make() to try to run your db insert code. I believe View::make() is just intended to render blade templates, so I don't think this will work. Instead you could put that type of thing into a controller method, or even directly into the post route closure. To access the submitted values, you should use Input::get('firstname') etc. In the laravel docs here it explains that
You do not need to worry about the HTTP verb used for the request, as
input is accessed in the same way for all verbs.
You need to change the form opening line in your HTML to this:
<form method="POST" action='adding_to_table' class="form-horizontal">
In your routes.php file, you need to have this:
Route::get('adding_to_table', function()
{
return View::make('create');
});
Route::post('adding_to_table', function()
{
DB::table('user_info')->insert(
array("First_name" => Input::get('firstname'),
"Last_name" => Input::get("lastname"),
"E-mail" => Input::get("email"),
"Username" => Input::get("username"),
"Password" => Input::get("password"),
)
);
});
Change
Input::post() to Input::get()
to retrieve inputs.
Just add {{ csrf_field() }} below your form, like this:
<form method="POST" action='/adding_to_table' class="form-horizontal">
{{ csrf_field() }}
in Create.blade.php
{!! Form::open(array('route' => 'ControllerName.store','method' => 'POST','files' => true)) !!}
in Controller :-
public function store(Request $request)
{
$this->validate($request, [
'first_name' =>'required',
'last_name' => 'required',
'e_mail' =>'required',
'username' => 'required',
'password' =>'required',
]);
ModelName::create($request->all());
return route->(ControllerName.index);
NOTE : check model with all fields are fillable are not .