什么是symfony小部件名称格式以及如何使用?/它用于什么?这是我的symfony表单,几乎没有小部件。我必须设置名称格式,以便我可以在视图中使用小部件。
class EmployeeTaskForm extends BaseForm
{
private $employeeTaskService;
private $employeeTask;
private $empNumber;
public function configure()
{
$empNumber = $this->getOption('empNumber');
$this->empNumber = $empNumber;
$employeeTasks = $this->getOption('employeeTasks');
// Note: Widget names were kept from old non-symfony version
$widgets = array('empNumber' => new sfWidgetFormInputHidden(array(), array('value' => $empNumber)));
$validators = array('empNumber' => new sfValidatorString(array('required' => true)));
$EmployeeTaskwidgets = $this->getTaskManagerWidgets();
$labels = $this->getFormLabels();
$EmployeeTaskvalidators = $this->getTaskManagerValidators();
$widgets = array_merge($widgets, $EmployeeTaskwidgets);
$validators = array_merge($validators, $EmployeeTaskvalidators);
$this->setWidgets($widgets);
$this->getWidgetSchema()->setLabel($labels);
$this->setValidators($validators);
$this->widgetSchema->setNameFormat('employeeTask[%s]');
}
/*
* This fuction will return the widgets of the form
*/
public function getTaskManagerWidgets(){
$widgets = array();
//creating widgets
$widgets['seqNo'] = new sfWidgetFormInputHidden();
$widgets['task_name'] = new sfWidgetFormInputText();
$widgets['description'] = new sfWidgetFormTextarea();
$widgets['due_date'] = new ohrmWidgetDatePicker(array(), array('id' => 'taskDueDate'));
return $widgets;
}
/*
* This fuction will return the form validators
*/
public function getTaskManagerValidators(){
$validators = array(
'seqNo' => new sfValidatorNumber(array('required' => false, 'min' => 1)),
'task_name' => new sfValidatorString(array('required' => true)),
'description' => new sfValidatorString(array('required' => false)),
'due_date' => new sfValidatorDate(array('required' => false))
);
return $validators;
}
/**
*
* @return array
*/
protected function getFormLabels() {
$labels = array(
'task_name' => __('Name'),
'description' => __('Description'),
'due_date' => __('Due date')
);
return $labels;
}
public function getEmployeeTask(){
if (!$this->employeeTask instanceof EmployeeTask) {
$posts = $this->getValues();
$employeeTask = new EmployeeTask();
$employeeTask->task_name = $posts['task_name'];
$employeeTask->description = $posts['description'];
$employeeTask->due_date = $posts['due_date'];
$employeeTask->seqno = $posts['seqNo'];
$empNumber = $this->getOption('empNumber');
$seqNo = $this->getOption('seqNo');
if (empty($seqNo)) {
$q = Doctrine_Query::create()
->select('MAX(et.seqno)')
->from('EmployeeTask et')
->where('et.emp_number = ?', $empNumber);
$result = $q->execute(array(), Doctrine::HYDRATE_ARRAY);
if (count($result) != 1) {
throw new PIMServiceException('MAX(seqno) failed.');
}
$seqNo = is_null($result[0]['MAX']) ? 1 : $result[0]['MAX'] + 1;
} else {
$employeeTask = Doctrine::getTable('EmployeeTask')->find(array('emp_number' => $empNumber,
'seqno' => $seqNo));
if ($employeeTask == false) {
throw new PIMServiceException('Invalid employee Task');
}
}
if ($employeeTask === false) {
$employeeTask = new EmployeeTask();
$employeeTask->emp_number = $empNumber;
$employeeTask->seqno = $seqNo;
}
$employeeTask->seqno = $seqNo;
$this->employeeTask = $employeeTask;
}
return $this->employeeTask;
}
public function save($empNumber){
$post = $this->getValues();
$employeeTask = $this->getEmployeeTask();
$employeeTask->emp_number = $empNumber;
$employeeTaskService = $this->getEmployeeTaskService();
$employeeTaskService->saveEmployeeTask($employeeTask);
}
}
问题:当我添加$this->widgetSchema->setNameFormat('employeeTask[%s]');
行时,收到错误消息Unexpected extra form field named "employeeTaskUpdate"
有人可以解释名称格式的使用方法以及使用它的正确方法吗?