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

New Form Element don't bind loaded data from model

Hi,

I've created a new form element 'url' for the html-element <input type="url" ...>.

class Url extends Element
{
    public function __construct($name, $attributes=null)
    {
        parent::__construct($name, $attributes);
    }

    public function render($attributes = null) 
    {   
        if ($attributes != null && is_array($attributes))
        {
            foreach ($attributes as $new_attr => $value)
            {
                $this->_attributes[$new_attr] = $value;
            }
        }

        $html =  '<input type="url" ';
        foreach ($this->_attributes as $attr => $value)
        {
            $html .= $attr.'="'.$value.'" ';
        }
        $html .= ' />';
        return $html;
    }
}

Creating and updating works fine, but it does not show (or bind) the data to the edit-form from the loaded entity.

$note = EntitiesNotes::findFirstById($id);
$form = new NoteForm($note);

All standard elements that come with phalcon shows the stored data, but the url-field is empty. Rendered HTML-Code shows that the input has no value="" attribute.

Do I have to override another function of the Form\Element?

Thanks alot!

Thanks a lot! :)



13.8k

Thanks alot!

For other people searching to make custom form elements, below is my solution. I made a directory 'elements' in the 'app/forms' directory


<?php
namespace PhalconTime\Forms\Elements;

use Phalcon\Forms\Element;
use Phalcon\Forms\ElementInterface;

/**
 * PhalconTime\Forms\Elements\Time
 *
 * Component INPUT[type=time] for forms
 */
class Time extends Element implements ElementInterface
{

    /**
     * Renders the element widget returning html
     *
     * @param array $attributes
     * @return string
     */
    public function render($attributes = null)
    {
        return \Phalcon\Tag::TimeField($this->prepareAttributes($attributes));
    }
}

And in the form where i want to use it:


use PhalconTime\Forms\Elements\Time;

$startTime = new Time(
            "start_time",
            [
                "class" => "form-control",
            ]
        );
        $startTime->setLabel("Start time");
        $startTime->setFilters(
            [
                "striptags",
                "string",
            ]
        );
        $this->add($startTime);