Welcome Guest, Not a member yet? Register   Sign In
How to implement JS validations through CodeIgniter
#1

[eluser]Sumon[/eluser]
Hello all,

I am very new in CodeIgnIter world. But within this time i love CI very much. I am used to validate a form like this....

JAVASCRIPT FUNCTION
--------------------------------------------------------------------
function Validation()
{
if(this.frmRegistration.txtFullName.value=="")
{
alert("Please enter full name.");
this.frmRegistration.txtFullName.focus();
return false;
}
return true;
}
--------------------------------------------------------------------
How shall i implement the same in CI. Please help me to get out.....

thanks,
Sumon
#2

[eluser]xwero[/eluser]
You have to use the validation library to do this
Code:
$rules['txtFullName'] = 'required';
$this->validation->set_rules($rules);
$fields['txtFullName'] = 'full name';
$this->validation->set_fields($fields);
If there is an error you get a message like 'Full name is a required field'. You can change the message in the language file but the message is for all the required rules.
#3

[eluser]nmweb[/eluser]
Are you looking for something like this: http://h1368840.stratoserver.net/2008/02...alidation/
So have the library generate the js? Otherwise you still have to write the js yourself.
#4

[eluser]adamp1[/eluser]
I would highly disapprove of JS validation. First off if the user doesn't have JS turned on your form won't be validated. Also they could change the JS to allow invalid input.

It would be fine if you used it in conjunction with the CI validation, but don't use it on its own.
#5

[eluser]Sumon[/eluser]
Thanks for your help. I like to know something more. Here is my files

-------------------------------------------------------
view_validate.php // inside view folder
-------------------------------------------------------
echo form_open("JsValidation/ValidateMethod");
$data = array(
'name' => 'txtFullName',
'id' => 'txtFullName',
'value' => '',
'maxlength' => '100',
'size' => '50',
'style' => 'width:50%',
);

echo form_input($data);
echo form_submit('mysubmit', 'Submit Post!');

$rules['txtFullName'] = 'required';
$this->validation->set_rules($rules);
$fields['txtFullName'] = 'full name';
$this->validation->set_fields($fields);
-------------------------------------------------------
And here is my controller code
-------------------------------------------------------
class JsValidation extends Controller
{
function JsValidation()
{
parent::Controller();
$this->load->helper("form");
$this->load->library('validation');
}
function Validate()
{
$this->load->view("view_validate");
}
}
-------------------------------------------------------
Now my question is how to set and display error message.
Anything to change in configuration file?
Please help me to get out.
#6

[eluser]xwero[/eluser]
You should add the rules and fields to the controller.
To display the error all you have to do is
Code:
<?php echo $this->validation->error_string; ?>
#7

[eluser]Sumon[/eluser]
I shall be highly happy if someone send me a complete example please.

thanks,
Sumon
#8

[eluser]xwero[/eluser]
Code:
/*-------------------------------------------------------
view_validate.php // inside view folder
-------------------------------------------------------*/
echo form_open("JsValidation/form");
echo $this->validation->error_string;
$data = array(
              'name'        => 'txtFullName',
              'id'          => 'txtFullName',
              'value'       => '',
              'maxlength'   => '100',
              'size'        => '50',
              'style'       => 'width:50%',
            );

echo form_input($data);
echo form_submit('mysubmit', 'Submit Post!');


/*-------------------------------------------------------
And here is my controller code
-------------------------------------------------------*/
class JsValidation extends Controller
{
    function JsValidation()
    {
        parent::Controller();    
        $this->load->helper("form");
        $this->load->helper("url"); // for the redirect function
        $this->load->library('validation');
        
    }

    function form()
    {
        if(count($_POST) > 0)
        {
           $rules['txtFullName'] = 'required';
           $this->validation->set_rules($rules);
           $fields['txtFullName'] = 'full name';
           $this->validation->set_fields($fields);
           if($this->validation->run() === TRUE)
           {
               redirect('JsValidation/success');
           }
        }
        $this->load->view("view_validate");
    }
    function success()
    {
        $this->load->view("view_success");
    }
}
For more examples and possibilities check the user guide.




Theme © iAndrew 2016 - Forum software by © MyBB