使用下拉列表的表单在 Phalcon 上不起作用
Form using dropdown doesn't work on Phalcon
我正在使用 Phalcon 框架。我正在尝试创建一个表单以从名为 cliente 的 table(在 mysql 中)获取一个值(一个名为 "idcliente" 的 ID),它有 2 列:"idcliente"和 "nombre"。使用此值,我想在另一个名为 Users 的 table 上更新一个字段(也是 "idcliente")。
我的表格是这样的:
class AsignarclienteForm extends Form{
public function initialize($entity = null, $options = null){
$idcliente = new Select('idcliente',[
Cliente::find(),
"useEmpty" => true,
"emptyText" => "Please select...",
"using" => ["idcliente", "nombre"],
]);
$idcliente->setLabel('ID Cliente');
$idcliente->addValidators(array(
new PresenceOf(array(
'message' => 'idcliente is required'
))
));
$this->add($idcliente);
}
}
我的控制器:
public function asignarclienteAction(){
$auth = $this->session->get('auth');
$permiso = $auth['active'];
$id = $auth['id'];
if($permiso!='A'){return $this->forward('servicios/index');}
$form = new AsignarclienteForm;
if ($this->request->isPost()) {
$idcliente = $this->request->getPost('idcliente');
$sql = "UPDATE Users SET idcliente = ?0 WHERE id = ?1";
$this->modelsManager->executeQuery($sql, array(0 => $idcliente, 1 => $id));
return $this->forward('admin/usuarios');
}
$this->view->form = $form;
}
而我的观点:
<div class="panel-body">
{{ form('admin/asignarcliente/', 'id': 'asignarclienteForm', 'onbeforesubmit': 'return false') }}
<fieldset>
<div class="control-group">
{{ form.label('idcliente', ['class': 'control-label']) }}
<div class="controls">
{{ form.render('idcliente', ['class': 'form-control']) }}
</div>
</div>
<div class="form-actions">
{{ submit_button('Asignar', 'class': 'btn btn-primary', 'onclick': 'return SignUp.validate();') }}
</div>
</fieldset>
</form>
</div>
我在网站中遇到以下错误:
ID Cliente
Catchable fatal error: Object of class Phalcon\Mvc\Model\Resultset\Simple could not be converted to string in C:\xampp\htdocs\OpinionZoom\cache\volt\c%%%%xampp%%htdocs%%opinionzoom%%app%%views%%admin%%asignarcliente.volt.php on line 31
第 31 行是 {{ form.render('idcliente', ['class': 'form-control']) }}
在我看来
尽管我创建了很多表单,但我还没有找到足够的文档来说明如何使用 select 创建表单。
如果有人能帮助我,我将不胜感激。谢谢。
您的表单 Asignarclienteform
中的元素定义不正确。
Select
的第一个参数必须是字符串(元素的名称)。
第二个参数采用 select 元素的选项。
// Select construct parameters
Select(string $name, [object | array $options], [array $attributes])
我把数组中的idcliente
移到了第一个参数位置:
$idcliente = new Select('idcliente', Cliente::find(), [
"useEmpty" => true,
"emptyText" => "Please select...",
"using" => ["idcliente", "nombre"],
]);
我正在使用 Phalcon 框架。我正在尝试创建一个表单以从名为 cliente 的 table(在 mysql 中)获取一个值(一个名为 "idcliente" 的 ID),它有 2 列:"idcliente"和 "nombre"。使用此值,我想在另一个名为 Users 的 table 上更新一个字段(也是 "idcliente")。
我的表格是这样的:
class AsignarclienteForm extends Form{
public function initialize($entity = null, $options = null){
$idcliente = new Select('idcliente',[
Cliente::find(),
"useEmpty" => true,
"emptyText" => "Please select...",
"using" => ["idcliente", "nombre"],
]);
$idcliente->setLabel('ID Cliente');
$idcliente->addValidators(array(
new PresenceOf(array(
'message' => 'idcliente is required'
))
));
$this->add($idcliente);
}
}
我的控制器:
public function asignarclienteAction(){
$auth = $this->session->get('auth');
$permiso = $auth['active'];
$id = $auth['id'];
if($permiso!='A'){return $this->forward('servicios/index');}
$form = new AsignarclienteForm;
if ($this->request->isPost()) {
$idcliente = $this->request->getPost('idcliente');
$sql = "UPDATE Users SET idcliente = ?0 WHERE id = ?1";
$this->modelsManager->executeQuery($sql, array(0 => $idcliente, 1 => $id));
return $this->forward('admin/usuarios');
}
$this->view->form = $form;
}
而我的观点:
<div class="panel-body">
{{ form('admin/asignarcliente/', 'id': 'asignarclienteForm', 'onbeforesubmit': 'return false') }}
<fieldset>
<div class="control-group">
{{ form.label('idcliente', ['class': 'control-label']) }}
<div class="controls">
{{ form.render('idcliente', ['class': 'form-control']) }}
</div>
</div>
<div class="form-actions">
{{ submit_button('Asignar', 'class': 'btn btn-primary', 'onclick': 'return SignUp.validate();') }}
</div>
</fieldset>
</form>
</div>
我在网站中遇到以下错误:
ID Cliente Catchable fatal error: Object of class Phalcon\Mvc\Model\Resultset\Simple could not be converted to string in C:\xampp\htdocs\OpinionZoom\cache\volt\c%%%%xampp%%htdocs%%opinionzoom%%app%%views%%admin%%asignarcliente.volt.php on line 31
第 31 行是 {{ form.render('idcliente', ['class': 'form-control']) }}
在我看来
尽管我创建了很多表单,但我还没有找到足够的文档来说明如何使用 select 创建表单。
如果有人能帮助我,我将不胜感激。谢谢。
您的表单 Asignarclienteform
中的元素定义不正确。
Select
的第一个参数必须是字符串(元素的名称)。
第二个参数采用 select 元素的选项。
// Select construct parameters
Select(string $name, [object | array $options], [array $attributes])
我把数组中的idcliente
移到了第一个参数位置:
$idcliente = new Select('idcliente', Cliente::find(), [
"useEmpty" => true,
"emptyText" => "Please select...",
"using" => ["idcliente", "nombre"],
]);