2014-02-11 78 views
1

我有4个字段,将由用户动态创建。Laravel 4验证失败后将输入重定向回页面

<div class="controls" id="exchange-fields"> 
      <p> 
      <div id='exchange_div'> 
      <div class="input-append" id='currency_div1'> 
      {{ Form::select('currency_id[]', $currencies, null, array('name'=>'currency_id', 'id'=>'currency_id', 'value'=>'Input::old("currency_id")[0]')) }} 
      </div> 

      &nbsp;&nbsp;&nbsp;&nbsp; 
      <div class="input-prepend" id='actual_div1'> 
      <span class="add-on">Actual</span> 
      {{ Form::text('exchange_rate[]', null, array('class'=>'input-medium rate', 'maxlength'=>10, 'id'=>'exchange_rate', 'value'=>'Input::old("exchange_rate")[0]')) }} 
      </div> 

      &nbsp;&nbsp;&nbsp;&nbsp; 
      <div class="input-append" id='markup_div1'> 
      {{ Form::text('exchange_rate_markup[]', null, array('class'=>'input-mini yellow rate', 'maxlength'=>4, 'id'=>'exchange_rate_markup', 'value'=>'Input::old("exchange_rate_markup")[0]')) }}<span class="add-on">%</span> 
      </div> 

      &nbsp;&nbsp;&nbsp;&nbsp; 
      <div class="input-prepend" id='rate_div1'> 
      <span class="add-on">Marked-up</span> 
      {{ Form::text('after_markup_rate[]', null, array('class'=>'input-medium yellow', 'maxlength'=>10, 'id'=>'after_markup_rate', 'value'=>'Input::old("after_markup_rate")[0]')) }} 
      </div> 


     </div> 
     </div> 
      &nbsp;&nbsp;&nbsp;&nbsp; 
      <div class="controls"> 
       <input class="btn btn-primary" type="button" id="addScnt" value="Add"> 

      </div> 

我使用javascript来动态填充这些字段。

var scntDiv = $('#exchange-fields'); 
    var i = $('#exchange-fields p').size() + 1; 


    $('#addScnt').click(function() {   

    $(scntDiv).append('<p>'); 
    $("#exchange_div").clone().attr("id",'exchange_div_'+ i).appendTo(scntDiv); 

//Append the remove button 
    $($('#exchange_div_'+ i)).append('<input class="btn btn-primary" name="remove" type="button" id="remScnt" value="Remove">'); 
    i++; 

    }); 

这是完美的工作,直到我POST这些值到我的控制器,如果验证失败。 如何使用在我的视图文件中动态填充的旧输入填充这些字段?我使用return Redirect :: back() - > withInput() - > withErrors($ e-> getErrors());验证失败后重新导向这些字段。但是,因为这4个字段只接受字符串值,并且返回的输入位于数组中,所以我无法在验证失败后重新填充这4个字段。

任何好的方法来解决这个问题?

在此先感谢。

+0

您是否尝试过使用'Input :: old(“exchange_rate_markup.0”)'而不是'Input :: old(“exchange_rate_markup”)[0]'? – Arda

+0

不好用 – Eain

回答

0

不要在HTML中设置输入字段的值。当验证失败时,Laravel将自动执行此操作。

Form::text中的第二个参数是值。将其设置为null将允许Laravel自动填充它。

+0

亲爱的布拉德利,我想你误解了我的qns。这在html上静态创建字段时有效。我的qns现在用于那些动态创建的字段(由Javascript创建),如何在验证失败时自动填充回来。无论如何,我已经解决了这个问题。谢谢 – Eain

+0

啊我明白了,对不起误解。 –

+0

@Eain好奇地想知道你是如何解决这个问题的,我正在为同一个问题研究解决方案 – a7omiton

3

@ a7omiton而不是返回所有输入。我使用Input :: except(array)来返回不是数组的其余字段。

重定向回来时,我使用一个额外的()来返回字段数组,例如当您最初加载页面时如何呈现视图。

// If validation fails, ignore those exchange rate fields 
//because they are in array not string 
$input = Input::except([ 
    'currency_id', 
    'exchange_rate', 
    'exchange_rate_markup', 
    'after_markup_rate' 
]); 
// return normally as separate array and it will store in session 
return Redirect::back() 
    ->withInput($input) 
    ->withErrors($e->getErrors()) 
    ->with('exchange_rate', Input::get('exchange_rate')) 
    ->with('currency_id', Input::get('currency_id')) 
    ->with('exchange_rate_markup', Input::get('exchange_rate_markup')) 
    ->with('after_markup_rate', Input::get('after_markup_rate')); 
+0

如何在我刚刚重定向的视图中访问'exchange_rate'? –

0

laravel将autopopulate表单域。 只需在Input :: old('column_name')中输入第二个参数即可。

{{ Form::text('name', Input::old('name'), array('class' => 'span4','placeholder' => 'name')) }} 
0

尽管它是一个旧的帖子,这将有助于一些人在这里寻找解决方案。

您可以通过在你的laravel视图添加该代码得到重定向后视图中的所有输入变量,

print_r(app('request')->old()); 

你可以指定这个值,你的JavaScript变量,并动态填充字段。

相关问题