Create new form Element

i Dont understand how to create new Form Element and use it

i got > 'App\Library' => "../app/library/", in loader File : App/library/Elements <? namespace Phalcon\Forms\Element;

use Phalcon\Tag; use Phalcon\Forms\Element;

class tel extends Element { public function render(attributes = null) -> string { return Tag::telField(this->prepareAttributes(attributes, true)); } }

How to use it ? if i do this in Form use Phalcon\Forms\Element\Tel;

i got error Class not found ..

Please help me

Here is example of Google Recaptcha which I made into custom form element.

Recaptcha.php

namespace Frontend\Forms\lib;

use Phalcon\Forms\Element;

class Recaptcha extends Element
{
    public function render($attributes = null)
    {
        $config = loadConfigFile('config');
        $language = \Phalcon\DI::getDefault()->getSession()->language;
        $html = '<script src="https://www.google.com/recaptcha/api.js?hl='. $language .'"></script>';
        $html.= '<script>function recaptchaCallback() { document.getElementById("recaptchaTemp").setAttribute("value", "checked") }</script>';
        $html.= '<div class="g-recaptcha" data-callback="recaptchaCallback" data-sitekey="'. $config->sensitive->recaptcha->publicKey .'"></div>';
        $html.= '<input type="hidden" name="recaptchaTemp" id="recaptchaTemp" value=""/>';
        return $html;
    }
}

RecaptchaValidator.php

namespace Frontend\Forms\lib;

use Phalcon\Validation\Validator;
use Phalcon\Validation\ValidatorInterface;
use Phalcon\Validation\Message;

class RecaptchaValidator extends Validator implements ValidatorInterface
{
    public function validate(\Phalcon\Validation $validation, $attribute)
    {
        $value = $validation->getValue('g-recaptcha-response');
        $ip = $validation->request->getClientAddress();

        if (!$this->verify($value, $ip)) {
            $validation->appendMessage(new Message($this->getOption('message'), $attribute, 'Recaptcha'));
            return false;
        }
        return true;
    }

    protected function verify($value, $ip)
    {
        $config = loadConfigFile('config');
        $params = [
            'secret' => $config->sensitive->recaptcha->privateKey,
            'response' => $value,
            'remoteip' => $ip
        ];
        $response = json_decode(file_get_contents('https://www.google.com/recaptcha/api/siteverify?' . http_build_query($params)));
        return (bool) $response->success;
    }
}

Usage in form class

$recaptcha = new lib\Recaptcha($name);
$recaptcha->addValidator(new lib\RecaptchaValidator([
    'message' => $this->translations->public->validation->recaptcha
]));
return $recaptcha;

You can see the folder structure by the namespaces.

Thank it will help me alot !

edited Jan '17

well in th controller :


    use Phalcon\Forms\Form,
    Phalcon\Forms\Element\Text,
    Phalcon\Forms\Element\Password,
    Phalcon\Forms\Element\Hidden;

class IndexController extends ControllerBase
{
    public function IndexAction()
    {
    $form = new Form;

      $element['security'] = new Hidden( "security" ,[
        'name'  => $this->security->getTokenKey(),
        'value' => $this->security->getToken()
      ]);

      $element['username'] = new Text( "username" ,[
        'required'     => true,
      ]);

      foreach ($element as $e)
      {
        $form->add($e);
      }

      $this->view->form = $form; // pass to the view

and then in the view:

    username : <php echo $form->render("username");?>