I am doing an airline reservation and I have 2 radio button.
1) One way
2) Round Trip
The things that I've done is when I select the Round Trip, all the fields are there(Depart, return and number of passengers) but I when select One way radio button the return field should hide.
In my controller, I have validation that all fields are required. The problem is, whenever I tried to search in One Way(the return field is hidden) it gives me an "Return field is required" error
Question: How can I prevent the validation in return field when I choice the One Way radio button?
View
<div class="pure-u-1-1 searchcontainer center">
<div class="pure-u-1-1 findcheaptxt">
<span>Find Cheap Flights</span>
</div>
<div class="pure-u-1-1 radiobtn">
<form action="">
<input type="radio" name="flight_type" value="one_way" class="onew" style="" >One Way
<input type="radio" name="flight_type" class="roundw" style="" checked>Round Trip
</form>
</div>
<form method="post" enctype="multipart/form-data" action="<?= base_url() .'User/search'?>">
<?= validation_errors(); ?>
<div class="pure-u-1-1 fromto">
<div class="pure-u-1-1">
<label for="from" class="margin2px">From</label>
<select name="flight_from">
<option value="">-- Please select depature --</option>
<?php foreach($countries as $country):?>
<option value ="<?= $country->country_name?>" ><?= $country->country_name?></option>
<?php endforeach?>
</select>
</div>
<div class="pure-u-1-1">
<label for="to" class="tomargin">To</label>
<!-- <input type="text" class="fromto"><br> -->
<select class="fromto" name="flight_to">
<option value="">-- Please select destination --</option>
<?php foreach($countries as $country):?>
<option value ="<?= $country->country_name?>" ><?= $country->country_name?></option>
<?php endforeach?>
</select>
</div>
<div class="pure-u-1-1 dr" name ="depart">
<label for="depart" class="drr">Depart</label>
<input type="date" id="depart" name="depart" class="departreturn">
</div>
<div class="pure-u-1-1 dr" id="try">
<label for="return" class="drr">Return</label>
<input type="date" id="return" name="return" class="departreturn"><br>
</div>
</div>
<div class="pure-u-1-1 personfield">
<!-- <div class="pure-u-1-5 margin">
Adult<br>
<input type="text" name="" class="person">
</div>
<div class="pure-u-1-5 margin">
Seniors<br>
<input type="text" name="" class="person">
</div>
<div class="pure-u-1-5 margin">
Children<br>
<input type="text" name="" class="person">
</div>
<div class="pure-u-1-5">
Class<br>
<input type="text" name="" class="person">
</div> -->
<div class="pure-u-1-5 margin">
Number of Passengers<br>
<input type="text" name="no_of_passengers" class="person">
</div>
</div>
<div class="pure-u-1-1 center">
<button class="submitbtn">Search Now</button>
</form>
</div>
</div>
</div>
Controller
public function search()
{
$data['countries'] = $this->CrudModel->get('countries');
$this->form_validation->set_error_delimiters('<div class="alert alert-danger" role="alert">', '</div>');
$this->form_validation->set_rules('flight_from', 'Select depature', 'required|trim');
$this->form_validation->set_rules('flight_to', 'Select Destination', 'required|trim');
if ($_POST['flight_type'] == 'round_trip')
{
$this->form_validation->set_rules('depart', 'Date of flight', 'required|trim');
$this->form_validation->set_rules('return', 'Date of return', 'required|trim');
$this->form_validation->set_rules('no_of_passengers', 'Number of Passengers', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->index();
}
else
{
$search_result = array(
$flight_from = $_POST['flight_from'],
$flight_to = $_POST['flight_to'],
$depart = $_POST['depart'],
$return = $_POST['return'],
$no_of_passengers = $_POST['no_of_passengers']
);
$data['search_result'] = $this->CrudModel->search('flight',$flight_from,$flight_to,$depart,$return,$no_of_passengers);
$this->load->view('partials/header');
$this->load->view('partials/nav');
$this->load->view('result',$data);
}
}
else
{
$this->form_validation->set_rules('depart', 'Date of flight', 'required|trim');
$this->form_validation->set_rules('no_of_passengers', 'Number of Passengers', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->index();
}
else {
$search_result = array(
$flight_from = $_POST['flight_from'],
$flight_to = $_POST['flight_to'],
$depart = $_POST['depart'],
$no_of_passengers = $_POST['no_of_passengers']
);
$data['search_result'] = $this->CrudModel->search('flight',$flight_from,$flight_to,$depart,$no_of_passengers);
$this->load->view('partials/header');
$this->load->view('partials/nav');
$this->load->view('result',$data);
}
}
}
My ajax/js when hiding the return field
<script type="text/javascript">
$(document).on('change', 'input:radio[name=flight_type]', function(){
$('div[id^="try"]').toggle(); // hide all DIVs begining with "my_radio_"
$('#' + $(this).attr('id') + '_text').show(); // show the current one
});
</script>
First of all you have to add value to round trip:
<input type="radio" name="flight_type" value="round_trip" class="roundw" style="" checked>Round Trip
Then do validation condition like below:
// Global validation for this method
if ($_POST['flight_type'] == 'round_trip')
{
$this->form_validation->set_rules('depart', 'Date of flight', 'required|trim');
$this->form_validation->set_rules('return', 'Date of return', 'required|trim');
}
else if($_POST['flight_type'] == 'one_way'){
// add validation for one way trip
}
You have to pass flight_type parameters in post request. Then your code will be something like this describe below.
$this->form_validation->set_rules('flight_from', 'Select depature', 'required|trim');
$this->form_validation->set_rules('depart', 'Date of flight', 'required|trim');
if($this->input->post('flight_type')== 'roundw'):
$this->form_validation->set_rules('flight_to', 'Select Destination', 'required|trim');
$this->form_validation->set_rules('return', 'Date of return', 'required|trim');
endif:
Let me know if it not works for you.
Related
I am working on a basic blog application with Codeigniter 3.1.8 and Bootstrap 4.
There is, among others an "Edit account information" form:
<?php echo form_open(base_url('dashboard/users/update')); ?>
<input type="hidden" name="id" id="uid" value="<?php echo $author->id; ?>">
<div class="form-group <?php if(form_error('first_name')) echo 'has-error';?>">
<input type="text" name="first_name" id="first_name" class="form-control" value="<?php echo $author->first_name;?>" placeholder="First name">
<?php if(form_error('first_name')) echo form_error('first_name'); ?>
</div>
<div class="form-group <?php if(form_error('last_name')) echo 'has-error';?>">
<input type="text" name="last_name" id="last_name" class="form-control" value="<?php echo $author->last_name;?>" placeholder="Last name">
<?php if(form_error('last_name')) echo form_error('last_name'); ?>
</div>
<div class="form-group <?php if(form_error('email')) echo 'has-error';?>">
<input type="text" name="email" id="email" class="form-control" value="<?php echo $author->email;?>" placeholder="Email">
<?php if(form_error('email')) echo form_error('email'); ?>
</div>
<div class="form-group <?php if(form_error('bio')) echo 'has-error';?>">
<textarea name="bio" id="bio" cols="30" rows="5" class="form-control" placeholder="Add a short bio"><?php echo $author->bio; ?></textarea>
<?php if(form_error('bio')) echo form_error('bio'); ?>
</div>
<div class="form-group">
<input type="submit" value="Save" class="btn btn-block btn-md btn-success">
</div>
<?php echo form_close(); ?>
It's corresponding controller logic is this:
public function edit($id) {
// Only logged in users can edit user profiles
if (!$this->session->userdata('is_logged_in')) {
redirect('login');
}
$data = $this->Static_model->get_static_data();
$data['pages'] = $this->Pages_model->get_pages();
$data['categories'] = $this->Categories_model->get_categories();
$data['author'] = $this->Usermodel->editAuthor($id);
$this->load->view('partials/header', $data);
$this->load->view('dashboard/edit-author');
$this->load->view('partials/footer');
}
public function update() {
// Only logged in users can edit user profiles
if (!$this->session->userdata('is_logged_in')) {
redirect('login');
}
$id = $this->input->post('id');
$data = $this->Static_model->get_static_data();
$data['pages'] = $this->Pages_model->get_pages();
$data['categories'] = $this->Categories_model->get_categories();
$data['author'] = $this->Usermodel->editAuthor($id);
$this->form_validation->set_rules('first_name', 'First name', 'required');
$this->form_validation->set_rules('last_name', 'Last name', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email');
$this->form_validation->set_error_delimiters('<p class="error-message">', '</p>');
if($this->form_validation->run() === FALSE)
{
$this->load->view('partials/header', $data);
$this->load->view('dashboard/edit-author');
$this->load->view('partials/footer');
} else
{
$this->Usermodel->update_user($id);
$this->session->set_flashdata('user_updated', 'Your account details have been updated');
redirect(base_url('/dashboard/manage-authors'));
}
}
The problem I have not been able to solve is related to the validation of this form: if I leave a required filed empty, and try to submit the form, the form is loaded, with the proper validation error message, only the invalid field is populated with the data from the database and so, valid data is displayed along with the error message, as seen in the form below:
However, as long as the field's current value is not replaced with an invalid one, I want it (the field value) to come from the database.
How can I solve this issue?
If you want to keep the submitted data on an input value, you could modify the email codes like this :
<div class="form-group <?php if(form_error('email')) echo 'has-error';?>">
<input type="text" name="email" id="email" class="form-control" value="<?php echo set_value('email', $author->email); ?>" placeholder="Email">
<?php if(form_error('email')) echo form_error('email'); ?>
</div>
This will set the submitted data as the email value (if available), or email data from the database if there is no submitted email data.
Creating my own application to learn CodeIgniter, by following CI documentation, i can't post my data into db.
I'm using create.php:
<p class="h4 mb-4 text-center"><?= $title ?></p>
<label>Username</label>
<input type="text" id="textInput" class="form-control mb-4" placeholder="Your username" name="username">
<label for="textInput">Title</label>
<input class="form-control mb-4" placeholder="Your title" name="title">
<label>Your message</label>
<textarea class="form-control mb-4" placeholder="Your message content" name="body"></textarea>
<div class="row">
<div class="col-8">
<select class="browser-default custom-select mb-4" id="select" name="genre">
<option selected="">Choose your Genre</option>
<option value="1">Male</option>
<option value="2">Female</option>
</select>
</div>
<div class="col-2">
<input class="form-control mb-4" placeholder="Your age" name="age">
</div>
</div>
<select class="browser-default custom-select mb-4" name="platform">
<option value="" disabled="" selected="">Choose your Platform</option>
<option value="1">Snapchat</option>
<option value="2">Kik</option>
<option value="3">Instagram</option>
<option value="4">Telegram</option>
</select>
<button class="btn btn-info my-4 btn-block" type="submit">Sign up</button>
<div class="text-center">
<p>By clicking
<em>Sign up</em> you agree to our
terms of use.
</p>
</div>
This is the controller:
public function create(){
$data['title'] = 'Add your Username';
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('body', 'Body', 'required');
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('genre', 'Genre', 'required');
$this->form_validation->set_rules('platform', 'Platform', 'required');
$this->form_validation->set_rules('age', 'Age', 'required');
if($this->form_validation->run() === FALSE){
$this->load->view('templates/header');
$this->load->view('posts/create', $data);
$this->load->view('templates/footer');
} else {
$this->post_model->create_post();
redirect('posts');
}
}
This is the model:
public function create_post(){
$slug = url_title($this->input->post('title'));
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'body' => $this->input->post('body'),
'username' => $this->input->post('username'),
'genre' => $this->input->post('genre'),
'age' => $this->input->post('age'),
'platform' => $this->input->post('platform'),
);
return $this->db->insert('posts', $data);
}
I've added as well in autoload (libraries->form_validation) and (helper->form).
I've already tested everything before those codes, and it was working fine. The problem is that those data in HTML i want to send them into db, but even validator works on blank forms (submitting without content) or inserting content, it's just realod the page, as it should redirecting it to posts url.
Someone can help me?
where your post method in view ?
Ex :
<form method="post" name="from_submit" action="">
You forgot to add form
<form method="post" name="myform" action="">
//Your form code---
</form>
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 have a view page.i have to edit a list of values.bt after submit i have an error disallowed character key.I cant find it how this error occur.
view
foreach($track_details->result() as $name) { ?>
<form class="form-horizontal" method="post" id="income_edit" action="<?php echo base_url();?>income_list_cntrl/update_income" enctype="multipart/form-data">
<input type="text" name="track_id" value="<?php echo $track_id; ?>" >
<div class="control-group">
<label class="control-label">Income Name</label>
<div class="controls">
<input type="text" class="span6 m-wrap" value="<?echo $name->income_name?>" name="income_name" id="income_name" readonly>
<?php echo form_error('income_name'); ?>
</div>
</div>
<div class="control-group">
<label class="control-label">Income Amount</label>
<div class="controls">
<input type="text" class="span6 m-wrap" value="<?echo $name->income_amount?>" name="income_amount" id="income_amount" >
<?php echo form_error('income_amount'); ?>
</div>
</div>
<div class="control-group">
<label class="control-label">Crop Quantity</label>
<div class="controls">
<input type="text" class="span6 m-wrap" value="<?echo $name->crop_quantity?>" name=crop_quantity" id="crop_quantity" >
<?php echo form_error('crop_quantity'); ?>
</div>
</div>
<div class="control-group">
<label class="control-label">Per Rate</label>
<div class="controls">
<input type="text" class="span6 m-wrap" value="<?echo $name->per_rate?>" name=per_rate" id="per_rate" >
<?php echo form_error('per_rate'); ?>
</div>
</div>
<div class="control-group">
<label class="control-label">Choose a client</label>
<div class="controls">
<select class="span6 m-wrap" name="select_client" >
<? foreach ($client_details->result() as $var) { ?>
<option <?php if($name->client_id == $var->client_id) { ?> selected="selected" <? } ?> value="<?echo $var->client_id;?>"><?echo $var->client_name;?></option>
<?}?>
</select>
<?php echo form_error('select_client'); ?>
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn green" id="Submit">Submit</button>
</form>
<?}?>
controller
After removing the crop quantity and per rate no error but dont edit it.
public function update_income(){
$this->form_validation->set_error_delimiters('<div style="color:#B94A48">', '</div>');
//$this->form_validation->set_rules('income_name', 'Income name', 'required');
$this->form_validation->set_rules('income_amount', 'Income Amount', 'required');
$this->form_validation->set_rules('crop_quantity', 'Crop Quantity', 'required');
$this->form_validation->set_rules('per_rate', 'Per Rate', 'required');
$this->form_validation->set_rules('select_client', 'Client Name', 'required');
if ( $this -> form_validation -> run() === FALSE ){
$this->index();
} else {
$track_id=$this->input->post('track_id');
$income_name=$this->input->post('income_name');
$income_amount=$this->input->post('income_amount');
$crop_quantity=$this->input->post('crop_quantity');
$per_rate=$this->input->post('per_rate');
$client_name=$this->input->post('select_client');
$this->load->model('income_edit_model');
$data = array(
'income_name'=>$income_name,
'income_amount'=>$income_amount,
'crop_quantity'=>$crop_quantity,
'per_rate'=>$per_rate,
'client_name'=>$client_name,
);
$result=$this->income_edit_model->update_track_income($data,$track_id);
// redirect(base_url().'income', 'refresh');
}
}
Two problems:
First: Missing <?php open tags. Look through this and change <? to <?php
<select class="span6 m-wrap" name="select_client" >
<? foreach ($client_details->result() as $var) { ?>
<option <?php if($name->client_id == $var->client_id) { ?> selected="selected" <? } ?> value="<?echo $var->client_id;?>"><?echo $var->client_name;?></option>
<? } ?>
</select>
Second: Missing closing </div
<div class="form-actions">
<button type="submit" class="btn green" id="Submit">Submit</button>
<!-- MISSING </div> HERE -->
</form>
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,