문제

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")

도움이 되었습니까?

해결책

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 ;)

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top