I'm building a poll in Laravel 5.3 and Vue.js 1.0
. A poll has several options I'm trying to validate the name of all options.
Currently my UpdateRequest
in Laravel looks like this:
public function rules()
{
$rules = [
'subject' => 'required|max:40|min:2',
'question' => 'required|max:150|min:2',
'options' => 'required|max:4|min:2'
];
foreach($this->request->get('options') as $key => $val) {
$rules['option'][$key] = 'max:2';
}
return $rules;
}
I'm adding the options like this in vue.js
:
<template>
<input
type="text"
name="option"
class="Form-group-item"
v-model="option.name"
placeholder="Optie">
<button class="Btn Btn-second" @click.prevent="addOption">
Voeg optie toe
</button>
</template>
<script>
export default {
data () {
return {
option: {},
options: []
}
},
methods: {
addOption () {
if(this.option.name) {
this.options.push(this.option);
this.option = {};
}
}
}
}
</script>
When I dd($rules);
it looks like this:
array:6 [
"subject" => "required|max:40|min:2"
"question" => "required|max:150|min:2"
"options" => "required|max:4|min:2"
"option.0" => "max:2"
"option.1" => "max:2"
"option.2" => "max:2"
]
When I dd($this->request->all());
within the UpdateRequest
result is:
array:8 [
"id" => 7
"slug" => "asdfasdf"
"subject" => "asdfasdf"
"question" => "asdfasdf"
"user_id" => 1
"created_at" => "2016-11-15 15:24:21"
"updated_at" => "2016-11-15 15:24:21"
"options" => array:3 [
0 => array:7 [
"id" => 15
"slug" => "asdfasdf"
"name" => "asdfasdf"
"poll_id" => 7
"created_at" => "2016-11-15 15:24:21"
"updated_at" => "2016-11-15 15:24:21"
"votes" => []
]
1 => array:7 [
"id" => 16
"slug" => "asdfasdfasdfasdfasdf"
"name" => "asdfasdfasdfasdfasdf"
"poll_id" => 7
"created_at" => "2016-11-15 15:24:21"
"updated_at" => "2016-11-15 15:24:21"
"votes" => []
]
2 => array:7 [
"id" => 17
"slug" => "efqwerqwer"
"name" => "efqwerqwer"
"poll_id" => 7
"created_at" => "2016-11-15 15:48:47"
"updated_at" => "2016-11-15 15:48:47"
"votes" => []
]
]
How do I get this to work!?