How to forward to a form when a required field isn't filled?

Hi,

Don't let you fool by the title and answer something like forward(REFERER), there is something more ;).

Here is the problem:

  1. - Someone submits a new object with a form
  2. - But unfortunately, it's missing a required field
  3. - So, what I want is that as it's an error, I want that the form comes back with the error AND with the data previously entered - I don't want it erased like the forward(referer) does.

 

So does someone have a better idea than saving that object and forwarding to the edit.php file?

 

I've tried with saving the data in a stdclass and then doing the usual elgg_view, page_draw but it doesn't want to do that, it's sending me to the homepage.

 

Thanks for your explanation because I'm tired to look through the search function and through search engines.

 

Vic

  • In 1.8 there is a built in system for it called "Sticky Forms"

    Info on that can be found here: http://blog.elgg.org/pg/blog/cash/read/169/elgg-18-sticky-forms

     

    For 1.7 there's nothing standard, but it's very easy to do using session variables.

    In your action where you're processing the inputs, set them as session variables like this:

    $city = $_SESSION['form_name']['city'] = get_input('city');

    This way you can do your sanity checks, and if anything fails send them back with

    forward(REFERRER);

    and the session will keep the data.

    If the data all passes, at the end of your action before sending them away you just unset the session variable (just the part you set, not the entire session!!!)

    unset($_SESSION['form_name']);

     

    On your form you will have inputs, before calling the view (if using views to generate the inputs) or displaying the html determine what the default value will be:

    $default = <default value if you're coming to the form for the first time>;

    if(!empty($_SESSION['form_name']['city']){

          $default = $_SESSION['form_name']['city'];

    }

    elgg_view('input/text', array('internalname' => "City", 'value' => $default));

    //and then unset the session again so if they leave the page it's not put in next time

    unset($_SESSION['form_name']['city']);

     

    I hope that helps.

     

  • Thanks, I'll try that solution I'm wondering why I didn't think about the sessions but it's probably because when I learned PHP, the guy never spoke about them.