0

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?

3
  • Do you have any of the checkboxes checked in your form? As far as I know, an unchecked checkbox is not passed to Request input. Commented Sep 26, 2017 at 15:20
  • ofc I have, I checked all of them during the form process Commented Sep 26, 2017 at 15:25
  • Your HTML says weekday (singular) but in your rules set is as weekdays (plural). I added this to my answer. Commented Sep 26, 2017 at 15:42

2 Answers 2

1

If you want to make sure you have always at least one weekday selected you should change:

'weekdays' => 'array|max:3',

into:

'weekdays' => 'array|required|max:3',

Also I suppose you don't send data using standard HTML form because you set for example name for divs so maybe you forget to attach weekdays or have bug in code elsewhere?

Sign up to request clarification or add additional context in comments.

7 Comments

I am sending them using ajax, the thing is I tried to do it as 'required' and funny thing is it was not returning any errors
So look at your browser console and verify what exactly is sent. How do you know it's not returning any validation errors? You verified it already in console?
I am however doing form.serialize in ajax, data: that might be the cause?
Hard to say. Look at your console and verify what exact data you are sending into your backend.
and yes, I have verified and if I remove dd($test) I get dd of $request->all but just without weekdays[]
|
0

Your HTML says weekday (singular) but your rules set says weekdays (plural).

There needs to be at least one checkbox selected to make the input weekdays to be included in the request. You can use a default value in case none was selected by adding a hidden input before the checkboxes.

<input type="hidden" name="weekdays" value="defaultValue">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.