-
Notifications
You must be signed in to change notification settings - Fork 297
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Value callback string #195
Comments
I can think of 2 solutions, not sure which one is better:
public function setClientSinceAttribute($value)
{
$this->attributes['client_since'] = Carbon::parse($value);
}
public function getCientSinceAttribute()
{
return $this->client_since->format('Y-m-d');
} Pros: No need for creating custom field type
class CustomDateType extends FormField
{
public function __construct($name, $type, Form $parent, array $options = [])
{
$this->valueClosure = function($value) {
return $value->format('Y-m-d');
}
}
} Pros: Good for multiple models, keeps code DRY. I haven't tested any of these solutions, but they should work. Hope I helped. Let me know which one better works for u. |
There is of course a 3rd option: your date field could support Carbon instances and print the right format in the |
@kristijanhusak @rudiedirkx FYI, I'm working on a PR to add "filters" on forms. That way, we could format/escape... values. |
Sounds good. Can we track it somewhere? I created the custom |
Not now, still having issues to fetch data after isValid. I'll try to push it this week and let you know. |
I have a form that adds date fields:
The db columns are default Laravel timestamps with Carbon init for date objects (
protected $dates
).To make the HTML form understand the date, I have to add a value callback to the form to rewrite it to
Y-m-d
at the very last second, for the default value:I have several forms with several date fields. There must be a better way.
I was very sad to learn this does not work:
because that's not considered a callback. (Laravel's
value()
function, but also your own code, specifically check forClosure
.)Is there a good way to do this?
The text was updated successfully, but these errors were encountered: