/** * @param FormField $formField * @param KForm $form * @throws Exception */ public function instanceMapTo(FormField $formField, KForm $form) { $attribute = $formField->getFieldName(); $method = 'mappingToFormField' . ucfirst($attribute); if ($this->instanceReflector->hasMethod($method)) { $methodReflector = $this->instanceReflector->getMethod($method); $parameters = array(); foreach ($methodReflector->getParameters() as $paramReflector) { switch (true) { case $paramReflector->getClass()->isSubclassOf(KForm::class): $parameters[] = $form; break; case $paramReflector->getClass()->isSubclassOf(FormField::class): $parameters[] = $formField; break; default: $parameters[] = null; break; } } $default = call_user_func_array(array($this->instance, $method), $parameters); } else { if (!$attribute || !is_string($attribute)) { throw new Exception('表单从模型映射数据时,fieldname不是一个合法的字符串:' . serialize($attribute)); } $default = $this->instance->{$attribute}; } $formField->setDefault($default); }
/** * @param KForm $form * @return void */ public function instanceMapTo(FormField $formField, KForm $form) { $formField->setDefault(null); }