# Edit toStructure with a form on the front

**URL:** <https://forum.getkirby.com/t/edit-tostructure-with-a-form-on-the-front/18595>\
**Category:** Questions\
**Tags:** v3\
**Created:** [June 23, 2020, 9:36pm UTC](https://forum.getkirby.com/t/edit-tostructure-with-a-form-on-the-front/18595 "2020-06-23T21:36:49Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mickaeldurand](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/mickaeldurand/32/8022_2.png) [@mickaeldurand](https://forum.getkirby.com/u/mickaeldurand)\
**Post date:** [June 23, 2020, 9:36pm UTC](https://forum.getkirby.com/t/edit-tostructure-with-a-form-on-the-front/18595/1 "2020-06-23T21:36:49Z")

</div>

Hello,

I would like to create a form which makes it possible to update ToStructure fields (more data) by users on the front end.

I have already managed to do it for simple fields:

> [@Users updating their own data from the frontend](https://forum.getkirby.com/t/users-updating-their-own-data-from-the-frontend/16716):
>
> Hey there! I’m trying to implement a small form to allow frontend users of a page to update part of their own user data. \<?php return function ($kirby) { $error = false; $success =false; $message = ""; if ($kirby-\>request()-\>is('POST') && get('update')) { //I've left out all the validation here to keep the example brief. try { $kirby-\>user()-\>update(['name' =\> get('name'), 'email' =\> get('email')]); …

But this is more complex.

Is it possible ?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [June 23, 2020, 10:32pm UTC](https://forum.getkirby.com/t/edit-tostructure-with-a-form-on-the-front/18595/2 "2020-06-23T22:32:31Z")

</div>

> [@mickaeldurand](#):
>
> Is it possible ?

Of course, see example here:

> [@Prevent a user to create two identical items in a structure field](https://forum.getkirby.com/t/prevent-a-user-to-create-two-identical-items-in-a-structure-field/16511/19):
>
> If I change 'page.update:before' =\> function ($page, $values, $strings) { to 'page.update:after' =\> function ($page, $values, $strings) { I get this error from panel: Exception: ArgumentCountError Too few arguments to function Kirby\Cms\App::{closure}(), 2 passed and exactly 3 expected. And If I change to: 'page.update:after' =\> function ($page) { Nothing happen on save…

---

<div class="post-metadata">

**Author:** ![mickaeldurand](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/mickaeldurand/32/8022_2.png) [@mickaeldurand](https://forum.getkirby.com/u/mickaeldurand)\
**Post date:** [June 25, 2020, 2:52pm UTC](https://forum.getkirby.com/t/edit-tostructure-with-a-form-on-the-front/18595/3 "2020-06-25T14:52:31Z")

</div>

Hello,

Thank you for your reply.

I am testing but it still seems quite complex.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [June 25, 2020, 3:12pm UTC](https://forum.getkirby.com/t/edit-tostructure-with-a-form-on-the-front/18595/4 "2020-06-25T15:12:27Z")

</div>

You don’t need the hook, only the `addToStructure()` method…

---

<div class="post-metadata">

**Author:** ![mickaeldurand](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/mickaeldurand/32/8022_2.png) [@mickaeldurand](https://forum.getkirby.com/u/mickaeldurand)\
**Post date:** [June 26, 2020, 7:06am UTC](https://forum.getkirby.com/t/edit-tostructure-with-a-form-on-the-front/18595/5 "2020-06-26T07:06:57Z")

</div>

Thanks texnixe,

With the help of my brother who is better at PHP we were able to manage the addition (here I had managed myself cool) and modify then delete.

This post was also interesting: [Add to structured data remove from structured data from the front end](https://forum.getkirby.com/t/add-to-structured-data-remove-from-structured-data-from-the-front-end/10154)

The question that arises now is what is the best way to manage security because currently we are creating an input field in hidden which displays the line id.

```
<div class="table-responsive">
    <table class="table table-striped">
        <thead>
            <tr>
                <th scope="col">Date</th>
                <th scope="col">Description</th>
                <th scope="col">Modification</th>
            </tr>
        </thead>
        <tbody id="myTable">
            <?php foreach ($page->listing()->toStructure()->flip() as $listing): ?>
            <tr>

                <td>
                    <?= $listing->date() ?>
                </td>
                <td><?= $listing->description() ?></td>

                <td>
                    <!-- uniquement un user client verra le formulaire -->
                    <?php if (($user = $kirby->user()) && $user->role()->id() === 'client'): ?>

                    <!-- MISE A JOUR (submit_update) -->
                    <form action="" method="post">
                        <div class="form-group">
                            <input type="text" class="form-control" id="date" name="date" placeholder="date" value="<?php echo $listing->date(); ?>">
                        </div>
                        <div class="form-group">
                            <input type="text" class="form-control" id="description" name="description" placeholder="description" value="<?php echo $listing->description(); ?>">
                        </div>
                        <input type="hidden" name="id" value="<?php echo $listing->id(); ?>">
                        <input class="btn btn-primary" type="submit" name="submit_update" value="Mise à jour">
                    </form>

                    <!-- DELETE (submit_delete) -->
                    <form action="" method="post">
                        <input type="hidden" name="id" value="<?php echo $listing->id(); ?>">

                        <input class="btn btn-primary" type="submit" name="submit_delete" value="Supprimer" onclick="return confirm('Êtes vous sur ?')">
                    </form>

                    <?php endif; ?>
                </td>
            </tr>
            <?php endforeach ?>
        </tbody>
    </table>
</div>

<!-- ADD (submit_new) -->
<form action="" method="post">
    <div class="form-group">
        <input type="text" class="form-control" id="date" name="date" placeholder="date" value="">
    </div>
    <div class="form-group">
        <input type="text" class="form-control" id="description" name="description" placeholder="description" value="">
    </div>
    <input class="btn btn-primary" type="submit" name="submit_new" value="Submit changes">
</form>

```

Our idea would be to check the role “client” of the user + his email to authorize it.

Is it clean?

Because currently all “client” could modify any field, just by modifying the entry.

Here is the code in the controllers:  
\<?php

```
// ADD (submit_new)
if(r::is('post') && get('submit_new')) {
    addToStructure(
        page()->uri(), 
        'listing', 
        array("date" => strip_tags(get('date')), 
              "description" => strip_tags(get('description'))
        )
    );
}

// ADD (submit_delete)
if(r::is('post') && get('submit_delete')) {
    $field = 'listing';
    
    $items = page()->{$field}()->yaml();
    unset($items[get('id')]);

    ///dump($items);
    
    $data = yaml::encode($items);
    try {
        page()->update([$field => $data]);
    } catch(Exception $e) {
        echo $e->getMessage();
    }
    // REFRESH PAGE
    echo "<meta http-equiv='refresh' content='0'>";
}

// UPDATE (submit_update)
if(r::is('post') && get('submit_update')) {
    $field = 'listing';
    
    $items = page()->{$field}()->yaml();
    $items[get('id')] = array("date" => strip_tags(get('date')), 
              "description" => strip_tags(get('description')));

    ///dump($items);
    
    $data = yaml::encode($items);
    try {
        page()->update([$field => $data]);
    } catch(Exception $e) {
        echo $e->getMessage();
    }
    // REFRESH PAGE
    echo "<meta http-equiv='refresh' content='0'>";
}

/**
 * Add a new element to a kirby structure field
 * @param string $page
 * @param string $field
 * @param array $data
 */
function addToStructure($page, $field, $data = array()){  
    // REFRESH PAGE
    echo "<meta http-equiv='refresh' content='0'>";

  $fieldData = page($page)->$field()->yaml();
  $fieldData[] = $data;
  $fieldData = yaml::encode($fieldData);
  try {
    page($page)->update(array($field => $fieldData));
    return true;
  } catch(Exception $e) {
    return $e->getMessage();
  }
}
```

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [June 26, 2020, 7:15am UTC](https://forum.getkirby.com/t/edit-tostructure-with-a-form-on-the-front/18595/6 "2020-06-26T07:15:11Z")

</div>

If only a given user should be able to submit this form, then yes, you would have to verify the user.

Also, I don’t quite understand the purpose of the id in the hidden field. All forms should have a CSRF token instead that is than verified in the controller.

> **[csrf()](https://getkirby.com/docs/reference/templates/helpers/csrf)**
>
> Checks / returns a CSRF token

---

<div class="post-metadata">

**Author:** ![mickaeldurand](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/mickaeldurand/32/8022_2.png) [@mickaeldurand](https://forum.getkirby.com/u/mickaeldurand)\
**Post date:** [June 26, 2020, 7:18am UTC](https://forum.getkirby.com/t/edit-tostructure-with-a-form-on-the-front/18595/7 "2020-06-26T07:18:13Z")

</div>

Hello,

thank you for your very quick response, we will look.

The id was to determine the row of the table and be able to delete. As it is a structural field it seemed the right solution.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [June 26, 2020, 7:24am UTC](https://forum.getkirby.com/t/edit-tostructure-with-a-form-on-the-front/18595/8 "2020-06-26T07:24:17Z")

</div>

> [@mickaeldurand](#):
>
> The id was to determine the row of the table and be able to delete. As it is a structural field it seemed the right solution.

Ah, yes, that makes sense.

---

<div class="post-metadata">

**Author:** ![mickaeldurand](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/mickaeldurand/32/8022_2.png) [@mickaeldurand](https://forum.getkirby.com/u/mickaeldurand)\
**Post date:** [July 3, 2020, 12:33pm UTC](https://forum.getkirby.com/t/edit-tostructure-with-a-form-on-the-front/18595/9 "2020-07-03T12:33:06Z")

</div>

Again thank you texnixe for the answer.

I haven’t had much time to continue my development and I haven’t yet implemented csrf ().

I build a cycling site so that members can add information only on the front side. It allows me to learn the roles, the front and I will also immerse myself in languages.

Cookbooks are great on Getkirby but there aren’t that many to do that kind of thing on the front end. An upgrade ?

To share, this is what it can give as a presentation on the front side:

```
<form action="" method="post">
                                <!-- Button modalUpdate -->
                                <button type="button" class="btn btn-sm btn-primary" data-toggle="modal" data-target="#modalUpdate0">
                                    Modifier la course
                                </button>
                                <input type="hidden" name="id" value="0">

                                <input class="btn btn-sm btn-primary valid" type="submit" name="submit_delete" style="font-family: FontAwesome" value="" onclick="return confirm('Êtes vous sûr de vouloir supprimer la course FSGT 4 et UFOLEP 3 ?')">
                            </form>

```

I use the id in a hidden field to modify the info:  
`<input type="hidden" name="id" value="0">`

Front side for presentation inspirations:

 ![Capture d’écran 2020-07-03 à 14.22.32](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/2X/b/b9befbe3927702ceeff25ecd8555f04cde112004.png)

 ![Capture d’écran 2020-07-03 à 14.28.28](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/2X/4/488f3ba0fd6cac5dd9712e5bcef16211cc4c08d7.png)
