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.

Use the same safecraker form for adding and editing content?

September 11, 2012 4:42pm

Subscribe [3]
  • #1 / Sep 11, 2012 4:42pm

    T Adams

    36 posts

    I am new to expressionengine, so if the answer to this question is obvious… apologies 😊. However I cannot seem to find a definite answer to this question. is it possible to use the same form for submitting new entries and editing existing ones.

    I have created a safe cracker form which seems to work for submitting recipes. However, i cannot seem to get the form to edit recipes :(

    Here is the code I am using

    {exp:safecracker channel="recipes" return="recipes/view/URL_TITLE"}
      
            <label for="name">Title: <span class="required">*</span></label>  
            <input type="text" id="name" name="title" value="{title}" placeholder="John Doe" required="required" autofocus="autofocus" />
    
            <label for="message">Recipe Description: <span class="required">*</span></label>  
            {field:recipe_description}
            
            <label for="categories">Categories</label>
            <select name="category[]" id="categories" size="4" multiple="multiple">
              {categories group_id="1"}
                <option value="{category_id}"{selected}>{category_name}</option>
              {/categories}
            </select>
    
            <label for="categories">Categories</label>
            <select name="category[]" id="categories" size="4" multiple="multiple">
              {categories group_id="2"}
                <option value="{category_id}"{selected}>{category_name}</option>
              {/categories}
            </select>
            
            <label for="prep-time">Prep Time: <span class="required">*</span></label>  
            {field:prep_time}  
              
            <label for="cook-time">Cook Time: </label>  
            {field:cook_time}  
            
            <label for="servings">Servings: </label>  
            {field:servings}
            
            <label for="calorie-count">Calorie Count: </label>  
            {field:calorie_count}
            
            <label for="difficulty">Difficulty: </label>  
            <select id="difficulty" name="difficulty">
             {options:difficulty}
              <option value="{option_value}"{selected}>{option_name}</option>
             {/options:difficulty}
            </select> 
            
            <label for="status">Status</label>
            <select name="status" id="status">
              {statuses}
                <option value="{status}"{selected}>{status}</option>
              {/statuses}
            </select>
    
            <label for="recipe-pics">Recipe Pictures: <span class="required">*</span></label>
            {field:recipe_pictures}
            
    
            <label for="ingredients">Ingredients: <span class="required">*</span></label>
            {field:recipe_ingredients}
            <label for="prep-instruct">Preparation Instructions: <span class="required">*</span></label>  
            {field:preparation_instructions} 
            
            <label for="recipe-pics">Recipe tags: <span class="required">*</span></label>
            {field:recipe_tags}
    
            <input type="submit" value="Holla!" id="submit-button" />  
            <span class="required">*</span> indicates a required field   
          {/exp:safecracker}

    and this is the code I am using to link to the edit template

    <a href="http://{path=recipes/edit/{segment_3}}">Edit your recipe!</a>

    Any help will be greatly appreciated.

    T.

  • #2 / Sep 11, 2012 6:42pm

    mark186282

    290 posts

    the key is to pass it an entry_id or url_title.  If the entry_id is given, it knows that it has to edit that entry, and it can’t possibly be entering a new one.

    example (using entry_id):

    your url might be: http://www.yoursite.com/recipies/edit/123

    {exp:safecracker
         channel="recipes"
         return="recipes/view/URL_TITLE"
         entry_id='{segment_3}'
    }
    ...

     

  • #3 / Sep 12, 2012 4:27am

    T Adams

    36 posts

    Ok I added the entry_id=”{segment_3}” and when I try to edit the entry, i get the following errors

    The form you submitted contained the following errors
    You must specify a valid entry to edit.

    The edit url looks to be the right one, http://www.mysite.com/recipes/edit/recipe_name

    not sure wht it does not think the entry is valid.

  • #4 / Sep 12, 2012 11:27am

    mark186282

    290 posts

    if the recipe_name is the url_title, then you must use that method instead of entry_id

    url_title method:
    http://ellislab.com/expressionengine/user-guide/modules/safecracker/index.html#id3

    entry_id method:
    http://ellislab.com/expressionengine/user-guide/modules/safecracker/index.html#entry-id

  • #5 / Sep 12, 2012 11:31am

    T Adams

    36 posts

    Thanks Mark, it works perfectly now.

    😊

  • #6 / Sep 12, 2012 11:42am

    mark186282

    290 posts

    excellent!

    oh, and welcome to the forums!  😊

  • #7 / Sep 12, 2012 11:51am

    T Adams

    36 posts

    Thanks, loving EE and the community so far 😊

  • #8 / Sep 12, 2012 4:48pm

    Shane Eckert

    7174 posts

    Hey there T Adams,

    AWESOME! Gladd to see that Mark was able to help you out here. Welcome to the forums!! It’s good to have you here.

    Before I close this thread off, do you need anything else?

    Cheers,

  • #9 / Sep 12, 2012 6:59pm

    T Adams

    36 posts

    Thanks Shane,

    I’m good for now 😊

    T.

  • #10 / Sep 13, 2012 5:09pm

    Shane Eckert

    7174 posts

    Hey T Adams,

    Awesome! glad to hear it!

    If you need anything else, please just let me know by opening a new thread.

    Cheers,

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

ExpressionEngine News!

#eecms, #events, #releases