Codeigniter form->run() equivalent to form_validation->run()? - php

I'm relatively new to CodeIgniter and this may be an obvious answer, but I am unable to find anything in the documentation.
I know how to use form_validation->run() to validate a form and run some code when it is submitted successfully. What if I wanted to do something with a form that did not require any sort of validation? Is there a form->run() equivalent that returns true when a user submits the form? Something like:
page.php
public function update($id)
{
$this->load->helper('form');
if($this->form->run())
{
// do some stuff after user has submitted form
}
}

I don't think there exists such method, but you can do it manually
For example:
if ( ! isset($_POST['something'])) // OR $this->input->post('something');
{
return false //maybe?
}
else
{
//$something = $_POST['something'];
return true //maybe?
}

A statement like this should work. You just need to check if any post data exists.
if ($_POST)
{
// do something
}

Related

Same function for jquery and php validation

So I have this problem. I'm doing a server side validation and a jquery validation.
In server side validation what I do is to use codeigniter's form_validation library, more specifically:
$this->form_validation->set_rules('documentn', 'Passport number', 'required|min_length[7]|max_length[20]|is_natural|callback_checkDocAndUser');
which needs a return true or return false.
I have this user edit form, to change user data. But there are some restrictions... user when stored in database has a unique passport number. I need to be able to change this passport number if it's wrong... but passport numbers should not be repeated on the database.
This is the php function that is called from callback_checkDocAndUser :
public function checkDocAndUser(){
if ((isset($_POST['documentn'])) && (isset($_POST['id']))) {
$dn = UserManager::getInstance()->checkUserDocument($_POST['documentn'],$_POST['id']);
if ($dn) {
//passport belongs to the user
echo "true";
// return true;
}else{
//does the passport entered belong to another user?
$exists = UserManager::getInstance()->getByDocument($_POST['documentn']);
if (!$exists) {
//passport belongs to another user
echo "true";
// return true;
}else{
//passport number is free to use
echo "false";
// return false;
}
}
}
}
As you can see I put some "echo" in the functions. This is because I want to use the same function for a jQuery validation (which needs echo, doesn't work with "return").
documentn: {
required: true,
minlength: 7,
maxlength: 20,
remote: {
url: '/admin/checkDocAndUser',
type: 'POST',
data: {
id: function(){
return $('#id').val();
}
}
}
},
So how can I use the same function for both kind of validations...? is there a way to make jquery function receive a return..or codeigniter's function to receive an echo?
I do the same thing in my CodeIgniter projects and the solution is quite simple. The following answer is generically named where you only need to add your validation logic.
This answer follows the DRY principle where your validation code is not repeated, as well as CodeIgniter structure.
This MODEL does the actual validation for both CodeIgniter (server-side) validation and jQuery Validate (client-side) remote...
// file name 'models/demo_model.php'
class Demo_model extends CI_Model {
public function check_demo($params)
{
// insert your validation logic here...
// the entirety of your validation logic, check the DB, etc.
// if it passes validation
return TRUE;
// if it fails validation
return FALSE;
}
}
This CONTROLLER is only called by client-side remote for jQuery Validation...
// file name 'controllers/demo.php'
class Demo extends CI_Controller {
public function remote_demo($params = FALSE)
{
// call the Model to do the actual validation
$valid = $this->demo_model->check_demo($params);
if ($valid)
{
echo 'true'; // passes validation
}
else
{
echo 'false'; // fails validation
}
}
}
This LIBRARY is only used for server-side validation by CodeIgniter...
// file name 'libraries/MY_Form_validation.php'
class MY_Form_validation extends CI_Form_validation {
public function __construct()
{
parent::__construct();
$this->ci =& get_instance();
}
public function demo_check($params)
{
$this->ci->form_validation->set_message('demo_check', 'The %s is incorrect.');
// call the Model to do the actual validation
return $this->ci->demo_model->check_demo($params);
}
}
The way it's organized, you'll have ONE centrally located validation function residing with your CodeIgniter Models. I chose Models as the central location because the code is primarily interacting with the database.
The following uses the same Model function for both kinds of validation.
Server-side CodeIgniter validation: Calls the Model from MY_Form_validation and the Model will return TRUE or FALSE back to your other CodeIgniter Controllers as per your CodeIgniter validation rules.
Client-side jQuery Validate remote: Calls the Model from the Controller and the Model will return TRUE or FALSE back to the Controller. Then the Controller function will echo true or false based on this boolean response from the Model.
Validation functions always need to return a boolean value.
In your controller, try to retrieve the return value of the validation methods and echo "true" or "false" there.
Put an exit at the end of your function.
EDIT:
If you want to use the same set of validations for both client and server side validation, divide your call to two functions, one which handles client and the another which handles server. check the following code:
In you jquery function call url - admin/validate_form_client
function validate_form_client(){
$op = $this->checkDocAndUser($_POST['documentn'],$_POST['id']);
echo $op;
exit;
}
function validate_form_server(){
if ((isset($_POST['documentn'])) && (isset($_POST['id']))) {
return $this->checkDocAndUser($_POST['documentn'],$_POST['id']);
}
}
public function checkDocAndUser($documentn,$id){
$dn = UserManager::getInstance()->checkUserDocument($documentn,$id);
if ($dn) {
//id belongs to the user
return true;
}else{
//does the id entered belong to another user?
$exists = UserManager::getInstance()->getByDocument($documentn);
if (!$exists) {
// id number belongs to another user
return "true";
}else{
//id number is free to use
return "false";
}
}
}
}
Note: the given function names are just for example. please follow standard practice in the variable and function naming conventions.

