LaravelCollective / html

HTML and Form Builders for the Laravel Framework
MIT License
4k stars 808 forks source link

Form model binding date format issue #465

Open anilkreddy opened 6 years ago

anilkreddy commented 6 years ago

Hi, I have a form with a date field, I have written form model accessors but the value is still not changing. <input type="text" id="birthdate" name="profile[birthdate]" value="12/05/1991">

Here the date format is in (m/d/Y) but required format is (d/m/Y)

And the form model accessor defined in Model Profile model is,

public function formBirthdateAttribute($date)
{
    return Carbon::parse($date)->format('d/m/Y');
}
chepe263 commented 6 years ago

looking at this question, are you using the trait?

anilkreddy commented 4 years ago

looking at this question, are you using the trait?

Seems I already have this issue raised, today when I tried the same thing, it still doesn't work for me.

Yes, I have the trait in the model,

I have use FormAccessible

protected $fillable = [
    'title', 'firstname', 'lastname', 'date_of_birth'
];

protected $dates = [
    'date_of_birth',
];

public function formDateOfBirthAttribute($date)
{
    return Carbon::parse($value)->format('d-m-Y');
}`

And the form field is {!! Form::text('profile[date_of_birth]', null, ['placeholder' => 'dd/mm/yyyy', 'class' => 'js-datepicker form-control', 'data-date-format' => 'dd/mm/yyyy']); !!}

chepe263 commented 4 years ago

What if your model did the date conversion?

anilkreddy commented 4 years ago

What if your model did the date conversion? Sorry, I'm not clear. How do I do that?

chepe263 commented 4 years ago

The docs says you can have a $dateFormat property in case your date format it's different from the usual Y-m-d.

There's also Mutators and Accesors; you define functions to retrieve and format data from a column and you can also modify it before saving it.

mmoser-adrianv commented 2 years ago

Is the generator not reading through eloquent's casting rules? I have some models that cast fields to date with a specific format but I still get datetime in the value outputted by the generator.