페이지에 여러 양식이 있습니다. 양식 중 일부는 CustomerID와 같은 이름의 요소를 공유합니다. 이는 요소 ID CustomerID가 다른 양식의 동일한 ID와 충돌 함을 의미합니다. 필드 이름에 폼의 이름을 접두사로 붙이는 방법을 찾고 싶습니다. 예를 들어 PaymentProfile_CustomerID입니다. 제안?Zend_Form 요소 이름 앞에 접두사를 붙일 방법이 있습니까?
지금까지 내가 가지고 올 수 있었던 최선은 아래
class MyForm extends Zend_Form
{
public function init()
{
$this->setName("PaymentProfile");
...
$this->_prefixElementNames();
}
private function _prefixElementNames()
{
$elements = $this->getElements();
$formName = $this->getName();
foreach($elements as $e) {
$e->setAttrib('id', $formName . '_' . $e->getName());
}
}
}
업데이트 @의 가비의 대답은 간단한 수정과 잘했다.
public function addElement($element, $name = null, $options = null)
{
$e = parent::addElement($element, $name, $options);
if($this->getName())
// I use setAttrib instead of setName because I only want the ID to be changed.
// Didn't want the form data to be prefixed, just the unique HTML identifier.
$element->setAttrib('id', $this->getName() . '_' . $element->getName());
return $e;
}
나는 그것이 무엇을하는지 보게 될 것이다. 감사! – David
이것은 내가하려는 일을 수행하는 가장 올바른 방법 인 것으로 보입니다. 고마워. – David
전에 짧은 답변을 드려 죄송합니다. 이전에는 시간이별로 없었지만 충분히 보았습니다.]. 나중에 참조 할 수 있도록 내 대답을 확장했습니다. – vandalizmo