Search code examples
datecakephpformhelper

CakePhp FormHelper - Date field issue


In the "view" of my "register" action , I tried to create a date field for the date of birth.

echo $this->Form->input("Member.dateofbirth", array(
            "label" => "Date of birth",
            "name" => "date_of_birth",
            "id" => "date_of_birth",
            "empty" => true,
);

But when I send data with submit button, and make a debug(), I see that $this-> request->data["date_of_birth"] contains only the year, and does not contain the month and day.

debug($this->request->data["date_of_birth"]);

Result of debug() :

array(
    'date_of_birth' => '1990'
)

Solution

  • I guess that if you set the name option in the FormHelper, it will only modify the name of the first input (date fields have 3 inputs). That's why you only get the year when you try to read the field with that name.

    If you don't really need a different input name, you should stick with the defaults.

    So, in this case, removing the name option in FormHelper will allows you to read the field like $this->request->data["Member"]["dateofbirth"]