Please please please can someone help me
$this->load->library('form_validation');
$this->load->helper('cookie');
$data = array();
if($_POST) {
// Set validation rules including additional validation for uniqueness
$this->form_validation->set_rules('yourname', 'Your Name', 'trim|required');
$this->form_validation->set_rules('youremail', 'Your Email', 'trim|required|valid_email');
$this->form_validation->set_rules('friendname', 'Friends Name', 'trim|required');
$this->form_validation->set_rules('friendemail', 'Friends Email', 'trim|required|valid_email');
// Run the validation and take action
if($this->form_validation->run()) {
echo 'valid;
}
}
else{
echo 'problem';
}
Form validation is coming back with no errors can cany one see why?
Is it actually echoing 'valid'? (you're missing an apostrophe there, btw)
The code you show will only echo 'problem' when $_POST is false, not when validation fails.
Without knowing more, it may be as simple as:
// Run the validation and take action
if($this->form_validation->run()) {
echo('valid');
} else {
echo('invalid');
}
Try like this without checking if $_POST is set - not really needed:
//validation rules here
//
if ($this->form_validation->run() == TRUE) {
//do whatever that shall be run on succeed
} else {
$this->load->view('form'); //load the form
}
Read more about the controller part here
Related
hoping someone can help me solve this problem, thanks a lot for providing the solution
i recently had a problem with my library called my_form_validation on codeigniter version 3. i don't know why it can enter validation true even though it was wrong in function photo check
I have done several options but none of the results I want. my example already used the callback it's the same
example code controller
public function validation_photo()
{
// load custom library Validation
$this->my_form_validation->config_photo();
$this->my_form_validation->photo_checker($_FILES['photo']);
// Conditional validation form
if ($this->form_validation->run() == FALSE) {
// does not pass validation
$data['Title'] = 'Profile';
$data['Content'] = 'management/V_Profile';
$this->load->view('dinamis/Layout', $data);
} else {
// passed validation
var_dump('passed');
}
}
the code above is my loading from my_form_validation library which works for configuring and validating the upload format
example code my_form_validation (config_photo)
function config_photo()
{
$ci = get_instance();
$config = array(
array(
'field' => 'photo',
'label' => 'photo',
'rules' => 'photo_checker',
),
);
$ci->form_validation->set_rules($config);
}
the code above is to set the rules to be used, I use photo_checker
example code my_form_validation (photo_checker)
function photo_checker($photo)
{
$ci = get_instance();
$allowed_mime_type_arr = array('image/jpeg', 'image/png', 'image/x-png', 'image/jpg');
$mime = $photo['type'];
if (in_array($mime, $allowed_mime_type_arr)) {
return TRUE;
} else {
$ci->form_validation->set_message('photo_checker', 'Please select a supported format (jpeg,png,jpg).');
return FALSE;
}
}
The above code is for validating if it doesn't match the desired format then it returns false with the message set
very very thank you very much for helping me.
I hope this issue gets resolved well in this forum, I'm sure there are really great people here
Login form validation not working for me.It works fine for signup but when I tried same thing with my login form.Some checks not working properly in code igniter.
here is my code.I run code ,it goes in my else condition but don't show validation error.
First two checks creating problem for me.When input empty or some enter wrong format of email.
function index() {
$data['nav']= $this->category_model->view_brands();
if($_POST) {
$data['error'] = '';
$this->load->library('form_validation');
$this->form_validation->set_rules('email_login', 'Email', 'required|valid_email');
$this->form_validation->set_rules('password_login', 'Password', 'required');
if($this->form_validation->run() !== FALSE) {
$log_in = $this->login_model->login_beyond(
$this->input->post('email_login'),
md5($_POST['password_login'])
);
if($log_in !== FALSE) {
echo "<script>window.location.href=\"../index.php\"</script>";
}
else {
// Set your error message
$data['error'] = 'Wrong Username/Password';
}
}
else {
// Set the validation errors
echo "it came heere but donot show errors";
$data['error'] =validation_errors();
}
}
$data['login_not'] = "login";
$this->template->load('template','client/login_view',$data);
}
It is best to write if ($this->form_validation->run() === FALSE) and work from there instead of writing if($this->form_validation->run() !== FALSE). This is because in form validation, when run() return true, it may be because it evaluated all results and did not find any errors or the values given did not even exist so error validation did not apply on non-existing values.
Lemme know if it works
I have two numeric fields to collect data from users. Need to validate it using codeigniter form validation class.
Conditions:
First field can be zero
Second field cannot be zero
First field should not be equal to second field
Second field should be greater than first field
Currently I use
$this->form_validation->set_rules('first_field', 'First Field',
'trim|required|is_natural');
$this->form_validation->set_rules('second_field', 'Second Field',
'trim|required|is_natural_no_zero');
But, how to validate for 3rd and 4th condition mentioned above?
Thanks in advance.
Thanks dm03514. I got it working by the below callback function.
$this->form_validation->set_rules('first_field', 'First Field', 'trim|required|is_natural');
$this->form_validation->set_rules('second_field', 'Second Field', 'trim|required|is_natural_no_zero|callback_check_equal_less['.$this->input->post('first_field').']');
and the callback function is:
function check_equal_less($second_field,$first_field)
{
if ($second_field <= $first_field)
{
$this->form_validation->set_message('check_equal_less', 'The First &/or Second fields have errors.');
return false;
}
return true;
}
Everything seems to be working fine now :)
You can write your own validation function for 3, and 4 using callbacks
http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#callbacks
Example from doc
<?php
class Form extends CI_Controller {
public function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'callback_username_check');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|is_unique[users.email]');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
$this->load->view('formsuccess');
}
}
public function username_check($str)
{
if ($str == 'test')
{
$this->form_validation->set_message('username_check', 'The %s field can not be the word "test"');
return FALSE;
}
else
{
return TRUE;
}
}
}
?>
If you are using HMVC and the accepted solution doesn't work then
add the following line after initialization in your controller
$this->form_validation->CI =& $this;
So it will be
$this->load->library('form_validation');
$this->form_validation->CI =& $this;
in your controller.
I am using uri segment to delete info in my database:
anchor('site/delete_note/'.$row->id, 'Delete')
Model:
function delete_note()
{
$this->db->where('id', $this->uri->segment(3));
$this->db->delete('note');
}
It works fine, but I want to do the same for updating my info and can't get it work
So this is link in view:
anchor('site/edit_note/'.$row->id, 'Edit')
My controller:
function edit_note()
{
$note_id = $this->uri->segment(3);
$data['main_content'] = 'edit_note';
$this->load->view('includes/template', $data);
$this->load->library('form_validation');
$this->form_validation->set_rules('content', 'Message', 'trim|required');
if($this->form_validation->run() == TRUE)
{
$this->load->model('Note_model');
$this->Note_model->edit_note($note_id);
redirect('site/members_area');
}
}
My model:
function edit_note($note_id)
{
$content = $this->input->post('content');
$data = array('content' => $content);
$this->db->where('id', $note_id);
$this->db->update('note', $data);
}
My view of edit_note:
<?php
echo form_open('site/edit_note');
echo form_textarea('content', set_value('content', 'Your message'));
echo form_submit('submit', 'Change');
echo anchor('site/members_area', 'Cancel');
echo validation_errors('<p class="error">'); ?>
Edit doesn't work as delete, when i am trying to get segment directly in edit model, as I used in delete model.
If I set $note_id to a number in my controller, instead of this '$this->uri->segment(3)', it updates my database. But if I use getting segment it doesn't work. I thought uri segments are available in controller as in model, but there is something I don't know.
Better yet, instead of manually reading the IDs via the segments, you could change your functions to be:
function delete_note($note_id)
and
function edit_note($note_id)
And remove the $note_id = $this->uri->segment(3); lines.
And as silly as it'll sound, the generated URL is definitely correct, right?
And last question, have you done anything with routes?
Edit
I've also noticed that in edit, you use this in your form:
echo form_open('site/edit_note');
So when the form submits, the URL it submits to is site/edit_note instead of site/edit_note/{SOME_ID}. So once you make your changes, and the form submits, there won't be a 3rd URL segment!
Well there are some logical errors in your code.
function edit_note()
{
$note_id = $this->uri->segment(3);
$data['main_content'] = 'edit_note';
$this->load->view('includes/template', $data);
//// What is the use of loadig a view when you are editing
$this->load->library('form_validation');
$this->form_validation->set_rules('content', 'Message', 'trim|required');
if($this->form_validation->run() == TRUE)
{
$this->load->model('Note_model');
$this->Note_model->edit_note($note_id);
redirect('site/members_area');
}
}
Instead do it like this
function edit_note()
{
if($this->input->post()){
$this->load->library('form_validation');
$this->form_validation->set_rules('content', 'Message', 'trim|required');
if($this->form_validation->run() == TRUE)
{
$this->load->model('Note_model');
$this->Note_model->edit_note($note_id);
redirect('site/members_area');
}
}else{
$note_id = $this->uri->segment(3);
$data['main_content'] = 'edit_note';
$this->load->view('includes/template', $data);
}
}
MOST IMPORTANT
And the other thing you should note that you are using anchor to access edit note but not actually submitting a form so it is not getting any post data to update.
In my view it's a 'bad' approach to use uri segments in your models... you should pass an id as a parameter from your controller functions ..
function delete_note()
{
$this->db->where('id', $this->uri->segment(3));
$this->db->delete('note');
}
What if you want to re-use this delete method? e.g. deleting notes from an admin panel, via a cron job etc then the above relies upon the uri segment and you will need to create additional delete methods to do the job. Also, if you were to continue with the same you don't even need a model then .. just call these lines in your controllers if you know what I mean ...
$this->db->where('id', $this->uri->segment(3));
$this->db->delete('note');
so best is to change it to similar to your edit_note() model function.
I want to create a callback function that is used during validation to check if the username / email address is already in the database... problem is I just cant seem to get it working
So this is the callback function:
function callback_username_available($username)
{
if($this->user_model->username_available($username))
{
return TRUE;
}
else
{
$this->form_validation->set_message('username_available', 'ERROR');
return FALSE;
}
}
And this is the validation logic :
// setup form validation rules
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'username', 'callback_username_available');
if($this->form_validation->run() == FALSE)
{
// validation errors
}
else
{
// no validation errors
}
I have been at this for hours and have no idea what i'm doing wrong... both functions are in the same controller and all other standard validation rules work just fine.
Even when i set the callback function to just return FALSE, it still validates the username.
Any ideas guys... its driving me up the wall at the moment :S
to invoke a callback in CI you don't need to name the function " callback_ my_function" - this it would appear is automatically appended.
this should work:
function username_available($username)
{
if($this->user_model->username_available($username))
{
return TRUE;
}
else
{
$this->form_validation->set_message('username_available', 'ERROR');
return FALSE;
}
}
// set the rule
$this->form_validation->set_rules('username', 'Username', 'callback_username_available');
// lets do this ~
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
$this->load->view('formsuccess');
}
to clarify by calling your function "callback_username_available", CI is attempting to find
callback_callback_username_available() which of course doesn't exist.
// setup form validation rules
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'username', 'callback_callback_username_available');
if($this->form_validation->run() == FALSE)
{
// validation errors
}
else
{
// no validation errors
}