Getting Form::model() values in custom Form::macro's

Submitted by chadwithuhc - 10 years ago

Creating a form macro is very easy and awesome for creating your own UI elements. The original downside I found was losing the automatic model binding from Form::model(). A user on StackOverflow helped solve the problem for me.

/**
 * Create a custom macro
 */
Form::macro('datepicker', function() {
    // Use Form::getValueAttribute() to get the value
    $value = Form::getValueAttribute('start_date');
    return /* custom element */;
});

/**
 * NOTE: The `Form::getValueAttribute()` works like normal `Form::model()` bindings
 *   This means it will look for the value in the following order:
 *   - Session Flash Data (Old Input)
 *   - Explicitly Passed Value
 *   - Model Attribute Data
 */