2014-04-29 5 views
0

CakePHP 앱에서 dynamiccaly 양식 필드를 추가하는 데 문제가 있으며 해결 방법을 모르겠습니다. EventsController/add.ctp에 양식을 추가하고 싶습니다. Events.name, Dates.from, Dates.to, Dates.endregister, Dates.location_id, {optional more Dates.from, Dates.to ...}, Terms_mem.teacher_id {및 옵션 더 Terms_mem.teacher_id은} 내 테이블은 다음과 같습니다동적으로 양식 필드 추가

CREATE TABLE `events` (
`id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, 
`name` varchar(150) NOT NULL 
); 

CREATE TABLE `dates` (
`id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, 
`from` datetime NOT NULL, 
`to` datetime NOT NULL, 
`endregister` datetime, 
`event_id` int(11) NOT NULL, 
`location_id` int(11) NOT NULL, 
FOREIGN KEY (`event_id`) REFERENCES `events`(`id`), 
FOREIGN KEY (`location_id`) REFERENCES `locations`(`id`) 
); 

CREATE TABLE `locations` (
`id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, 
`street` varchar(70), 
`city` varchar(70) NOT NULL 
); 

CREATE TABLE `dates_mem` (
`id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, 
`teacher_id` int(11) NOT NULL, 
`date_id` int(11) NOT NULL, 
FOREIGN KEY (`teacher_id`) REFERENCES `users`(`id`), 
FOREIGN KEY (`date_id`) REFERENCES `dates`(`id`) 
) 

그래서 형태가 보일 것 같은 :

<?php echo $this->Form->create('Event'); ?> 
<fieldset> 
<?php 
    // events 
    echo $this->Form->input('name'); 
    // dates 
    echo $this->Form->input('from'); 
    echo $this->Form->input('to'); 
    echo $this->Form->input('endregister'); 
    echo $this->Form->input('location_id'); 

    /* HERE optional dynamically add next inputs for dates (from, to, ...) */ 

    // teachers 
    echo $this->Form->input('teacher_id'); 

    /* HERE optional dynamically add next inputs for teachers(teacher_id) */ 
?> 
</fieldset> 
<?php echo $this->Form->end(__('Submit')); ?> 

그리고 해당 테이블에 저장 필드가 결국

. 이것은 CakePHP, 버전 2.4에서 가능합니까? 그렇다면, 도와 주시겠습니까?

편집 :

버줌 썼다 :

$this->Form->input('Date.0.from'); 
$this->Form->input('Date.0.to'); 
$this->Form->input('Date.1.from'); 
$this->Form->input('Date.1.to'); 

그것으로 할 수 있나요? 따라서 Date.1.from 및 Date.1.to은 버튼을 클릭

$this->Form->input('Date.0.from'); 
$this->Form->input('Date.0.to'); 
// button add next date 
$this->Form->input('Date.1.from'); // after click on add next date 
$this->Form->input('Date.1.to'); // after click on add next date 
// button add next date 
$this->Form->input('Date.2.from'); // after click on add next date 
$this->Form->input('Date.2.to'); // after click on add next date 
// button add next date 

답변

1

당신이 설명서를 읽어 봤나 다음 날짜를 동적으로 추가 한 후 형성 필드 추가? 설명서를 읽지 못하면 "Saving your data"섹션에 자세히 설명되어 있습니다. this part을 참조하십시오. 한마디로

먼저보기

$this->Form->input('FirstModel.field1'); 
$this->Form->input('SecondModel.field1'); 
$this->Form->input('SecondModel.field2'); 
$this->Form->input('Date.0.from'); 
$this->Form->input('Date.0.to'); 
$this->Form->input('Date.1.from'); 
$this->Form->input('Date.1.to'); 
// ... 

컨트롤러 :

$this->saveAll($this->request->data); 

예, 추가 할 수있는 필드를 동적으로 즉석에서 DOM에 추가 필드를 주입하기 위해 자바 스크립트를 사용합니다. JS 템플릿을 통해 또는 AJAX를 통해. 생성하는 양식 입력이 CakePHP 규칙을 따르고 JS 생성 필드를 흰색으로 나열했는지 확인하십시오. 또한 원하지 않는 것을 추가하지 않도록 매우 엄격한 입력 목록의 유효성을 검사하십시오. 보안 구성 요소를 이미 사용하지 않으면 보안 구성 요소를 사용해야합니다.

+0

감사합니다. 제 편집문을 보시겠습니까? – user3027356

+0

내 대답도 업데이트되었습니다. – burzum

관련 문제