폼 헬퍼 매직이 두 필드를 구별 할 수 없다는 것을 알고 있다면, 목록을 개별적으로 전달하거나 사용자 지정 양식 도우미를 사용해야합니다.
별도로 목록을 통과 할 때 options
옵션
를 사용하면 필요한 CakePHP를 자동으로 요청에 전달 된 값에 따라 적절한 목록 항목을 선택합니다 경우 options
option을 사용해야 할 일은. 제출 된 데이터 형식을 렌더링 할 때 예상대로
컨트롤러
$tableOneFoos = array('a', 'b', 'c');
$tableTwoFoos = array('x', 'y', 'z');
$this->set(compact('tableOneFoos', 'tableTwoFoos'));
보기 그것 뿐이다
$this->Form->input('TableOne.0.foo', array('type' => 'select', 'options' => $tableOneFoos));
$this->Form->input('TableOne.1.foo', array('type' => 'select', 'options' => $tableOneFoos));
$this->Form->input('TableOne.2.foo', array('type' => 'select', 'options' => $tableOneFoos));
$this->Form->input('TableTwo.0.foo', array('type' => 'select', 'options' => $tableTwoFoos));
$this->Form->input('TableTwo.1.foo', array('type' => 'select', 'options' => $tableTwoFoos));
$this->Form->input('TableTwo.2.foo', array('type' => 'select', 'options' => $tableTwoFoos));
이 값을 선택해야합니다. 의 필드 이름의 camelCased plurals있는 변수 이름에 대한
사용자 지정 양식 도우미
양식 도우미 검사 (FormHelper::_optionsOptions()
참조)는 모델 이름은 고려되지 않고, 따라서 귀하의 경우는 foos
를 찾습니다 이것이 모든 입력에 대해이 목록을 사용하는 것으로 끝나는 이유입니다.
도우미가 tableOneFoos
및 tableTwoFoos
과 같은 변수를 찾을 수 있도록 해당 메서드를 재정의하고 모델 이름을 사용하는 추가 검사를 구현할 수 있습니다.
여기 테스트되지 않았습니다!예 :
다음
App::uses('FormHelper', 'View/Helper');
class MyFormHelper extends FormHelper {
protected function _optionsOptions($options) {
$options = parent::_optionsOptions($options);
if (!isset($options['options'])) {
// this is where the magic happens, an entity name like
// `Model_field` is turned into a variable name like
// `modelFields` which is then used to lookup the view vars.
$entityName = $this->model() . '_' . $this->field();
$varName = Inflector::variable(
Inflector::pluralize(preg_replace('/_id$/', '', $entityName))
);
$varOptions = $this->_View->get($varName);
if (is_array($varOptions)) {
if ($options['type'] !== 'radio') {
$options['type'] = 'select';
}
$options['options'] = $varOptions;
}
}
return $options;
}
}
당신은 단지 입력에 대해 options
및 type
옵션을 사용하지 않고보기로 tableOneFoos
및 tableTwoFoos
을 전달할 수 :
컨트롤러
$tableOneFoos = array('a', 'b', 'c');
$tableTwoFoos = array('x', 'y', 'z');
$this->set(compact('tableOneFoos', 'tableTwoFoos'));
보기
$this->MyForm->input('TableOne.0.foo');
$this->MyForm->input('TableOne.1.foo');
$this->MyForm->input('TableOne.2.foo');
$this->MyForm->input('TableTwo.0.foo');
$this->MyForm->input('TableTwo.1.foo');
$this->MyForm->input('TableTwo.2.foo');
자신의 분야 즉, 같은 변수 이름 tableOneFoos
및 tableTwoFoos
을 끝낼 것입니다 이것은 같은 긴 HABTM 패션 방식으로 사용에는 TableOneFoo
또는 TableTwoFoo
모델이없는 것처럼 작동합니다.
사용자 정의 도우미 방법은 방법으로 나를 위해 :) – bancer
작동 BE- 것 complex-- > input ('TableOne.foo'); ' – bancer