How to Create Standalone Edit Forms in ExpressionEngine 2.0

Unfortunately in the latest version of EE 2.0 (2.0.1 Build 20100121 as of this writing) the SAEF core files are incapable of supporting edit functionality... this post will show you how to correct that shortcoming (and hopefully the core hack required here will be implemented in some future version of EE).

Standalone entry forms and standalone edit forms (ambiguously referred to as SAEF) have been of critical importance to many ExpressionEngine developers in the past. The {exp:weblog:entry_form} (renamed as {exp:channel:entry_form} in EE 2.0) is what allows us to let visitors to our sites create and edit entries without having access to the EE control panel... this has led to EE being capable of housing business directories, event calendars, and other types of sites with a focus on user-generated content.

First off, the core hack:

expressionengine/modules/channel/mod.channel_standalone.php, line 177 (edit: line 227 as of EE 2.1.0). You'll need to change the following line:

$success = $this->EE->api_channel_entries->submit_new_entry($channel_id, $data);

to this:

$success = isset($data['entry_id']) ? 
  $this->EE->api_channel_entries->update_entry($data['entry_id'], $data) :
  $this->EE->api_channel_entries->submit_new_entry($channel_id, $data);

For those familiar with coding edit forms in 1.6.x that'll get EE to behave as you're accustomed to. All the same required fields apply.

For those not familiar with the coding of an edit form or those who relied on the form_helper plugin, I'll show you how to get a very basic edit form working, based on the Agile Records default site.

First you'll need a template for this edit form, let's make a template group called "admin" with a template inside of it called "edit". Where you place your edit link is outside the scope of this post but if you've followed my template naming conventions you'll need to construct a link to this template for a particular entry like so:

<a href="{title_permalink="admin/edit"}">edit this entry</a>

The url should look something like: yourdomain.com/index.php/admin/edit/welcome_to_the_example_site

