Postingan lainnya
Codeigniter form validation
Ane mau tanya gan ane ngikut di tutorial <a href='http://mbahcoding.com/tutorial/php/codeigniter/codeigniter-ajax-crud-modal-server-side-validation.html'>http://mbahcoding.com/tutorial/php/codeigniter/codeigniter-ajax-crud-modal-server-side-validation.html</a> disitu ane nemu validasi yg agak beda yg sama ane gunain biasanya scriptnya kaya dibawah
private function _validate()
{
$data = array();
$data['error_string'] = array();
$data['inputerror'] = array();
$data['status'] = TRUE;
if($this->input->post('firstName') == '')
{
$data['inputerror'][] = 'firstName';
$data['error_string'][] = 'First name is required';
$data['status'] = FALSE;
}
if($this->input->post('lastName') == '')
{
$data['inputerror'][] = 'lastName';
$data['error_string'][] = 'Last name is required';
$data['status'] = FALSE;
}
if($data['status'] === FALSE)
{
echo json_encode($data);
exit();
}
}
kalo ane mau coba implement pake form validation yg biasa bagaimana ya, seperti script dibawah, karena script diataskan cuma ketika fieldnya kosong aja.. ane mau coba implement min_length atau max_length misalkan pake script diatas
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[12]');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[8]');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'trim|required|matches[password]');
7 Jawaban:
private function _validate()
{
// load library form validation
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
// set rule form input set_rules('input name', 'title / caption info, 'syarat / option validasi')
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[12]');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[8]');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'trim|required|matches[password]');
if ($this->form_validation->run() == FALSE)
{
// misal jika validasi gagal, redirect ke form untuk melengkapi syarat input
}
else
{
// misal jika validasi berhasil, input data ke db dan redirect page ke dashboard
}
}
referensi : https://codeigniter.com/user_guide/libraries/form_validation.html
cara diatas bisa diaplikasikan ke yg gak reload page ga ya mas? saya coba cara ini sih.. tapi masih eror
private function _validate2()
{
$data = array(
array(
'field' => 'show_title',
'label' => 'Show Title',
'rules' => 'required|trim|min_length[5]|max_length[12]'
)
);
if($this->form_validation->run()==FALSE)
{
echo json_encode($data);
exit();
}
}
sebelum execute validation run, coba tambahin set_rule
$this->form_validation->set_rules($data);
bisa aja di post tanpa reload page, pake ajax coba liat di http://stackoverflow.com/questions/37185883/do-form-validation-with-jquery-ajax-in-codeigniter
saya coba cara seperti ini mas, sudah berhasil sih //Controller
public function ajax_add()
{
$this->form_validation->set_rules('firstname', 'First Name', 'required|min_length[5]|max_length[12]');
$this->form_validation->set_rules('lastname', 'Last Name', 'required');
if($this->form_validation->run()==FALSE)
{
echo json_encode(array('st'=>0, 'msg' => validation_errors()));
}else{
echo json_encode(array('st'=>0, 'msg' => 'Successfully Submiited'));
}
}
tapi saya mau coba pake form_error bukan validation_error kira2 bagaimana ya, jadi messagenya per text field ini script add form saya
<div id="validation-error"></div>
<form action="#" id="form" method="post" enctype="multipart/form-data">
<div class="row">
<div class="col-md-8">
<div class="form-body">
<div class="form-group">
<label>First Name</label>
<input type="text" class="form-control" name="firstname" value="<?php echo set_value('firstname'); ?>">
<span class="help-block"></span>
</div>
<div class="form-group">
<label>Last Name</label>
<input type="text" class="form-control" name="lastname" value="<?php echo set_value('lastname'); ?>">
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<button type="button" id="btnSave" onclick="insert()" class="btn btn-success">
<span class="glyphicon glyphicon-floppy-disk"></span> Simpan
</button>
<button type="reset" class="btn btn-default">
<span class="glyphicon glyphicon-floppy-disk"></span> Clear
</button>
</div>
</div>
</form>
<script type="text/javascript">
function insert()
{
$('#btnSave').text('saving...'); //change button text
$('#btnSave').attr('disabled',true); //set button disable
var url;
url = "<?php echo site_url('validate/ajax_add')?>";
// ajax adding data to database
tinyMCE.triggerSave();
var formData = new FormData($('#form')[0]);
$.ajax({
url : url,
type: "POST",
data: formData,
contentType: false,
processData: false,
dataType: "JSON",
success: function(data)
{
if(data.st == 0)
{
$('#validation-error').html(data.msg).css('color','red');
}
if(data.st == 1)
{
$('#validation-error').html(data.msg);
}
$('#btnSave').text('save'); //change button text
$('#btnSave').attr('disabled',false); //set button enable
},
error: function (jqXHR, textStatus, errorThrown)
{
alert('Error adding / update data');
$('#btnSave').text('save'); //change button text
$('#btnSave').attr('disabled',false); //set button enable
}
});
}
</script>
paling gini..
html
<html>
<head>
<title>Home</title>
<style media="screen">
.errorMessage{
color:red;
}
</style>
</head>
<body>
<div class="ajax_result">
<form id="form">
<h5>firstname</h5>
<input type="text" name="firstname" value="<?php echo set_value('firstname'); ?>" />
<div class="errorMessage"><?php echo form_error('firstname'); ?></div>
<h5>lastname</h5>
<input type="text" name="lastname" value="<?php echo set_value('lastname'); ?>" />
<div class="errorMessage"><?php echo form_error('lastname'); ?></div>
<div><input type="button" id="submit" value="Submit" /></div>
</form>
</div>
</body>
</html>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#submit').click(function(){
jQuery.ajax({
type: "POST",
url: "index.php/welcome/ajax_add",
data: $("#form").serialize(),
success: function(res) {
$(".ajax_result").html(res);
}
});
});
});
</script>
controller
public function ajax_add()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('firstname', 'First Name', 'required|min_length[5]|max_length[12]');
$this->form_validation->set_rules('lastname', 'Last Name', 'required');
if ($this->form_validation->run() == false) {
$this->load->view('ajax');
} else {
echo 'success';
}
}
Mas,
$this->load->view('ajax');
itu apa ya?