[英]Input from Dynamically added text fields are not getting through to the request()->validate() array
I have a form where the user enters their education from Elementary to College.我有一个表格,用户可以在其中输入从小学到大学的教育。 If they want to add any other studies they've done, they can click the "Add Other Studies" button, and a new set of input fields will appear for Other Studies
如果他们想添加他们已经完成的任何其他研究,他们可以单击“添加其他研究”按钮,然后会出现一组新的其他研究输入字段
{{-- Button --}}
<tr>
<td scope="col"><button type="button" onclick="addOtherStudies()" name="add-educ-exp" id="ar-educexp" class="btn btn-outline-primary">Add Other Studies</button></td>
</tr>
{{-- Add Other Studies section --}}
{{-- .d-none is display:none --}}
<tr class="d-none" id="otherStudiesRow">
<th scope="row"><label for="other">Other Studies</label>
<input type="text" name="otherStudies[0]other_studies_name"/>
Degree/Major:<input type="text" name="otherStudies[0]other_studies_degree_major" placeholder="Masters Degree, Doctorate, Associate$quos, etc." />
</th>
<td>
<input type="text" style="width: auto; margin-top: 2rem;" name="otherStudies[0]other_studies_year"/><label for="units">If not complete, enter no. of units taken:</label><input type="text" style="width: auto;" name="otherStudies[0]other_studies_units" cols="10"/>
</td>
<td><textarea name="otherStudies[0]other_studies_awards" rows="3" cols="30"></textarea></td</tr>
<tr class="d-none" id="otherStudiesbuttons">
<td><button type="button" onclick="addEducation()" name="add-educ-exp" id="ar-educexp" class="btn btn-outline-primary">Add Education</button></td>
<td><button type="button" class="btn btn-outline-danger remove-input-field">Delete</button></td>
</tr>
I used the Javascript classList
property to display/hide the set of input fields whenever needed dynamicAddRemove.js :我使用 Javascript
classList
属性在需要 dynamicAddRemove.js 时显示/隐藏输入字段集:
function addOtherStudies(){
var section = document.getElementById("otherStudiesRow");
var buttons = document.getElementById("otherStudiesbuttons");
section.classList.remove("d-none");
buttons.classList.remove("d-none");
}
However, when I click the button and add in my data, the values in the input fields don't get forwarded to the request()->validate() array: This is my controller:但是,当我单击按钮并添加数据时,输入字段中的值不会转发到 request()->validate() 数组:这是我的控制器:
$validatedEducExp = request()->validate([
//education start at 0, length 12
"elementary_name" => ['nullable','required_with:elementary_year,elementary_awards','max:100',new LetterSpaceOnly],
"elementary_year" => ['nullable','required_with:elementary_name,elementary_awards',new fromToYearRule],
"elementary_awards" => ['nullable','required_with:elementary_name,elementary_year','string'],
"highschool_name" => ['nullable','required_with:highschool_year,highschool_awards','max:100',new LetterSpaceOnly],
"highschool_year" => ['nullable','required_with:highschool_name,highschool_awards',new fromToYearRule],
"highschool_awards" => ['nullable','required_with:highschool_name,highschool_year','string'],
"college_name" => ['nullable','required_with:college_year,college_awards,degree,college_completed,college_awards','max:100',new LetterSpaceOnly],
"college_year" => ['nullable','required_with:college_name,college_awards,degree,college_completed,college_awards',new fromToYearRule],
"degree" => ['nullable','required_with:college_name,college_year,college_awards,college_completed,college_awards','alpha','min:4','max:100'],
"college_completed"=> ['nullable','required_with:college_name,college_year,college_awards,degree,college_awards'],
"college_units" => ['nullable','digits_between:1,2'],
"college_awards" => ['nullable','required_with:college_name,college_year,college_awards,degree,college_completed','required_with:elementary_name,elementary_year','string'],
//other studies start at 12, length 5
'otherStudies.*.other_studies_name' => ['max:100',new LetterSpaceOnly],
'otherStudies.*.other_studies_degree_major' => ['alpha','min:4','max:100'],
'otherStudies.*.other_studies_year' => [new fromToYearRule],
'otherStudies.*.other_studies_units' => ['digits_between:1,2'],
'otherStudies.*.other_studies_awards' => ['string'],
//exams start at 17, length 3
'Exams.*.exam_name' => ['nullable','required_with: exam_grade,exam_date','max:100',new LetterSpaceOnly],
'Exams.*.exam_grade' => ['nullable','required_with: exam_name,exam_date','digits_between:1,3'],
'Exams.*.exam_date' => ['nullable','required_with: exam_date,exam_name','before_or_equal: today','date'],
//work exp starts at 20 length 6
'workExps.*.from' => ['nullable','digits:4'],
'workExps.*.to' => ['nullable','digits:4'],
'workExps.*.salary' => ['nullable','numeric'],
'workExps.*.employer_name_address' => ['nullable','max:255','string'],
'workExps.*.position' => ['nullable','min:5','max:50', new LetterSpaceOnly],
'workExps.*.reason_for_leaving' => ['nullable','string',new LetterSpaceOnly],
//seminar starts at 26,3
'Seminars.*.title' => ['nullable','max:100',new LetterSpaceOnly],
'Seminars.*.date' => ['nullable','before_or_equal: today','date'],
'Seminars.*.conducted_by' => ['nullable','max:100',new LetterSpaceOnly],
//skills starts 29, 2
'languages' => ['nullable','max:255',new CommaSpace],
'hobbies_skills' => ['nullable','max:255',new CommaSpace],
//others info starts 31, length 3
'blood_type' => ['nullable'],
'health_problems' => ['nullable', 'max:255',new CommaSpace],
'convictions' => ['nullable', 'max:255','string']
]);
When I enter information in the otherStudies input fields and do a ddd()
when save button is clicked, only this appears, and otherStudies
array can't be found:当我在 otherStudies 输入字段中输入信息并在单击保存按钮时执行
ddd()
时,只出现这个,并且otherStudies
数组:
array:19 [▼
"elementary_name" => null
"elementary_year" => null
"elementary_awards" => null
"highschool_name" => null
"highschool_year" => null
"highschool_awards" => null
"college_name" => null
"college_year" => null
"degree" => null
"college_units" => null
"college_awards" => null
"languages" => null
"hobbies_skills" => null
"blood_type" => null
"health_problems" => null
"convictions" => null
"Exams" => array:1 [▶]
"workExps" => array:1 [▶]
"Seminars" => array:1 [▶]
]
However, when I go back to the page, the entered information gets retained in the otherStudies
field due to the old()
helper, meaning the information is saved to the session, correct?但是,当我返回该页面时,由于
old()
助手,输入的信息会保留在otherStudies
字段中,这意味着该信息已保存到会话中,对吗? But is not able to move forward to the validate() function.但无法前进到 validate() 函数。 What's going on?
这是怎么回事?
Change your name of input to将您的输入名称更改为
otherStudies[0][other_studies_name]
otherStudies[0][other_studies_degree_major]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.