Question

I am creating a Registration Page and the only issue I am having is that the Date input I have is returning as an Array, which is breaking my SQL request on submission, and I want to be sure that only the date and nothing related to time is being submitted.

The column in my database is labeled DoB and is what the date will be submitted into once the user submits the form. The code:

echo $this->Form->input('DoB', array(
    'label' => 'Date of birth',
    'type' => 'date',
    'dateFormat' => 'YMD',
    'minYear' => date('Y') - 70,
    'maxYear' => date('Y') - 16,
));

I have yet to find and understand it but I believe I could achieve the same result if I were to do a group of the Year Month and Day inputs under the same name of DoB.

EDIT: the values I would like to pass into the column would be formatted as such: YYYY-MM-DD

Demonstration: In other values place test. Leave DoB field as default ('1998', 'May', '7'). Upon submission of form a flash message tells me that there was a fatal error in my SQL where there is no defined Array.

SQL Error: (data columns: "username", "password", "fName", "lName", "DoB") (input from form: "test", "passwordtest", "test", "test", "Array")

Was it helpful?

Solution

you can make normal input to save it to database like ex.

    echo $this->Form->input('begin_time', array(
        'label' => 'Start',
        'value' => date('Y-m-d'). ' 09:00'
    ));

or you can use function like this for ex. in AppModel

    /**
 * input field of type datetime returns array - this func change it 
 * @param array $dateTimeArray
 * @return string "YY-MM-DD HH-ii"
 */
public function timestamp($dateTimeArray) {

    return "{$dateTimeArray['year']}-{$dateTimeArray['month']}-{$dateTimeArray['day']} {$dateTimeArray['hour']}:{$dateTimeArray['min']}";
}

and than in your model in beforeSave use it ;)

OTHER TIPS

Don't worry about it anymore. Turned out I had it all in the wrong directory.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top