CodeIgniter form validation using session variables

How do I get the CodeIgniter form validation to validate the $_SESSION if there is no passed form data? I tried manually setting the $_REQUEST variable, but it doesn't seem to work.
i.e. I have a function search in the controller which validates the form input passed, and either returns you to the previous page with errors, or else moves you onto the next page. But I want this function to also work if you previously filled out this page, and the info is stored in the $_SESSION variable.
function search () {
$this->load->library("form_validation");
$this->form_validation->set_rules("flightID", "Flight Time", "required|callback_validFlightID");
$this->form_validation->set_rules("time", "Flight Time", "required|callback_validFlightTime");
$this->setRequest(array("flightID", "time"));
// adding session check allows for inter-view navigation
if ($this->form_validation->run()) {
// some application logic here
$this->load->view("seats", $data);
} else {
$this->logger->log($_REQUEST, "request");
// redirect back to index
$this->index();
}
}
function setRequest () {
// make sure none of the parameters are set in the request
foreach ($vars as $k) {
if (isset($_REQUEST[$k])) {
return;
}
}
foreach ($vars as $k) {
if (isset($_SESSION[$k])) {
$_REQUEST[$k] = $_SESSION[$k];
}
}
}
You can store the form post info in a session using the following codeigniter functions
$formdata = array(
'flightID' => $this->input->post('flightID'),
'time' => $this->input->post('time')
);
$this->session->set_userdata($formdata);
and the information can be retrieved with the following
$this->session->userdata('flightID')
$this->session->userdata('time')
form_validation works directly with $_POST, so use that instead of $_REQUEST.
What you're trying to do is setting Post values manually which is not natively
supported by CodeIgniter. So what we're doing first is extending the core.
Create a new file (MY_Input.php) and paste the following contents into it:
class MY_Input extends CI_Input{
function set_post($key, $value)
{
$_POST[$key] = $value;
}
}
That's a very basic implementation of your purpose but it's enough to test around. You might want to extend it to make it fit your needs (f.e. allowing the input of arrays).
So now. In your controller you can check if something has been posted by a user. If not you'll be just setting the post variable manually with your new method of the Input class.
class Some_Controller extends CI_Controller{
public function index()
{
// The user hasn't filled out a field?
if(!$this->input->post('a_key'))
{
// Let's set the postfield to the value of a session key
$this->input->set_post('a_key', $this->session->userdata('mystoredkey'));
}
}
}
After having set your postfield manually, it can be handled by the form validation library as it is meant to be.
That should be your way to go :)
You can really do some pretty things if you're not afraid of hacking the core. Many people are, don't be one of them!
Happy coding

Codeigniter Ocular and Form Validation

New to CodeIgniter and new to Ocular so bear with me.
I used to code in the following way when running form validation (where the index() method contains the form loading code):
if ($this->form_validation->run() == FALSE)
{
$this->index();
}
else
{
$this->load->view('view_name', $data);
}
However I'm now trying to use the Ocular Template Library and the above code no longer works see example below:
if ($this->form_validation->run() == FALSE)
{
$this->index();
}
else
{
$this->template->set($data);
$this->template->render();
}
The above code does not run through the index method as it used to without Ocular and I was wondering what the best practice is to correct this or even if my previous code was best practice?
Regards,
Numan1617
Sometimes it's hard to determine best practice with Codeigniter because it's convention-less nature, so all I can really tell you is how I've found best in my experience...
I assume you're form view is being served up via index() and then you're submitting your form to this (seperate) controller function that validates and processes the form data, and re-displays the view with errors if there was a problem...
I would clean this up by consolidating it all into a single function...
public function form()
{
//if this is a post request
if ($_POST)
{
//check the validation
if ($this->form_validation->run())
{
//process it
//and then redirect if you want to send to a "success" page
redirect('uri/to/success');
}
else
{
//load up $data values to re-display form
}
}
//load up any $data values needed for standard view
$this->load->view('view', $data);
// or $this->template stuff...
}
It always seemed to me to be a bad route to go down calling controller functions internally, ex. $this->index()

