events icon indicating copy to clipboard operation
events copied to clipboard

Edit event in frontend form

Open tin-soldier opened this issue 2 years ago • 3 comments

How would you go about creating a frontend form for editing an exiting event. I'm using the below method for adding an event. I can pull in the exiting data using the event id but the form still creates a new entry rather than editing the chosen event.

I've tried adding {{ hiddenInput('entryId', entry.id) }}

  <body>
    {% set eventType = craft.events.getEventTypeById(1) %}
    <form id="form-submission">
      <input type="hidden" name="action" value="events/events/save">
      <input type="hidden" name="typeId" value="1">
      {{ csrfInput() }}

      <input type="text" name="title" value="New Event">

      {% for field in eventType.getFieldLayout().getFields() %}
          {{ field.name }}: <input type="text" name="{{ field.handle }}">
      {% endfor %}

      <input type="date" name="startDate">
      <input type="date" name="endDate">

      <input type="submit" name="" value="send">
    </form>

    <script type="text/javascript">
      $(document).ready(function(){

        $("#form-submission").on("submit", function(e){
          e.preventDefault();

          var formEl = new FormData($(this)[0]);

          $.ajax({
            url: $("input[name='action']",this).val(),
            type: 'post',
            method: "post",
            processData: false,
            contentType: false,
            data: formEl,
            complete: function(o){
              console.log("Complete");
              console.log(o);
            }
          });


        })

      })
    </script>


  </body>

Originally posted by @JoshC96 in https://github.com/verbb/events/issues/69#issuecomment-703211043

tin-soldier avatar May 03 '22 01:05 tin-soldier

So the tricky part will be replicating the ticketing table, as per the control panel, where you can add multiple ticket types and their capacities, their own fields. You'll need some JS to handle adding new ticket definitions.

The best resource would be to view-source on the event edit page in the control panel and look at the name attribute for inputs. So long as you replicate that, you'll be fine! Consult the (CP edit template)[https://github.com/verbb/events/blob/craft-3/src/templates/events/_edit.html].

But, it should like you're just missing sending the ID of the event in your form.

<input type="hidden" name="id" value="{{ event.id }}">

engram-design avatar May 06 '22 03:05 engram-design

Thanks. That put me on the right track. The field was actually:

<input type="hidden" name="eventId" value="{{ event.id }}">

tin-soldier avatar May 06 '22 03:05 tin-soldier

Ah, my mistake sorry!

engram-design avatar May 06 '22 03:05 engram-design