ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Handling form interaction

September 19, 2010 4:17pm

Subscribe [3]
  • #1 / Sep 19, 2010 4:17pm

    Avi Block

    27 posts

    One of the biggest drawbacks for me in Expression Engine 1.x was how hard and annoying it was to create a script to handle form interaction, while maintaining the EE environment in that script.
    To do this meant to create a module action, and either to “install it”, or add it to the database.
    Then the form would submit to a meaningless ACT=34, or something like that.
    Did EE2 improve this at all? Would I be able to make a codeigniter action that has access to the EE environment? Is there a better way of handling this with EE itself in EE2?

  • #2 / Sep 20, 2010 3:25pm

    Lisa Wess

    20502 posts

    Hi, Avi -

    You would still need a custom solution for your form handling. Ultimately, what you need to manage will dictate how much of an add-on or PHP you would need to meet your goals.

    Can you tell us a bit more about what you need to accomplish? What kind of form interaction do you need?

    Thank you.

  • #3 / Sep 20, 2010 3:36pm

    Avi Block

    27 posts

    Case in point:
    EE 2 gives me an API for creating entries. I would rather use that than a SAEF for a few reasons.
    1) More control over validation. I could use a framework like Zend_Form to control those forms and validation.
    2) I want errors to be displayed *on* the form, instead of displaying a box with a list of errors. Do in case of a validation error, I would have the render the template out with the errors on it.
    3) Would prefer this than to mess around with extension hooks. Those suffer from the same flaw that I’m trying to get away from.

    The cleanest way to handle this in EE 1.x is to create a module action. But I would rather post to a URL than a meaningless ACT=xxxx. Also I don’t have to add something to the database everytime I need a script which accesses the EE environment.

    Hooks have the same problem. It’s annoying to add a hook to the database everytime I need one. I just want to write code.

    I overcame this in 1.x by writing a EE which hooks on to the earliest point (session_start maybe?) and added in hooks from a config file, but obviously I can’t use that for hooks at the point.

  • #4 / Sep 20, 2010 4:00pm

    Brandon Jones

    5500 posts

    Hi Avi,

    You don’t need actions to get form data to your module. Actions are primarily designed for cases where you perform some database actions and then want to redirect the client elsewhere (think Search module).

    The alternative is to simply let your module handle the data directly. Say you have a template with your module’s tags like:

    {exp:my_mod:my_func}
       {validation_errors}
       ... forms ...
    {/exp:my_mod:my_func}

    Calling this page will execute your module’s my_func() method. Inside that method you can grab any POST/GET data sent along with the page request, typically using $this->EE->input->post() or get(), and do whatever you’d like with it. If there’s a validation error, populate the {validation_errors} tag.  No actions necessary and quite a bit of freedom depending on what you need to do.

    Hope that helps.

  • #5 / Sep 20, 2010 4:32pm

    magder

    24 posts

    I’ve done that before as well, but then you have to go through the whole process of rendering out the template, at least until you’re modules tag. This wastes resources.

    This discussion comes to mind: http://test.ical.ly/2010/09/09/why-you-should-not-handle-your-symfony-form-inside-a-component/

    Although it’s about symfony, the idea is the same. Symfony components are meant for encapsulating display logic, but some developers like to use it for also data processing (the example there is a comment form meant to be easily embedded on a page).

    The argument is the proper place for doing this would be a symfony action. The EE equivalent to that is a module action, but that’s uglier.

    In EE2, there might be an equivalent with codeigniter, which is really what my question is.

  • #6 / Sep 20, 2010 4:40pm

    Brandon Jones

    5500 posts

    magder, not sure what you mean by:

    you have to go through the whole process of rendering out the template, at least until you’re modules tag. This wastes resources.

    Your module should only be concerned with replacing its own tags, often doing nothing more at the end than:

    return $this->EE->TMPL->parse_variables($this->EE->TMPL->tagdata, $tags);

    EE takes care of the rest of the template for you.

  • #7 / Sep 20, 2010 9:19pm

    Avi Block

    27 posts

    Ostensibly, the module tags are not at the beginning of the template! There will obviously be some processing before EE gets to your module.
    Another use case, let’s say I want to return JSON for an AJAX call.
    What if I don’t want to return anything and just redirect. In that case you say use module actions? But I just want to right code. I don’t want to bother adding an entry in the database, or writing update logic for my module.

  • #8 / Sep 21, 2010 3:03am

    Brandon Jones

    5500 posts

    Ostensibly, the module tags are not at the beginning of the template! There will obviously be some processing before EE gets to your module.

    Your module’s tags need not be at the beginning of the template.

    Another use case, let’s say I want to return JSON for an AJAX call. What if I don’t want to return anything and just redirect. In that case you say use module actions?

    Not at all. This would do the trick:

    $this->EE->functions->redirect($this->EE->functions->create_url('template_group/template'));
  • #9 / Sep 21, 2010 8:29am

    Avi Block

    27 posts

    If your module tags are not at the beginning, would EE not process all the tags until that point?

  • #10 / Sep 21, 2010 8:15pm

    Brandon Jones

    5500 posts

    Yes, that’s true. You would need this to happen if you want form validation errors to appear on the same page. If there’s no error you can simply use the redirect function like above.

  • #11 / Sep 21, 2010 8:53pm

    Avi Block

    27 posts

    I would still like to have my original question. Is it possible, in EE2, to delegate a form action to codeigniter action that still maintains “knowledge”, of the EE environment (has access to the api, etc.)?

    I suppose it can be done because I’m assuming the control panel is written that way. But is there some special magical incantation required?

  • #12 / Sep 23, 2010 12:05am

    Brandon Jones

    5500 posts

    Your extension or module, whether processing the POST data “directly” or via an action ID, has access to CodeIgniter’s libraries and helpers by virtue of being an EE add-on. And it will always have access to EE’s session object as well.

    Have you looked at our Development guide? I came to EE from the CodeIgniter angle as well and have been nothing but pleased. I may still be unclear on the question, but if you find that you can’t do what you’d like with EE we’re happy to refund the purchase within 30 days.

    Hope this helps!

  • #13 / Sep 25, 2010 11:22pm

    Avi Block

    27 posts

    I think you are misunderstanding me. As I said before, I would like to stay away from an EE module for interacting with my form. I would like to use a plain old CI action in a plain old CI controller, but still have access to EE from that controller. Is that possible?

  • #14 / Sep 27, 2010 2:20pm

    Brandon Jones

    5500 posts

    Avi,

    I don’t believe that is possible. To have access to EE, which is itself a CodeIgniter app, you are going to be running inside the context of EE’s controllers. So an EE add-on would be the preferred route.

  • #15 / Sep 27, 2010 2:56pm

    Avi Block

    27 posts

    Unless I’m missing the market for Expression Engine (I thought it was marketed for developers), this is a big loss.
    What’s the gain in advertising that it’s built on CI if you can’t take advantage of it?
    Why should I choose EE over wordpress when wordpress is a lot simpler to bend to my will than EE?

    I’ve used a couple of CMS’s built on top of symfony. All of them just add a layer of content management to symfony, but when you need the bare metal, symfony is always there for you. I expected the same from EE + CI.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases