user3489502
user3489502

Reputation: 3611

'htmlentities() expects parameter 1 to be string, array given' error on Laravel view

I was testing the Request.php (submitting form with empty required fields) of my create view, and got the 'htmlentities() expects parameter 1 to be string, array given' error.

I know the error was generated because of the code below. This is a part of my view (calls javascript functions) used to create future appointment reminders.

I just don't see what is causing the error exactly.

<div style="display:none">    
<div id="reminderiteminitial">      
<div class="row" style="padding: 0.5em 0 0.5em 0.5em">  
      <div class="col-sm-2" >
           {!! Form::text('reminder_time[]', 30, ['class' => 'form-control', 'placeholder' => '']) !!}
      </div>
      <div class="col-sm-2" >
           {!! Form::select('reminder_timeunit[]', ['minute' => 'minutes(s)', 'hour' => 'hour(s)', 'day' => 'day(s)', 'week' => 'week(s)', 'month' => 'month(s)'], null, ['class' => 'form-control']) !!}
      </div>
      <div class="col-sm-2" >
           {!! Form::select('delivery_method[]', ['eml' => 'Email'], null, ['class' => 'form-control']) !!}
      </div>
      <div class="col-sm-4" >
           {!! Form::text('delivery_contact[]', null, ['class' => 'form-control', 'placeholder' => 'Your email address']) !!}
      </div>
      <div class="col-sm-2" >
          <input class="btn btn-default" type="button" value="Delete reminder" onClick="removeitem(this);">
      </div>

</div>
</div>
</div>

<div id="reminderitem">     
</div>

<div class="row" style="padding: 1.5em 0 2.5em 0">      
<div class="col-sm-12">         
      <input class="btn btn-default" type="button" value="Add a reminder" onClick="addreminderitem();">
</div>
</div>  

Javascript:

<script language='javascript' type='text/javascript'>

  var itemCount = 1;
  var limit     = 6;

  function addreminderitem(){

        if (itemCount == limit)  {

           alert("You have reached the maximum number of reminders.");
        }
        else  {
           var newreminderitem            = document.getElementById('reminderitem');
           var initialreminderitem_clone  = document.getElementById('reminderiteminitial').cloneNode(true);

           initialreminderitem_clone.id   = 'item_'+itemCount++;

           newreminderitem.appendChild(initialreminderitem_clone);
        }
  } 

  function removeitem(item){
        item.parentNode.parentNode.parentNode.remove(); 

        itemCount = itemCount - 1;
  }     

</script>

Whoops shows:

reminder_time   
array:1 [
  0 => "30"
]

reminder_timeunit 
array:1 [
  0 => "minute"
]

delivery_method 
array:1 [
  0 => "eml"
]

delivery_contact 
array:1 [
  0 => ""
]

Update #2

What's strange is that if I replace the following HTML forms code:

  <div class="col-sm-2" >
       {!! Form::text('reminder_time[]', 30, ['class' => 'form-control', 'placeholder' => '']) !!}
  </div>
  <div class="col-sm-2" >
       {!! Form::select('reminder_timeunit[]', ['minute' => 'minutes(s)', 'hour' => 'hour(s)', 'day' => 'day(s)', 'week' => 'week(s)', 'month' => 'month(s)'], null, ['class' => 'form-control']) !!}
  </div>
  <div class="col-sm-2" >
       {!! Form::select('delivery_method[]', ['eml' => 'Email'], null, ['class' => 'form-control']) !!}
  </div>
  <div class="col-sm-4" >
       {!! Form::text('delivery_contact[]', null, ['class' => 'form-control', 'placeholder' => 'Your email address']) !!}
  </div>
  <div class="col-sm-2" >
      <input class="btn btn-default" type="button" value="Delete reminder" onClick="removeitem(this);">
  </div>

with the html it generates:

<div class="col-sm-2" >
   <input class="form-control" placeholder="" name="reminder_time[]" type="text" value="30">
</div>
<div class="col-sm-2" >
   <select class="form-control" name="reminder_timeunit[]"><option value="minute">minutes(s)</option><option value="hour">hour(s)</option><option value="day">day(s)</option><option value="week">week(s)</option><option value="month">month(s)</option></select>
</div>
<div class="col-sm-2" >
   <select class="form-control" name="delivery_method[]"><option value="eml">Email</option></select>
</div>
<div class="col-sm-4" >
   <input class="form-control" placeholder="Your email address" name="delivery_contact[]" type="text">
</div>
<div class="col-sm-2" >
  <input class="btn btn-default" type="button" value="Delete reminder" onClick="removeitem(this);">
</div>

I don't get the error!?

Upvotes: 0

Views: 1552

Answers (1)

astratyandmitry
astratyandmitry

Reputation: 500

Erorr

Your field names must be declared as string not as array:

<input name="reminder_time" /> 

instead of

<input name="reminder_time[]">`

Validating array

Or you can change your validation rules for array (https://laravel.com/docs/5.3/validation#validating-arrays):

$rules = [
    'reminder_time.*' => 'unique',
],

Upvotes: 0

Related Questions