Now that we've gotten to the template we need to print the form fields and fill it with data from the entry. We'll use the {exp:channel:entries} tag to pull the data, but we'll make sure that the author is the current user just to make sure they're editing something they own. In an edit form you have to set the format values for each field as well as the entry_id (so that EE knows which entry you're trying to update). To have EE create the form with the necessary hidden fields we'll wrap all of this with the {exp:channel:entry_form} tag.

{exp:channel:entry_form channel="news" return="admin/index"}
  {exp:channel:entries channel="news" author_id="CURRENT_USER" status="Open|Closed" show_expired="yes" show_future_entries="yes" use_live_url="no"}
    <input type="hidden" name="entry_id" value="{entry_id}" />
    <input type="hidden" name="url_title" value="{url_title}" />
    <input type="hidden" name="field_ft_1" value="xhtml" />
    <input type="hidden" name="field_ft_2" value="xhtml" />
    <input type="hidden" name="field_ft_3" value="none" />
    {if status == "open"}<input type="hidden" name="status" value="{status}" />{/if}

    <div>
      <p>
        <b>News Title</b><br />
        <input type="text" name="title" id="title" value="{title}" size="50" />
      </p>
    </div>

    <div>
      <p>
        <b>News Body</b><br />
        <textarea type="text" name="field_id_1" cols="50" rows="10">{field_id_1}</textarea>
      </p>
    </div>

    <div>
      <p>
        <b>News Extended</b><br />
        <textarea name="field_id_2" cols="50" rows="10">{field_id_2}</textarea>
      </p>
    </div>

    <div>
      <p><input type="submit" value="Save Entry"></p>
    </div>
  {/exp:channel:entries}
{/exp:channel:entry_form}

As an aside, the reason I used {field_id_1} instead of {news_body} within the textarea is because that gets around the xhtml formatting on the field. Otherwise EE would wrap the content of that field in <p> tags which isn't what we want while editing.

Hopefully this helps some of you early EE 2.0 adopters. I realize this isn't a comprehensive guide to edit forms (I didn't talk about categories, file uploads, etc.) but I didn't want to overwhelm anyone just trying to get the basics to work. If you have questions or feedback please use the comments!


  • Hi Ty,

    Very very kind of you to post this for everyone to see. This is definitely a major issue at the moment with the forms not working the way they used to. I know the Edit form was never a sanctioned way of working but I’ve used it so many times and never had a problem with it so it’s good that this can still be done so thanks for sharing.

    Best wishes,

    Mark

    Mark Bowen commented on
    Feb 3,2010 at 2:25PM
  • Hi Ty,

    Great work, as you know this really pulled me out of a hole! Its good that this functionality can be returned in 2.0

    Thanks Again

    David

    David Henderson commented on
    Feb 3,2010 at 2:34PM
  • Thanks for posting that Ty! You are one sharp guy..

    Jeff Lleru commented on
    Feb 3,2010 at 9:12PM
  • Your post is the first one I have seen that offers clear instructions on how to build and use an edit form. Thanks!

    Would love to also have info on how you deal with related entries in an edit form.

    icebreaker commented on
    Mar 23,2010 at 12:35AM
  • Thanks for this info Ty.  Is there any possibility of you posting a followup to this to include how to deal with custom fields that are drop-down types or checkboxes?

    Matthew Thompson commented on
    Apr 14,2010 at 9:22PM
  • Saved my bacon! Thanks for posting this up - such a shame we need to hack the core to get EE2 to support this.

    Cheers
    Tim

    Tim Jukes commented on
    Jul 5,2010 at 7:12PM
  • I’m confused - being new to EE in general, trying to make the switch to EE2 and ran into the SAEF issue.

    What about just an add (not edit) form? How is that different from what’s being done here?

    thanks!

    danielle commented on
    Jul 12,2010 at 4:02PM
  • I’m hoping that the “maintenance” expressionengine.com is currently undergoing means that the release of EE 2.1 is imminent. There are quite a few SAEF bugs that should be fixed in that release.  Hopefully the hack documented here is also incorporated so that we don’t have to mess with our core files to enable edit forms.

    There are a number of issues that I don’t expect will be resolved, however. Most notably is that EE 2 SAEFs are currently incompatible with custom fieldtypes.

    At any rate, I’m hoping that I’ll be able to devote some time to writing a new guide to SAEFs after the launch of EE 2.1.  Hopefully enough has changed that will make the information contained in this post irrelevant. smile

    Ty Wangsness commented on
    Jul 12,2010 at 4:08PM
  • Didn’t see any mention of SAEF’s in the EE 2.1 changelog - hopefully this will be coming in the next build.

    Stephen McIver commented on
    Jul 13,2010 at 9:35AM
  • It appears that the 2.1 release still requires the core hack documented in this article. The line to change is now 227 of mod.channel_standalone.php.

    Ty Wangsness commented on
    Jul 13,2010 at 9:57AM
  • thanks for sharing…  beginning my new 2.1 install and this is a must.. lets see what happens!

    dr3w commented on
    Jul 16,2010 at 8:00PM
  • Thanks for this, it’s a huge help. I’m having no problem pulling in all my custom fields (text and textarea) to the edit form. However, when I go to submit, it doesn’t save them, just the title. Is there something special I have to do to accommodate custom fields to ensure they submit?

    Chris Brauckmuller commented on
    Jul 27,2010 at 11:01AM
  • Just an update, I’ve got image uploads and checkbox fields working too. If there’s demand for a tutorial, I’d be happy to write one.

    Chris Brauckmuller commented on
    Aug 11,2010 at 9:22AM
  • I think one can say “there is always a demmand”!!!  thanks!!

    dr3w commented on
    Aug 11,2010 at 9:31AM
  • Please create a tutorial for getting uploads and checkbox fields working! Pretty please!

    Kyle commented on
    Aug 26,2010 at 12:52PM
  • I’m actually working on it as we speak. Got the first draft done and working through revisions. Should be out soon, I’ll post when it is.

    Chris Brauckmuller commented on
    Aug 26,2010 at 12:53PM
  • Can’t wait for that Chris - thanks!

    (just wanting to be notified when Chris has the tutorial completed)

    Matt Crest commented on
    Aug 26,2010 at 2:41PM
  • I have my edit form working except for one thing - it’s saving as a new entry instead of an edit. Any ideas on how to fix that?

    I’m also awaiting a tutorial on implementing checkboxes in the saef. Thanks!

    meredith commented on
    Aug 27,2010 at 11:49AM
  • @meredith: my guess is you either don’t have the core hack in place or you’re not passing the existing {entry_id} as a hidden field in the edit form.

    Ty Wangsness commented on
    Aug 27,2010 at 11:54AM
  • Regarding the 2 hidden fields:
    <input type=“hidden” name=“field_ft_1” value=“xhtml” /> 
    <input type=“hidden” name=“field_ft_2” value=“xhtml” /> 
    <input type=“hidden” name=“field_ft_3” value=“none” /> 

    I get an Error on submit:
    Unknown column ‘field_ft_1’ in ‘field list’

    IF I remove field_ft_1 I get the same error for _2 and then _3.

    This is new - it was working fine and I’m not sure what I changed about the form to make this error occur. IF I remove all 3 fields the edit for works fine.


    QUESTION - can I leave these out? I’m not sure what these fields are doing…

    dzr commented on
    Aug 31,2010 at 9:40AM
  • Any hints on how to do multi-select/categories etc. so that the values are selected?

    thanks

    danielle commented on
    Sep 14,2010 at 9:48AM
  • Thanks for sharing this workaround Ty.. it’s a bit puzzling that the official EE2 doesn’t (yet?) incorporate such a simple addition, which enables a very useful functionality.

    Matteo commented on
    Sep 30,2010 at 7:33AM
  • Hey - anyone doing SAEF in EE2 - check out this add-on: SafeCracker http://barrettnewton.com/products/safecracker/

    It takes care of the fact that EE2 doesn’t support Playa in stand alone forms. It’s also pretty rad for other stuff.

    danielle commented on
    Sep 30,2010 at 8:08AM
  • Thanks for this posting.
    Is the tutorial ready Chris? or can you sent only the code? I use solspace tag for 2.1. Is this field also editable? or must I switch to PHP outside EE.

    Marcus commented on
    Nov 28,2010 at 2:19AM

say your piece

name

name

name

Remember my personal information

Notify me of follow-up comments?

Please enter the word you see in the image below: