0
votes

so i have this function where the user needs to enter the start date and end date of a certain event. So what i did was i added a date picker to my view and in my controller, the only thing i can do is allow end dates after the start date.but i should also be able to allow same start and end date say for example the user chose the start date today, i should be able to allow the end date to be today too. basically, end dates can be on and after the start date here is my validation codes in my controller.

$rules = array(
        'eventName'    => 'required',
        'users'        => 'required',
        'evStart'      => 'required',
        'evEnd'        => 'required|after:evStart',
    );

and here is my codes for the view

<div class="input-field col s10 offset-s1" style="margin-top:40px;">
    {{ Form::label('date', 'Event Start *') }}
    {{ Form::input('date', 'evStart', Input::old('evStart'), ['class'=>'datepicker',   'placeholder' => 'Date']) }}
</div>

<div class="input-field col s10 offset-s1">
    {{ Form::label('date', 'Event End *') }}
    {{ Form::input('date', 'evEnd', Input::old('evEnd'), ['class'=>'datepicker', 'placeholder' => 'Date']) }}
</div>

any ideas on how i can improve my codes? thanks so much in advance!

2
r u using $("#startdate").datepicker({ }); in js - Borna
hi so i just added the datepicker class to my Form::input - BourneShady
see i add a answer...check if it works - Borna

2 Answers

1
votes

Make you own validation rules, for dates:

Validator::extend('after_equal', function($attribute, $value, $parameters, $validator)
{
    return strtotime($validator->getData()['evStart']) <= strtotime($value);
});

For start date:

Validator::extend('before_equal', function($attribute, $value, $parameters, $validator)
{
    return strtotime($validator->getData()['evEnd']) >= strtotime($value);
});

Put this code for where you want. Use Validator::extendImplicit if you want to validate empty fields.

1
votes
<script type="text/javascript">
     $( function() {


    //----------date format-----------------

    $("#startdate").datepicker({
            dateFormat: "mm/dd/yy",

            onSelect: function (date) {
                var date2 = $('#startdate').datepicker('getDate');
                date2.setDate(date2.getDate());

                $('#enddate').datepicker('option', 'minDate', date2);
            }
        });
        $('#enddate').datepicker({
            dateFormat: "mm/dd/yy",
            onClose: function () {
                var dt1 = $('#startdate').datepicker('getDate');
                console.log(dt1);
                var dt2 = $('#enddate').datepicker('getDate');
                if (dt2 <= dt1) {
                    var minDate = $('#enddate').datepicker('option', 'minDate');
                    $('#enddate').datepicker('setDate', minDate);
                }
            }
        });
  } );
</script>