We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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.7k

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);