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

Help on forms

Hi All,

I have code below but showing me error:  Exception: Macro 'is_a' does not exist
Which part am i doing wrong or missing?

Controller
/**
 * Edits a user based on its id
 */
public function editAction($id)
{
    $collection = $this->collection;

    if (!$this->request->isPost()) {
        $criteria = array('_id' => new MongoId($id));           
        $users = $this->getCollection($collection, $criteria);

        if (!$users) {
            $this->flash->error("User was not found");
            return $this->forward("users/index");
        }            

        $users = (object) $users[0];

        $this->view->form = new RegisterForm($users, array('edit' => true));
    }
}  

Form <?php

use Phalcon\Forms\Form; use Phalcon\Forms\Element\Text; use Phalcon\Forms\Element\Password; use Phalcon\Validation\Validator\PresenceOf; use Phalcon\Validation\Validator\Email; use Phalcon\Forms\Element\Hidden;

class RegisterForm extends Form {

public function initialize($entity = null, $options = null)
{                        
    if (!isset($options['edit'])) {
        $element = new Text("id");
        $this->add($element->setLabel("Id"));            
    } else {
        $this->add(new Hidden("id"));            
    }

    // Name
    $name = new Text('name');
    $name->setLabel('Your Full Name');
    $name->setFilters(array('striptags', 'string'));
    $name->addValidators(array(
        new PresenceOf(array(
            'message' => 'Name is required'
        ))
    ));
    $this->add($name);

    // Name
    $name = new Text('username');
    $name->setLabel('Username');
    $name->setFilters(array('alpha'));
    $name->addValidators(array(
        new PresenceOf(array(
            'message' => 'Please enter your desired user name'
        ))
    ));
    $this->add($name);

    // Email
    $email = new Text('email');
    $email->setLabel('E-Mail');
    $email->setFilters('email');
    $email->addValidators(array(
        new PresenceOf(array(
            'message' => 'E-mail is required'
        )),
        new Email(array(
            'message' => 'E-mail is not valid'
        ))
    ));
    $this->add($email);

    // Password
    $password = new Password('password');
    $password->setLabel('Password');
    $password->addValidators(array(
        new PresenceOf(array(
            'message' => 'Password is required'
        ))
    ));
    $this->add($password);

    // Confirm Password
    $repeatPassword = new Password('repeatPassword');
    $repeatPassword->setLabel('Repeat Password');
    $repeatPassword->addValidators(array(
        new PresenceOf(array(
            'message' => 'Confirmation password is required'
        ))
    ));
    $this->add($repeatPassword);

}

}

View

{{ form("products/save", 'role': 'form') }}

<ul class="pager">
    <li class="previous pull-left">
        {{ link_to("products", "&larr; Go Back") }}
    </li>
    <li class="pull-right">
        {{ submit_button("Save", "class": "btn btn-success") }}
    </li>
</ul>

{{ content() }}

<h2>Edit user</h2>
<pre>

<fieldset>
    {% for element in form %}        
        {% if is_a(element, 'Phalcon\Forms\Element\Hidden') %}            
            {{ element }}
        {% else %}
            <div class="form-group">
                {{ element.label() }}
                {{ element.render(['class': 'form-control']) }}
            </div>
        {% endif %}
    {% endfor %}

</fieldset>

</form>

The problem is not with forms. The problem is that Volt does not recognize the IS_A function you are using.

Volt does not have all php functions built in. You can read here on how to extend votl: https://docs.phalcon.io/en/latest/reference/volt.html#extending-volt

And check Invo Demo where they add this function to volt: https://github.com/phalcon/invo/blob/master/app/config/services.php

thank you @nikolay

Can you mark thread as solved so others can use it?