动态字段返回错误

时间:2014-11-03 13:21:58

标签: php laravel

我有这个观点

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 => 'abc@email.com',
    1 => 'def@email.com',
  )

所以我自己进行验证以验证电子邮件

        //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

根据我的理解,返回到电子邮件输入的输入是数组,如何在文本框中添加正确的文本框计数和值?

1 个答案:

答案 0 :(得分: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