2014-11-03 94 views
0

返回错误我有此视图动态字段

enter image description here

<div id="div_email" class="panel panel-default"> 
     <div class="panel-heading"> 
      <div style="float:left;margin-top:7px;"> 
       Email 
      </div> 
      <div align="right" > 
       <a class="btn btn btn-default btn-square" onclick="add_email()"><i class="fa fa-plus" ></i></a> 
      </div> 
     </div> 
     <div class="panel-body"> 
      <div id="dynamic_email" class="form-group input-group"> 
       <span class="input-group-addon"> 
        <i class="fa fa-envelope"></i> 
       </span> 
       {{ Form::text('email', Input::old('email'), array('id'=>'email1','name'=>'email[]','class'=>'remove_err_onkeyup form-control','placeholder'=>'Email', 'onblur'=>'fetchRecord()', 'autocomplete' => 'off')) }} 
      </div> 
      @if($errors->has('email')) 
       <div class="err text-danger"> 
        @foreach($errors->get('email') as $key => $message) 
         {{ $message }} 
         @if($key + 1 < count($errors->get('email'))) 
         <br> 
         @endif 
        @endforeach 
       </div> 
      @endif 
     </div> 
    </div> 

它将调用一个jquery函数来添加附加的文本框+按钮被点击

function add_email() 
    { 
     var ctr = $("#dynamic_email").length + 1; 

     var email_html = $('<div class="form-group input-group"><span class="cs_icon_cursor input-group-addon" onclick="remove_input($(this))" onmouseover="$(this).children(\'i\').attr(\'class\',\'fa fa-times\').css(\'width\',\'14px\')" onmouseout="$(this).children(\'i\').attr(\'class\',\'fa fa-envelope\')"><i class="fa fa-envelope"></i></span><input id="email'+ ctr +'" class="form-control" type="text" name="email[]" placeholder="Email"></div>'); 


     email_html.hide(); 

     $("#dynamic_email").after(email_html); 

     email_html.fadeIn('slow'); 
    } 

窗体将当然后通过表格提交提交,输入数据将为

'email' => 
    array (
    0 => '[email protected]', 
    1 => '[email protected]', 
) 

所以我做我自己的验证来验证电子邮件

 //check if email is set prevent error 
     if(isset($inputs['email'])) 
     { 
      //check if theres an email set 
      if(count($inputs['email']) > 0) 
      { 
       //loop through the email 
       foreach($inputs['email'] as $email) 
       { 
        if($email) 
        { 
         //check email format if valid 
         if(filter_var($email, FILTER_VALIDATE_EMAIL) == FALSE) 
         { 
          // display_output('invalid email'); 
          $validator->getMessageBag()->add('email','Invalid Email: '.$email); 
          $err_flag++; 
         } 
         else 
         { 
          //if email format is valid. next check domain if valid 
          //get domain name 
          $domain = explode('@',$email)[1]; 

          //check domain if valid 
          if(checkdnsrr($domain, "ANY") == FALSE) 
          { 
           // display_output('invalid domain'); 
           $validator->getMessageBag()->add('email','Invalid domain: '.$domain); 
           $err_flag++; 
          } 
         } 
        } 
       } 
      } 
     } 
     if($err_flag > 0) 
     { 
      return Redirect::to('researcher/create') 
      ->withInput() 
      ->withErrors($validator); 
     } 

我的问题是,才有可能在表单提交,旧邮件值返回到文本框中?我收到此错误

htmlentities() expects parameter 1 to be string, array given 

从我的理解,在返回的电子邮件输入输入数组,我怎么能添加正确的文本框数和文本框里面的价值?

+0

你使用laravel 4还是5? – Jerodev 2014-11-03 13:31:31

+0

我正在使用Laravel Framework 4.2.11版本 – Ponce 2014-11-04 05:20:32

回答

1

尝试使用循环遍历所有电子邮件。该错误告诉你Input::old('email')返回一个数组,这很明显,因为你有多个电子邮件字段。

@if(is_array(Input::old('email'))) 
    @foreach(Input::old('email') as $index => $email) 
     <!-- your other html --> 
     {{ Form::text('email', $email, array('id'=>'email'.($index+1),'name'=>'email[]' /* ... */)) }} 
    @endforeach 
@else 
    <!-- your other html --> 
    {{ Form::text('email', $email, array('id'=>'email1', 'name'=>'email[]' /* ... */)) }} 
@endif 
+0

我没有设法使它工作,但是这个代码以某种方式给了我一些关于如何解决它的想法,我将它存储在会话中,然后将其返回到页面。 – Ponce 2014-11-05 10:30:42