how focus right text field on custom form field template
- alnux
- Member | 139
hi there , tell this is very dificult to me becouse i am spanish language
mother.
well i made the next form
public function VentaEmpresaForm()
{
$this->form->addText('cliente_id', 'Cliente')
->setAttribute('placeholder', 'Seleccione el cliente')
->addRule(Form::MAX_LENGTH, 'Maximo de caracteres %d',100)
->addRule(Form::PATTERN, 'Solo puede usar números, letras, espacios y puntos','[a-zA-ZñÑ0-9. ]+');
$this->form->addText('estado', 'Estado')
->setRequired('Estado de la venta venta')
->setAttribute('placeholder', 'Estado de la venta')
->addRule(Form::MAX_LENGTH, 'Maximo de caracteres %d',100)
->addRule(Form::PATTERN, 'Solo puede usar letras numeros y espacio','[a-zA-ZñÑ0-9 ]+');
$prod_raw=[
'{"1":3.5}'=>"Hielo grande",
'{"2":2.5}'=>"Hielo pequeño",
'{"3":3.5}'=>"Agua",
];
$this->form->addCheckboxList('productos_raw', 'Producto Raw', $prod_raw);
$this->form->addHidden('producto_id');
$this->form->addHidden('preciounitario');
$this->form->addText('cantidad', 'Cantidad')
->setAttribute('placeholder', 'Cantidad del producto')
->addRule(Form::MAX_LENGTH, 'Maximo de caracteres %d',45)
->addRule(Form::PATTERN, 'Solo puede usar números','[0-9]+');
$this->form->addText('observaciones', 'Observaciones')
->setAttribute('rows', 4)
->setAttribute('placeholder', 'Por favor ingrese las observaciones de la venta')
->addRule(Form::MAX_LENGTH, 'Maximo de caracteres %d',250)
->addRule(Form::PATTERN, 'Solo puede usar letras, numeros, signos + - /','[a-zA-ZñÑ0-9. +-/]+');
if(!$this->presenter->getParameter('postId'))
{
$this->form->addSubmit('send', 'Realizar Venta');
}
else
{
$this->form->addSubmit('send', 'Editar Venta');
}
$this->form->onSuccess[] = callback($this, 'VentaEmpresaProcess');
return $this->form;
and i made a dynamic template form
{foreach $form['productos_raw']->items as $key => $item}
{var $nombreid = str_replace(' ', '', $item)}
{var $productoid = 'producto'.$nombreid}
<label>
<input n:name="productos_raw:$key" id="{$nombreid}" onchange="insertarProducto({$nombreid})"> {$item}
</label>
<div id="producto{$nombreid}" style="display:none;" class="form-group" onmouseover="divFocus({$productoid})">
<span n:ifset="$form['productos_raw']->errors[0]" style="color:red; font-size:small; opacity:0.4;">
{$form['productos_raw']->errors[0]}
</span>
{input $form['producto_id']->name name=>"{$form['producto_id']->name}[]", id=>"{$form['producto_id']->name}", class=>"form-control"}
{input $form['preciounitario']->name name=>"{$form['preciounitario']->name}[]", id=>"{$form['preciounitario']->name}", class=>"form-control"}
{input $form['cantidad']->name name=>"{$form['cantidad']->name}[]", id=>"{$form['cantidad']->name}", class=>"form-control", type=>"number",min=>"1", onchange=>"obtenerMonto()"}
{input $form['observaciones']->name name=>"{$form['observaciones']->name}[]", id=>"{$form['observaciones']->name}", class=>"form-control"}
</div>
{/foreach}
if you see on template i declare text fields as arrays instead of regular field
name=>"{$form['producto_id']->name}[]"
the question is when i type a incorrect key (like return key) it show me the error alert But as there are several fields with the same name (with[]) the focus goes to the first one of the same name Although I was wrong in the second one, how can i made that the focus goes to the one that i made the wrong input
i hope that you understand me
Last edited by alnux (2017-05-12 15:49)
- GEpic
- Member | 566
Hello, don't know if I understood it correctly, but are you looking for this
(autofocus attribute)?
https://www.w3schools.com/…utofocus.asp
Last edited by GEpic (2017-05-12 16:00)
- alnux
- Member | 139
sorry but that one is when the page loads, my problem is when the data-nette-rules detect an error the focus goes to the first one of field text array there is, even if I make the mistake in the second field.
please saw on template code.
Last edited by alnux (2017-05-12 17:46)