We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

set Default value in custom Form/Element

Hello , I created my own autocomplete using the datalist html5 . This works fine . But now I need set a default value to edit . As I can customize the method $elemento->setDefault (); for setting the input and datalist

class SimpleDataList extends \Phalcon\Forms\Element implements \Phalcon\Forms\ElementInterface
  {
    public function  __construct($name, array $attributes)
    {

    parent::__construct($name, $attributes);
    }

    /**
    * 0: modelo,
    * 1: string columna del datalist
    * 2: array atributos del input
    * @param null $attributes
    * @return string|void
    */
    public function render($attributes = null)
    {
      $name           = $this->getName();
      $modelo         = $this->getAttributes()[0];
      $columna        = $this->getAttributes()[1];
      $atributosInput = $this->getAttributes()[2];

      $input = "<input type='text' id='$name' name='$name' list='list_$name' autocomplete='off' ";
      $datalist = "<datalist  id='list_$name' >";
      foreach ($atributosInput as $clave => $valor)
      {
              $input .= " $clave = '$valor' ";
      }
      foreach ($modelo as $m) 
      {
              $datalist .= "<option value='" . $m->$columna . "'></option> \n ";

      }
      $input      .= ">\n ";
      $datalist   .= "</datalist> \n ";
      $retorno     = $input . $datalist;
      return $retorno;

    }

  }

  $elemento = new SimpleDataList('memo_sectorDestino',
        array(
        Sectores::find(array('sector_activo=1', 'order' => 'sector_nombre')),
        'sector_nombre',
        array(
            'placeholder' => 'Ingrese el destino',
            'class' => 'form-control',
            'maxlength' => 70,
            $required['required'] => 'true'
        )));

    $elemento->setLabel( ' Sector Destino ');
    $this->add($elemento);

<label for="memo_sectorDestino">Sector Destino </label>
<input type="text" id="memo_sectorDestino" name="memo_sectorDestino" list="list_memo_sectorDestino" autocomplete="off" placeholder="Ingrese el destino" class="form-control" maxlength="70" required="true">
<datalist id="list_memo_sectorDestino">
        <option value="ADMINISTRACIÓN GRAL."></option> 
         <option value="ASESORIA LEGAL"></option> 
         <option value="ASESORÍA MÉDICA"></option> 
         <option value="Asesoría técnica"></option> 
         <option value="COMPLEJO CAVIAHUE"></option> 
         <option value="COMPLEJO LAS GRUTAS"></option> 
         <option value="COMPLEJO SAN MARTÍN DE LOS ANDES"></option> 
         <option value="COMPLEJO VILLA LA ANGOSTURA"></option> 
         <option value="CONSEJO DE ADMINISTRACIÓN"></option> 
</datalist>

just use value=>$value