adonisjs : multiple checkboxes in a form give results of different types : string if only one checked, array if several cheched

16 views Asked by At

I"m using Adonisjs V6 with Edge.

I have a from with several checkboxes, one per item the controller give to the view. items are displayed in a table.

The controller which get datas from database and shoot them to the viewer :

  async returnList({ view }: HttpContext) {
    const usersBooks = await db
      .from('loans')
      .join('users', 'user_id', '=', 'users.id')
      .whereNull('realreturndate')
      .orderBy('users.lastname')
      .join('books', 'books.id', '=', 'loans.book_id')
      .select('loans.id as loanId')
      .select('users.id as userId')
      .select('users.firstname')
      .select('users.lastname')
      .select('books.id as bookId')
      .select('books.title')
      .select('loans.loandate')
      .select('loans.returndate')

    return view.render('pages/loan/return', { usersBooks })
  }

The viewer which build the form :

 <form class="box" autocomplete="off" action="/return" method="POST">
    {{ csrfField() }}
    <div class="container is-fluid">
      <div class="columns is-centered">
        <div class="column is-half">
        <h1 class="title is-1 has-text-link" style="text-align: center">Retour d'un livre</h1>
            <table class="table is-striped">
              <thead>
                <tr>
                  <th>[X]</th>
                  <th><abbr title="lastname">Nom</abbr></th>
                  <th><abbr title="firstname">Prénom</abbr></th>
                  <th><abbr title="title">Titre du livre</abbr></th>
                  <th><abbr title="loandate">Date prêt</abbr></th>
                  <th><abbr title="returndate">Date retour prévu</abbr></th>
                </tr>
              </thead>
              <tbody>
              @each(loan in usersBooks)
              <tr>
                <td><input type="checkbox" name="loanid" value={{loan.loanId}}></td>
                <td>{{loan.firstname}}</td>
                <td>{{loan.lastname}}</td>
                <td>{{loan.title}}</td>
                <td>{{loan.loandate}}</td>
                <td>{{loan.returndate}}</td>
              </tr>
              @end
          
            </tbody>
          </table>
        </div>
      </div>
    </div>

If one checkbox is checked, the result is a string : { loanid: '8' }

If several checkboxes are checked, the result is an array : { loanid: [ '8', '9' ] }

The question is : how can the called controller accept the form result ?

  • test the type of the result ? I don't know how to do this
  • can I test de number of values ? I don't know
  • Other ideas ?

Thank you for your expertise

1

There are 1 answers

0
Finelizzyx On

Goy it ! The object Array and its function isArray is my friend :

async return({ request, session, response }: HttpContext) {
    const payload = request.body()

const loanid = Array.isArray(payload.loanid) ? payload.loanid : payload.loanid.split(' ')

Thanx to all !