在组件中,
default.htm
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var maxField = 3; //Input fields increment limitation
var addButton = $('#add'); //Add button selector
var wrapper = $('#main'); //Input field wrapper
var i=1;
var fieldHTML = '<div id="p_'+i+'" >' +
'Name:<input type="text" name="name'+i+'" id="name'+i+'">' +
' <a href="" id="p_'+i+'" class="remove_button">remove</a>' +
'</div>'; //New input field html
var x = 1; //Initial field counter is 1
//Once add button is clicked
$(addButton).click(function(){
//Check maximum number of input fields
// alert("k");
if(x < maxField){
x++; //Increment field counter
$(wrapper).append(fieldHTML); //Add field html
}
i++;
});
//Once remove button is clicked
$(wrapper).on('click', '.remove_button', function(e){
var proj_button_id = $(this).attr("id");
//alert(edu_button_id);
e.preventDefault();
$("#"+ proj_button_id +"").remove();
// $(this).parent('div').remove(); //Remove field html
x--; //Decrement field counter
});
});
</script>
<form
method="post"
name="form" id="form" accept-charset="UTF8"
enctype="multipart/form-data" data-request="onEnter" data-request-files>
<input type="hidden" name="_handler" value="onEnter">
{{ form_token() }}
{{ form_sessionKey() }}
<div id="main">
Name:<input type="text" name="name0" id="name0">
<a href="javascript:void(0);" id="add">Add Another</a>
<input type="submit" name="createAccount" value="Submit">
</div>
</form>
componentname.php
class RepeaterName extends ComponentBase
{
public function componentDetails()
{
return [
'name' => 'ReapeterName',
'description' => 'Name.'
];
}
public function onEnter()
{
$input = post();
log::info($input);
$contact = new RepeaterModel();
$instArr = [];
$instArr['innername'] = [];
for($i=0; $i<=5; $i++)
{
if(isset( $input['name'.$i])) {
$instArr['innername'][] = $input['name'.$i];
}
}
Log::info(json_encode($instArr));
$contact->name =json_encode($instArr);
//$contact->name=Input::post('name');
$contact->save();
Log::info($contact);
}
}
?>
modelname.php
protected $jsonable = ['name'];
我想显示输入来自前端,该前端是多个名称,必须以json格式保存,因为我想显示在repaeter控件中的那个输入控件,其名称为“ name”,并且其中具有名为“ innername”的文本控件所以有什么解决方案可以在后端表单上显示输入
答案 0 :(得分:0)
是的,我解决了实际上是我的错误,我在componentName.php上使用json_encode,这是错误的,因为我们已经在模型上使用$ jsonable,所以传递json_encode()直接传递存储数据的数组
componentName.php
public function onEnter()
{
$input = post();
// log::info($input);
$contact = new RepeaterModel();
$instArr = [];
for($i=0; $i<3; $i++)
{
$obj = [];
if(isset( $input['name'.$i])) {
$obj['innername'] = $input['name'.$i];
}
if(isset( $input['addr'.$i])) {
$obj['inneraddr'] = $input['addr'.$i];
}
if(!empty($obj))
$instArr[] = $obj;
}
$contact->fname=$instArr;
$contact->age=$input['age'];
$contact->save();
Log::info($contact);
}
}