Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel:重复字段(和字段组):表单模型绑定_Php_Mysql_Validation_Laravel - Fatal编程技术网

Php Laravel:重复字段(和字段组):表单模型绑定

Php Laravel:重复字段(和字段组):表单模型绑定,php,mysql,validation,laravel,Php,Mysql,Validation,Laravel,我正在Laravel中构建一个表单,它以可重复实体的形式处理数组字段(无论是单输入还是字段组)。当表单中存在重复字段的验证错误或不同输入时,我在使用表单模型绑定时遇到了一个问题 现在,我通过使用AJAX拉入部分视图来生成每个字段的新“实例” # Add Feature $(document).on 'click', '.js-listing__add-feature', (e) -> e.preventDefault() $.ajax url: dashboard.part

我正在Laravel中构建一个表单,它以可重复实体的形式处理数组字段(无论是单输入还是字段组)。当表单中存在重复字段的验证错误或不同输入时,我在使用表单模型绑定时遇到了一个问题

现在,我通过使用AJAX拉入部分视图来生成每个字段的新“实例”

# Add Feature
$(document).on 'click', '.js-listing__add-feature', (e) ->
  e.preventDefault()
  $.ajax
    url: dashboard.partials.feature
    type: 'GET'
    success: (data) ->
      $data = $(data)
      $('.js-listing__features').append $data
      return
  return

# Removing features
$(document).on 'click', '.js-listing__remove-feature', (e) ->
  e.preventDefault()
  $(this).parent('.js-listing__feature-wrapper').remove()
  return
因此,用户可以动态创建新的功能输入,这些输入在保存时最终合并为一个数组。当表单中存在验证问题并且我们被重定向回时,问题就会出现。我还没有找到一种方法来访问处于(动态或非动态)状态的features数组,以吐出它们以前拥有的内容。在写这篇文章时,我想如果是输入本身导致了验证问题,那么问题也会清除该字段

我在docs和ole Google中搜索过关于这个主题的灵感,但没有发现任何东西。任何朝正确方向的轻推都会非常有帮助。一如既往地谢谢你

表格范例
@extends('dashboard.master')
@节(“内容”)
编辑列表
@包括('dashboard.partials.errors')
{!!Form::model($listing,['method'=>'PATCH','route'=>['dashboard.listings.update',$listing->id],'class'=>'uk Form'])
{!!Form::label('price','price')
{!!Form::text('price')!!}
{!!Form::label('features','features')
@如果($listing->features&&count($listing->features))
@foreach($listing->features as$key=>$feature)
@如果($key>0)
@恩迪夫
@endforeach
@否则
@恩迪夫
{!!Form::submit('updatelisting')!!}
{!!Form::close()!!}
@停止

当我在编辑列表时有值显示它们时,您将看到我对
@foreach
的看法。这里的问题不是读取返回的值(我已经设置/获取了这些属性),而是表单模型绑定如何与输入数组一起工作,以便在使用AJAX将这些值动态添加到表单中时仍然可以使用这些值。

我以前也遇到过类似的问题。。。我的代码当然不是很优雅,但它起了作用;它可能会帮助你建立一些东西

我的技巧是为项目生成不同的名称,区分新旧项目,计算新项目:

<input type="text" name="E1-features"> // existing feature #1
<input type="text" name="N1-features"> // new feature #1
<input type="text" name="N3-features"> // new feature #3 (assuming user deleted #2)
<input type="hidden" name="counter" value="3"> // 3 features were added
//现有功能#1
//新功能#1
//新功能#3(假设用户已删除#2)
//增加了3个功能
在服务器端,控制器将现有输入与新输入区分开来。以下是新输入的代码:

Input::flash();

// Existing features
foreach($features as $key => $feature){
    if (Input::get('E'.$key.'-features')){
        $rules['E'.$key.'-features'] = 'required';
    }
}

// New features
for ($i = 1;  $i <= Input::get('counter'); $i++) {
    if (Input::get('N'.$i.'-features')){
        $rules['N'.$i.'-features'] = 'required';
    }
}

$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()){
    return Redirect::to('page')->withErrors($validator)->withInput();
}else{
   // data stuff
}
Input::flash();
//现有特征
foreach($key=>$feature的功能){
if(输入::get('E'.$key.-features')){
$rules['E'.$key.-features']='required';
}
}
//新功能
对于($i=1;$i失败(){
return Redirect::to('page')->withErrors($validator)->withInput();
}否则{
//数据资料
}

你能举例说明你的表格是什么样子的,以及你到底在哪里面临问题吗?当然@DhirajBodicherla我已经在我的原始问题中添加了表格示例。谢谢如果你能在fiddle或其他地方展示它,那么解决你的问题就更容易了。我不知道如何在像Laravel这样的框架后面展示服务器端技术。。。
Input::flash();

// Existing features
foreach($features as $key => $feature){
    if (Input::get('E'.$key.'-features')){
        $rules['E'.$key.'-features'] = 'required';
    }
}

// New features
for ($i = 1;  $i <= Input::get('counter'); $i++) {
    if (Input::get('N'.$i.'-features')){
        $rules['N'.$i.'-features'] = 'required';
    }
}

$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()){
    return Redirect::to('page')->withErrors($validator)->withInput();
}else{
   // data stuff
}