来自动态添加的文本字段的输入无法通过 request()->validate() 数组



我有一个表单,其中用户输入他们的教育从小学到大学。如果他们想添加他们做过的任何其他研究,他们可以点击"添加其他研究"。按钮,其他研究

将出现一组新的输入字段
{{-- 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>

我使用JavascriptclassList属性在需要时显示/隐藏一组输入字段dynamicAddRemove.js:

function addOtherStudies(){
var section = document.getElementById("otherStudiesRow");
var buttons = document.getElementById("otherStudiesbuttons");
section.classList.remove("d-none");
buttons.classList.remove("d-none");
}

但是,当我单击按钮并添加数据时,输入字段中的值不会被转发到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']
]);

当我在其他研究输入字段中输入信息并在单击保存按钮时执行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 [▶]
]

但是,当我返回页面时,由于old()helper,输入的信息保留在otherStudies字段中,这意味着信息被保存到会话中,对吗?但无法转到validate()函数。这是怎么呢

将您的输入名称更改为

otherStudies[0][other_studies_name]
otherStudies[0][other_studies_degree_major]

相关内容

  • 没有找到相关文章

最新更新