Octobercms set dynamically datepicker minDate

I need to set dynamically

public function filterFields($fields, $context = null)
{
   $return_date = $fields->return_date->value;
   if(empty($return_date)) {
     $fields->return_date->minDate = Carbon::now();
   }
}

Not work!

Upvotes: 0

Views: 577

Answers (2)

Raja Khoury
Raja Khoury

Reputation: 3195

You should pay attention to your model's fields.yaml - Where have you defined the return_date field, is it in the primary or secondary tabs ?

Try :

public function formExtendFieldsBefore( $widget ) {
    $widget->tabs['fields']['return_date']['minDate'] =  Carbon::now()->format('Y-m-d');
}

Or

public function formExtendFieldsBefore( $widget ) {
    $widget->secondaryTabs['fields']['return_date']['minDate'] =  Carbon::now()->format('Y-m-d');
    // notice here $widget->secondaryTabs Vs $widget->tabs
}

This should work, if it doesn't please share your fields.yaml file .

Upvotes: 1

Hardik Satasiya
Hardik Satasiya

Reputation: 9693

Hmm, may be datetime widget is setting this restrictions at init time and filterfields is fired after that so it can not use new modified data.

to over come this we set config data before init , we use controller method formExtendFieldsBefore add this method to your controller

public function formExtendFieldsBefore($form) {
    $form->fields['return_date']['minDate'] = \Carbon::now()->format('Y-m-d');
}

it should work, please check this and if face any issue please comment.

Upvotes: 1

Related Questions