Welcome Guest, Not a member yet? Register   Sign In
Global form validation callbacks? Staged forms?
#1

[eluser]tomclowes[/eluser]
Three questions today Smile

1. I have a simple capcha check for usage with the form validation library/callack capabilities. I define my callack at the bottom of my controller. I want to use this callback function in all of my forms - is there a global location where I can define these functions?

2. I have a complicated 3 stage form. What is the best way of doing this with a controller?
At the moment I basically have a single function which uses lots of if statements to detect whether the form subit buttons of the respective pages have been submitted - is this the correct approach?

3. I had a break from development, came back to my code and thought WHAT ! I have commented everything well, so understand it again but it seems to me that splitting up things into M, V, and C makes it extra complicated to understand.. Views?

Cheers
#2

[eluser]WanWizard[/eluser]
1. If you need extra validation rules thoughout your application, extend the form validation library with a MY_Form_validation library, and create the methods there. You then no longer need the callback, you can define them like all other rules.
2. Use a session variable to store posted information, and an variable indicating the current form. This also allows previous/next buttons to work.
3. The MVC pattern is actually quite simple. The flow is:
- user requests a page
- controller contains the control flow for the request
- controller calls model methods to do the bulk of the processing and all I/O
- model methods return results to the controller
- controller passes them on the the view
- a view is the output of the request

So, a controller contains methods for the different tasks related to the controller. For example 'users/add', 'users/list', 'users/delete', and so on. Every method contains the control flow to deal with that request, either display an initial page or process form input.
A view contains your HTML for the result of the request. In standard CI, a view is equal to a page. You can load multiple views, which will be output in sequence. You can use a template library to structure your page, with header, sidebar, footer, etc.
#3

[eluser]tomclowes[/eluser]
Points 1 and 2 - great ! Thanks

Point 3. I apreciate that MVC is a more organized approach, and i 'get it', however would it be fair to say that from the point of view of looking at code and saying 'wait a minute, how does this work again', it is pretty complex..?

You can look at the controller and see the flow of things but you cant see the underlying programming that makes it work, nor the code which displays.

Regaring the controller, I am finding myself with an index function in each of my controllers, and nothing more.

Having /controller_name/index/whatever/vars/i/want (which i route with /controller_name/whatever/vars/i/want), I havnt seen a need for any other methods.. have i missed the point?

Thanks
#4

[eluser]tomclowes[/eluser]
Further to this, re point 2.

I have created the MY_Form_validation class, defined the function capcha check and utilized the CI superglobal.

I have changed the rules required in my Form_validation config file from 'callback_capcha_check' to simply 'capcha_check'.

None of my form validation functions are working - not even the default CI ones..

Thanks
#5

[eluser]WanWizard[/eluser]
That is the generic downside of using a framework. There is some 'magic' going on in the background.

How you organize things is entirely up to you. Some people use a single index method (or even a _remap method), and then use a large if/switch structure to process all the different paths the control flow can take. Some prefer to split it into different methods, and either use _remap() as dispatcher or call the methods from the URI. It leads to more segmented code, which is easier to maintain in the future. In your example. 'whatever' would become the method name, which means the control stucture you now have in the index method to process the 'whatever' value is no longer needed. Same for the routes.

MY_Form_validation exends Form_validation I presume? And the constructor of MY_Form_validation calls it's parent constructor?
#6

[eluser]tomclowes[/eluser]
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed.');

class MY_Form_validation extends CI_Form_validation
{

  function MY_Form_validation() {
    parent::CI_Form_validation();
  }
  
        //FUNCTION FOR CHECKING IF THE CAPCHA IS RIGHT
        function capcha_check($str)
        {
            $CI =& get_instance();

            //IF SUBMITTED VALUE IS EQUAL TO THE FLASH SESSION DATA SET
            if ($str == $CI->session->flashdata('capcha'))
            {
            
                return TRUE;
            }
            else
            {
                //SET THE ERROR MESSAGE
                $CI->form_validation->set_message('capcha_check', 'Your answer to the %s question was incorrect.');
                return FALSE;
            }
        }
        

  }

This is my code, which I am confident is correct..
I have my form valiation rules in a seperate config file.


