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

How to declare a function for check box field in VOLT?

I am using the volt syntax to display the check box options

Ex: {{ check_filed('name':'myClass')}}

Generally in volt it will automatically call the function based on the volt file name action function in controller but I don't want like that. I need to call the function seperately for every checkbox(if I have multi check box). I am not using any .js file.

Here how could I call the function in controller(php file) from volt file(When using check box)? Is it possible?

Hi @Karthik261193 you can use a controller as a service or create a User Component and use it like proxy

Good luck

Hi @Karthik261193 you can use a controller as a service or create a User Component and use it like proxy

Good luck

Hi @Degiovanni Emilio. Thanks for the reply. If possible could you please explain it in detail I am completely to new to this. I can't able to understand this.

Thank you



13.8k

Hi @Karthik261193 you can use a controller as a service or create a User Component and use it like proxy

Good luck

Hi @Degiovanni Emilio. Thanks for the reply. If possible could you please explain it in detail I am completely to new to this. I can't able to understand this.

Thank you

Do you mean you want to use a custom function for a field that does not exist by default in volt?

Hi @Karthik261193 you can use a controller as a service or create a User Component and use it like proxy

Good luck

Hi @Degiovanni Emilio. Thanks for the reply. If possible could you please explain it in detail I am completely to new to this. I can't able to understand this.

Thank you

Do you mean you want to use a custom function for a field that does not exist by default in volt?

Yes exactly



13.8k
edited Aug '17

You can make your own form fields like this app/forms/elements/FormElementObjectName.php (it's not required to have the same location for the file), see below example.


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

Then in a form object use it like this (see namespace of custom form element and the part 'new Time..')


namespace PhalconTime\Forms;

use Phalcon\Forms\Form;
use Phalcon\Forms\Element\Text;
use Phalcon\Forms\Element\TextArea;
use Phalcon\Forms\Element\Hidden;
use Phalcon\Forms\Element\Select;
use Phalcon\Forms\Element\Date;
use Phalcon\Validation\Validator\PresenceOf;
use Phalcon\Forms\Element\Numeric;

use PhalconTime\Forms\Elements\Time;

use PhalconTime\Models\Project;
use PhalconTime\Models\TimeType;

class TimeregistrationForm extends Form
{
    /**
     * Initialize the time registration form
     *
     * @param mixed $entity
     * @param array $options
     */
    public function initialize($entity = null, $options = [])
    {

        if(isset($options['edit']) && $options['edit'] === TRUE) {
            $id = new Hidden(
                "id", [
                    "class" => "hidden",
                ]
            );
            $this->add($id);
        }

        // Project
        $project = new Select('project_id', Project::find(), [
            'using'      => ['id', 'name'],
            'useEmpty'   => true,
            'emptyText'  => 'Select project',
            'emptyValue' => '',
            'class'      => 'form-control',
        ]);
        $project->setLabel('Project');
        $project->addValidators(
            [
                new PresenceOf(
                    [
                        "message" => "Selecting a project is required",
                    ]
                )
            ]
        );
        $this->add($project);

        // Time type
        $timeType = new Select('time_type_id', TimeType::find(), [
            'using'      => ['id', 'name'],
            'useEmpty'   => true,
            'emptyText'  => 'Select time type',
            'emptyValue' => '',
            'class'      => 'form-control',
        ]);
        $timeType->setLabel('Time type');
        $timeType->addValidators(
            [
                new PresenceOf(
                    [
                        "message" => "Selecting a time type is required",
                    ]
                )
            ]
        );
        $this->add($timeType);

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

        // End time
        $endTime = new Time(
            "end_time",
            [
                "class" => "form-control",
                "step"  => "60"
            ]
        );
        $endTime->setLabel("End time");
        $endTime->setFilters(
            [
                "striptags",
                "string",
            ]
        );
        $this->add($endTime);

        // Total time
        $totalTime = new Time(
            "total_time",
            [
                "placeholder"   => "00:01 (not required if using start/end date/time)",
                "class"         => "form-control",
                "step"          => "60"
            ]
        );
        $totalTime->setLabel("Total time");
        $totalTime->setFilters(
            [
                "striptags",
                "string",
            ]
        );
        $this->add($totalTime);

        // Field description
        $description = new TextArea(
            "description",
            [
                "placeholder" => "Description",
                "class"       => "form-control",
                "id"          => "textarea",
            ]
        );
        $description->setLabel("Description");
        $timeType->addValidators(
            [
                new PresenceOf(
                    [
                        "message" => "Description is required",
                    ]
                )
            ]
        );
        $description->setFilters(
            [
                "string",
            ]
        );
        $this->add($description);

    }

}

I hope I have understand your question well though. It's also possible to create your own functions for .volt if the function doesn't exist yet.