Codeigniter Form Validation not submitting form and no errors - php

I had this form working and to be honest I can not figure out what I might have done. The form will not submit now and I don't see any form validation errors.
controller:
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="ncerror" >', '</div>');
$this->form_validation->set_rules('media_consent', 'Media Consent', 'required');
$this->form_validation->set_rules('aic_name', 'Name', 'required');
$this->form_validation->set_rules('aic_phone', 'Cell phone', 'required');
$this->form_validation->set_rules('aic_email', 'Email', 'required');
if ($this->form_validation->run() == FALSE) {
$data = $this->ncformdata();
$this->load->view('templates/sponsorheader', array('title' => 'National Convention Registration'));
var_export($_POST);
$this->load->view('ncform', $this->ncformdata());
$this->load->view('templates/footer2');
}
I have
<?php echo "errors" . validation_errors();
at the top of my form. After submitting form, the form reloads with "errors" displayed but no other output from the validation_errors function. Any help with troubleshooting?

should this line
$this->load->view('ncform', $this->ncformdata());
be this?
$this->load->view('ncform', $data);

You have a condition when
$this->form_validation->run() == FALSE
but I can not see any condition for when the form validation returns TRUE. Try adding a condition when
$this->form_validation->run() == TRUE
Example:
if ($this->form_validation->run() == FALSE) {
$data = $this->ncformdata();
$this->load->view('templates/sponsorheader', array('title' => 'National Convention Registration'));
var_export($_POST);
$this->load->view('ncform', $this->ncformdata());
$this->load->view('templates/footer2');
}
else
{
echo 'There are no form validation errors!.';
}

Related

Codeigniter matches return false

i tried to save account to the db.
when validating the form with form_validation, it always returning false, even when i tried to insert the correct value. heres the code
public function save(){
$params = $this->input->post();
if(!empty($params['id_account']) && !empty($params['password']) ){
$this->form_validation->set_rules('confPass', 'Confirmation Password', 'matches[password]');
}else{
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('confPass', 'Confirmation Password', 'required|matches[password]');
}
$this->form_validation->set_data($params);
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('id_role', 'Role', 'required');
if ($this->form_validation->run() == TRUE) {
unset($params['confPass']);
$params['account_status'] = array_key_exists('account_status', $params) ? $params['account_status'] : ACCOUNT_STATUS_PENDING;
$this->load->model('user_model');
$this->user_model->save($params);
$this->session->set_flashdata('notif', '<div class="alert alert-success">Berhasil menyimpan data.</div>');
redirect('rbac/form/account');
} else {
$error = validation_errors();
echo '<pre>';
print_r($error);
echo '</pre>';
var_dump($params['password']);
var_dump($params['confPass']);
die();
// $this->session->set_flashdata('notif', '<div class="alert alert-danger">'.$error.'</div>');
// redirect('rbac/form/account');
}
}
i tried to returning the validation_errors(), $params['confPass'] & $params['password'] and here is the result :
The Confirmation Password field does not match the password field.
string(1) "e" string(1) "e"
as you can see $params['confPass'] and $params['password'] is match.
if(!empty($params['id_account']) && !empty($params['password']) ){
$this->form_validation->set_rules('confPass', 'Confirmation Password', 'matches[password]');
}else{
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('confPass', 'Confirmation Password', 'required|matches[password]');
}
When the condition you wrote above is true i.e. control is in if (below) block
if(!empty($params['id_account']) && !empty($params['password']) )
you did not put a condition for the 'password' field you only have confirm password rule. Which does not match to anything and although the passwords match it always return false.
You need to add validation rule for password
$this->form_validation->set_rules('password', 'Password', 'required');
before confirm password rule which is looking to match the password.
If your condition states that it does not require password you can do something like,
$this->form_validation->set_rules('password', 'Password', 'min_length[5]');
anything which fulfills your condition but not "required" i.e trim or min_length
Hope it helps.

Form validation is not working in codeigniter