Passing values from jQuery to PHP

Here's the situation:
I'm using jquery.validate.js to validate my forms, but I need to add a server-side validation layer. jQuery Validate uses class names within form-elements to determine what it should do -- I want to fetch those class names into PHP so that I can perform the same validations on the server-side.
I'm hoping to do something like this:
foreach($_POST as $key=>$value) {
$class=array(...// get this #key element class name list with jQuery
$error=...// based on the class names, perform specific PHP validations
if($error=="")
return true;
else
return false;
}
Question is, can I do this? Can I fetch element class names w/ jQuery and pass them to PHP?
You could get jQuery to send the class names along with the form - HOWEVER this leads to serious security issues. Seeing as everything posted to your server is really done by your users browsers, your users will have the ability to then change your validation rules and as such bypass them, and that would make your entire purpose here useless. I would instead suggest the following.
Make a function in php to take in the name of a form element and return appropriate class names. When generating each form element for the form, call this function to get the class names (and then applying the jQuery validation). When you read your form in your PHP code - use the same function to get the class names and then you know which validation rules to apply in your server side php code.
Hope it made sense.
Never trust the client(browser)!
You should never allow anything on the client to dictate to the server how to test the validity of posted information.
This is a very bad idea.
client-side:
function validate()
{
var params = {
var1: {key:$('#field1').get(0).className, value:$('#field1').get(0).value},
var2: {key:$('#field2').get(0).className, value:$('#field2').get(0).value},
...
}
$.post('http://host/validate_script.php', params, onValidate);
}
function onValidate(data)
{
alert(data);
}
hope this will help... sure it's a simple way and you can do it more formalized.
serializeArray will convert your form data to JSON:
var json = $('#formid').serializeArray();
Typically you can send the entire JSON string to the server which can take it from there.
As mentioned above, you can use AJAX and JSON to pass the values to PHP. This will, however, not provide more secure validation than your regular JS validation (since your PHP will still depend on your JS)
If you choose to use this method, here are some improvements to the script provided previously by Evgeniy Savichev
<script type="text/javascript">
params = {
elementName : {
className : $('elementId').attr('class'),
elementValue : $('elemenetId').val()
},
anotherElement : {
//etc
}
}
$.post('http://host/validate_script.php', params, onValidate);
function onValidate(data)
{
alert(data);
}
</script>
However a better solution is to automatically generate and validate your form elements. The Zend Framework has a great class for doing so. I've included a simplified version of how something could look in case you decide to write your own script.
I hope this can be of some help to you
Wim
$elements = array(
'email-field' => array('email', 'required'),
'integer' => array('integer')
);
if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
$error = false;
foreach($elements as $elementName => $validators) {
if (array_key_exists($elementName, $_POST)) {
foreach ($elements[$elementName] as $validator ) {
switch($validator) {
case 'email':
if (filter_input(FILTER_VALIDATE_EMAIL, $elementValue)) {
$error = true;
}
break;
case 'integer':
// etc
break;
default :
break;
}
}
} else {
if ( in_array('required', $validators) ) {
$error = true;
}
}
}
if ( $error ) {
// etc
}
}
?>

Missing Parameter in PHP $_POST Function

I am using CodeIgniter to pass some parameters to my PHP page through $_POST request, and in the PHP page I am reading.
$foo = $this->input->post('myParam');
If the myParam parameter is present in the $_POST request, then $foo will be assigned the myParam value. How do I check if myParam is not passed in the $_POST request?
I Googled 'codeigniter input post'.
First result is this.
From that document:
$this->input->post('some_data');
The function returns FALSE (boolean) if the item you are attempting to
retrieve does not exist.
So you need to do:
if ($foo===false) {
// do something if it's not set
}
I think the best way to do this would be to use the form validation class to do pre-processing on your data. This is documented here.
You'd do something like:
function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('myParam', 'myParam', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
$this->load->view('formsuccess');
}
}
If your validation fails it will send you back to the form and you'll have to re-populate it with data, there is a way to do this (see the doc). If it passes you can then be sure that $this->input->post('myParam'); will return a value.

Categories