--------------------------------------------------------------------------------------

Apologies for drilling your brains. These will be the last questions Smile - I do appreciate it.

I am working with various javascript files for my development. I have them in a folder called js in my base directory.
I am utilizing ajax for various update processes, so I also have php files in this folder with which my js communicates to update databases etc.

Am I approaching this correctly?

Assuming that I am, how can I access codeigniter functions/sessions etc from here?

For example, I have a simple JS which means onclicking a button, a value is added to the database. I want to also input the username which is stored in a codeignitor session. Is there anyway to access this from such a file in such a setup?

Likewise I want to utilize the base_url function in my Javascripts so that i dont have to continually update my paths when things are changed. Is this possible?

Thanks a lot.
#7

[eluser]WanWizard[/eluser]
Nothing wrong with this code, I agree. Should work without problems.
Does your rule include 'required' (to make sure it isn't skipped because of a form error)?

There are several approaches to the ajax question.

Separate PHP files work if you don't need access to the CI object, but not my favorite because your code is all over the place. If you need the CI object, you have to call a controller. Some use separate methods in their application controllers to deal with ajax calls (http://host/controller/ajax/var/value).

Because I use Modular CI in my projects, there is no such thing as a single controller that processes a page request. Instead, a page is build using different sections using a template system, each section being produced by a different controller. I use a specific front controller (ajax.php instead of index.php), that takes care of all ajax calls, using a URI like http://host/ajax.php/modulename/method/var/value, which calls the method method() in the controller ajax.php in the requested module. So I centralize all ajax code for a given module in a single controller per module.

My header template contains a piece of javascript that creates an "exitecms" javascript object. This object has a method called base_url() which returns the CI base_url. I call this method from other javascript components.
Something like:
Code:
var exitecms = new function() {
    this.base_url = function () {
        return "<?php echo base_url() ?>";
    };
}
#8

[eluser]tomclowes[/eluser]
Looking into the callback now,

Ok. Being new to CodeIgniter, and only really having gotten into JS for my users experience, your post went over my head. I have tried to get my head around it and understand it as follows.

I need my base_url in my javascript files. This can be done by settting it as an object (in your example).

To test this, I defined a similar 'thing' - I am unsure how what you have written is an object as I was under the impression an object was contained with {}braces. Is this not simply a function?

None the less if in my javascript files i refer to excitecms.base_url it returns
Code:
function () {
        return "<?php echo base_url() ?>";
    };
when what i want is simply
Code:
mybaseurl.com
.

Essentially i am confused as to your usage of functions as opposed to defining
Code:
var excitecms.base_url=<?php echo base_url();?>";
.

Then if i understand correctly you are saying that i could have a controller named 'ajax' with various different functions accessed by /ajax/function1/, /ajax/function2/ etc which do the processing of my JS. That way I have access to the CI object?

I'm terribly sorry if i have completely missed your point. Thank you.
#9

[eluser]tomclowes[/eluser]
With regards to extending the form_validation library.

I used the following code, which should not affect functioning in any way shape or form. It does not work. As soon as I delete the MY_Form_validation.php file, everything goes back to normal.

This (for once) doesn't seem like an error on my part - perhaps a setting?
I am completely stumped.

Code:
<?php
class MY_Form_validation extends CI_Form_validation {

    function My_Form_validation()
    {
        parent::CI_Form_validation();
    }

}

Thanks again
#10

[eluser]WanWizard[/eluser]
Like in PHP, in javascript a method is a function... I just showed you a single function as an example, it does contain a lot more functionality.
It doesn't realy matter if you do
Code:
var exitecms = new function() {
    this.base_url = function () {
        return '<?php echo base_url(); ?>';
    };
};
alert( exitecms.base_url() );
or
Code:
var exitecms = {
    base_url: function() {
        return '<?php echo base_url(); ?>';
    }
};
alert( exitecms.base_url() );
the effect is exactly the same. Then, when I need the base_url in a javascript file that is included, I just call the base_url() method of the exitecms object.

As for the extension, I've checked my Form validation extension, and I can't spot any difference between your code and mine. I'm clueless to why this would stop it from working...




Theme © iAndrew 2016 - Forum software by © MyBB