I am working with codeigniter. I have created one function to add university form.
function add_uni_admin()
{
$user_id = $this->session->userdata("user_id");
if (!empty($user_id)) {
$uni_name = $this->input->post("uni_name");
$uni_image = $this->input->post("uni_image");
$uni_email = $this->input->post("uni_email");
$phn_no = $this->input->post("phn_no");
$m_no = $this->input->post("m_no");
$address = $this->input->post("address");
$password = $this->input->post("pass");
$this->form_validation->set_rules('uni_name', 'University Name', 'required');
$this->form_validation->set_rules('uni_email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('phn_no', 'Phone Number', 'required');
$this->form_validation->set_rules('m_no', 'Mobile Number', 'required');
$this->form_validation->set_rules('address', 'Address', 'required');
$this->form_validation->set_rules('pass', 'Password', 'required|matches[conf_pass]');
$this->form_validation->set_rules('conf_pass', 'Confirm Password', 'required');
if ($this->form_validation->run() == false) {
$this->load->view("admin/add_uni_admin");
} else {
echo "successfull";exit;
}
} else {
redirect(base_url());
}
}
When I submit the form without filling any field then it does not give any error.Here I have added form_validation library into autoload.php file and added form helper in it.
So what code should I have to write to display errors?
use echo validation_errors() in your view to display errors.
<html>
<head>
<title>My Form</title>
</head>
<body>
<?php echo validation_errors(); ?>
**your form here.**
</body>
</html>
Add $this->load->library('form_validation');
in your controller function..
And <?php echo validation_errors(); ?>
in your view file to display errors.

validation false, although the data is filled

I have form validation like this :
function insert() {
$this->form_validation->set_message('required', '*column must be filled');
$this->form_validation->set_rules('judul', 'Judul', 'required');
$this->form_validation->set_rules('isi', 'Isi', 'required');
if ($this->form_validation->run() == FALSE) {
$this->layout->addJs('ckeditor/ckeditor.js');
$this->layout->addJs('js/admin/b267648789c30a07b0efa5bce7bdd9fe.js');
$this->layout->view('admin/admin_view/insertAdmin_view');
} else {
$data = array(
'title' => $this->input->post('judul'),
'content' => $this->input->post('isi'),
);
$this->db->insert('newses', $data);
... other proses ...
}
}
although I filled the data in form add data (I use ckeditor for adding data), condition always false so it's not adding data that I want. but it's got error 'if I add some html data, but if I add normal data text it's work well'. once again when I running my program in localhost xampp it's all work but when I use hosting that happen.
any suggest ?
thank you for all advice.
Print the errors of validation
function insert() {
$this->form_validation->set_message('required', '*column must be filled');
$this->form_validation->set_rules('judul', 'Judul', 'required');
$this->form_validation->set_rules('isi', 'Isi', 'required');
//echo validation_errors();
echo $this->input->post('judul')."<br>";
echo $this->input->post('isi');
if ($this->form_validation->run() == FALSE) {
$this->layout->addJs('ckeditor/ckeditor.js');
$this->layout->addJs('js/admin/b267648789c30a07b0efa5bce7bdd9fe.js');
$this->layout->view('admin/admin_view/insertAdmin_view');
} else {
$data = array(
'title' => $this->input->post('judul'),
'content' => $this->input->post('isi'),
);
$this->db->insert('newses', $data);
... other proses ...
}
}

Codeigniter Setting rules for form_validation isn't working properly

I am trying to validate 2 fields here, the "title" and the "HTML" field.
I have this code:
$this->form_validation->set_rules('title', $this->input->post('title'), 'required|min_length[5]|max_length[255]');
$this->form_validation->set_rules('html', $data['html'], 'required');
if ($this->form_validation->run() == FALSE){
echo 'Fail';
}else{
echo 'Success';
}
die();
I can confirm that both variables are valid strings.
The function always returns false, even if both fields are valid. If I only set one rule for one field, then the function will return true if it is a success.
Could somebody please advise?
Many thanks indeed,
Peter
The second parameter of your validation rules should be the human readable name of the field, not the actual data. Consider the following code.
$this->form_validation->set_rules('title', 'Title', 'required|min_length[5]|max_length[255]');
$this->form_validation->set_rules('html', 'HTML', 'required');
The first rule will check the POST variable $_POST['title'] to ensure that it is set and is between 5 and 255 characters. If it fails these rules the name Title is used in the error message. The second rule will check the variable $_POST['html'] to ensure that it is set and if it is not set will use the name HTML in the error message.
Take a look at the CodeIgniter Documentation for more specific implementation details.
Based on your code, it looks like you want to run some variables that are not part of the $_POST array through form validation. To do this you have two choices. First you can put all of the data you want to validate into an array and use $this->form_validation->set_data($array) to use that array instead of $_POST. Alternatively you can just add the fields you want to validate to $_POST.
set_data() example:
$formData = array('title' => $this->input->post('title'), 'html' => $data['html']);
$this->form_validation->set_data($formData);
$this->form_validation->set_rules('title', 'Title', 'required|min_length[5]|max_length[255]');
$this->form_validation->set_rules('html', 'HTML', 'required');
if ($this->form_validation->run() == FALSE){
echo 'Fail';
}else{
echo 'Success';
}
$_POST example:
$_POST['html'] = $data['html'];
$this->form_validation->set_rules('title', 'Title', 'required|min_length[5]|max_length[255]');
$this->form_validation->set_rules('html', 'HTML', 'required');
if ($this->form_validation->run() == FALSE){
echo 'Fail';
}else{
echo 'Success';
}
Personally I recommend the $_POST method.

Codeigniter 2.1 send data from one controller to another

I have function like this:
function gi_insert()
{
if(!IS_AJAX){
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Naslov', 'trim|required|strip_tags');
$this->form_validation->set_rules('body', 'Tekst', 'trim|required|strip_tags');
$this->form_validation->set_rules('email', 'E-mail', 'trim|required|strip_tags|valid_email');
$this->form_validation->set_rules('tag', 'Tagovi', 'trim|required|strip_tags');
if(isset($_POST['category_new'])){
$this->form_validation->set_rules('category_new', 'Kategorija', 'trim|strip_tags');
}
$p = $this->uri->segment(3);
if ($this->form_validation->run() == FALSE)
{
$errors = validation_errors();
redirect("admin/create/$p", 'location');
}
else
{
$this->gi->gi_insert();
redirect('admin/pregled/' . $this->uri->segment(3));
}
} else
{
$this->gi->gi_insert();
}
}
How can I send validation errors to admin/create controller? At the moment, this is working, but I don't get error report (page where errors should appear is containig
<?php echo validation_errors(); ?>
)
validation_errors() only works if the $_POST data is still set, since you redirect to admin/create, $_POST is empty and the function no longer works, returning nothing.
You could send the errors with a piece of flashdata to the page.

Categories