0

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!?

7
  • Install whoops and get more details, there's nothing there that would cause that error as you're using unescaped output. Commented Oct 6, 2016 at 13:50
  • I belive that it could be whatever you are doing or adding into your #reminderitem div. There is nothing wrong with that portion of code. Commented Oct 6, 2016 at 14:02
  • Installed Whoops, output shown above. Not sure what to look for in Whoops Commented Oct 6, 2016 at 14:06
  • @JulianRodriguez well, I didn't add anything to reminderitem in my test. That's what's strange to me. Commented Oct 6, 2016 at 14:09
  • Ok, this is strange. I tried your code in a clean laravel project just a couple of minutes ago and everything was ok. I would say that this portion of code is not generating the error. Probably a foreach in some other place in your view is causing this error. Commented Oct 6, 2016 at 14:15

1 Answer 1

0

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',
],
Sign up to request clarification or add additional context in comments.

4 Comments

The thing is that I need them to be declared as arrays (e.g reminder_time[] ) because I have the option of generating many reminders. reminder_time[0] and reminder_time[1] can have the same values
So you gust must to change validation rules for array, and also if remainder_time's stored as one column in database, you need to json_encode() it or serialize() before save.
Tried adding this validation to my Request rules 'reminder_time.*' => 'numeric', 'reminder_timeunit.*' => 'alpha_num', 'delivery_method.*' => 'alpha_num', 'delivery_contact.*' => 'email', but got the same error... and why does it work with plain html, but not with Forms Html, see update #2
Yeah, it doesn't make sense. It should not be the name at all or it would not works in plain html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.