mardi 26 septembre 2017

Laravel checkbox validation, always empty?

I have a checkboxes like this:

<div class="form-group">
  <div style="display:none ;" class="weekday_message form-control alert-warning"></div>
  <label id="weekday2" for="weekday" class="col-md-4 control-label">Weekday</label>
  <div class="required form-field" name="weekday" id="weekday">
    <input class="weekday" type="checkbox" name="weekdays[]" value="MO">Monday
    <input class="weekday" type="checkbox" name="weekdays[]" value="TU">Tuesday
    <input class="weekday" type="checkbox" name="weekdays[]" value="WE">Wednesday
    <input class="weekday" type="checkbox" name="weekdays[]" value="TH">Thursday
    <input class="weekday" type="checkbox" name="weekdays[]" value="FR">Friday
    <input class="weekday" type="checkbox" name="weekdays[]" value="SA">Saturday
    <input class="weekday" type="checkbox" name="weekdays[]" value="SU">Sunday
  </div>
    <span class="help-block">
        <strong></strong>
</span>
  </div>

My validation:

public function rules()
    {
        return [
            'startdate' => 'required|date',
            'endate' => 'nullable|date',
            'startime' => ['required', new Time],
            'endtime' => ['required', new Time],
            'title' => 'required',
            'entity_id' => 'required',
            'type' => 'required|exists:entities,type',
            'description' => 'required',
            'frequency' => 'required',
            'interval' => 'nullable|numeric',
            'monthday' => 'nullable|numeric|min:1|max:3',
            'weekdays' => 'array|max:3',
            'month' => 'nullable|numeric',
            'until' => 'nullable|date',
            'tags' => 'nullable',
        ];
    }

and controller:

public function storeEvent(EventRequest $request)
{
    $test = ($request->input('weekdays'));
    dd($test);
    $weekday_string = implode(",", $request->input('weekdays'));
    $request->merge(array('weekday', $weekday_string));
    dd($request->all());
    $event = DirtyEvent::create($request->all());
    $geoloc_id = Entity::find($event->entity_id)
        ->first();
    $user_id = Auth::id();
    // Save Geoloc + user id into newly created event
    $event->_geoloc()->associate($geoloc_id);
    $event->users()->associate($user_id);
    $event->save();

Now, validation seems to pass because it does data dump, however both dd($test) as well as $request->all() are giving me back empty weekdays, like it would not be defined. What could be the possible cause of this?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire