New (Form) Element in Phalcon

There's a dynamic list of phone numbers, so I thought it would be a good idea to abstract this in a custom element.

There is a problem, though I don't know how to reuse existing elements, or how validation should work ($form->isValid() should check if the phone numbers match a certain pattern, for example).

How would I be able to implement that element?

edited Dec '17

I'd make a custom validator for it:
https://docs.phalconphp.com/en/3.2/validation#validators

As this feels like something that's been done before, I did a quick search for it, and came up with this:
https://github.com/mattdanger/phalcon-utils/blob/master/src/PhalconUtils/Validation/Validator/Phone.php

Looks like you'd just composer install it and you'd have the Phone validator available.
Not sure I'd rely on a strict Phone number validator though because they use different things in different countries.
One approach is to lead with a +, which is a more modern way to imply it's an international call as opposed to 011 to leave the country if it's the USA, or 00 from Germany, etc. There's a whole list of exit code here btw: https://www.howtocallabroad.com/codes.html

Another thing to keep in mind is extensions. The phone number might be a work line and you'd need to dial the extension afterward to reach the person or correct branch of the business.

Based on this answer: https://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation/123681#123681
I would say strip the inputted phone number of all characters except digits 0-9, the plus sign +, and the letter x, for extension.

You could also try a regexp something like:

^(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?$

This type of stuff can be very limiting, so I'd just trust whatever the user enters. Personally I'd validate it more like so:

Validate the field is not empty, then validate there's at least 7 characters in their input and at least one number.
Probably a million ways to approach this.



5.5k

ah, how do I integrate that validation with a custom element?

edited Dec '17

Depends what approach you want to use and how much you want to customize the validation.

If you decide on the regexp approach, you'd be able to approach it like so:
https://docs.phalconphp.com/en/3.2/api/Phalcon_Validation_Validator_Regex

$validator = new \Phalcon\Validation; // or a custom class which extends it with an initialize() that adds validators
$validator->add(
    "telephone",
    new \Phalcon\Validation\Validator\Regex(
        [
            "pattern" => "/^(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?$/",
            "message" => "The Phone number is invalid",
        ]
    )
);

You could also go more the approach, as shown in the documentation example like so:

<?php

use Phalcon\Validation;
use Phalcon\Validation\Validator\Regex;
use Phalcon\Validation\Validator\PresenceOf;

$validation = new Validation();

$validation->add(
    'telephone',
    new PresenceOf(
        [
            'message'      => 'The telephone is required',
            'cancelOnFail' => true,
        ]
    )
);

$validation->add(
    'telephone',
    new Regex(
        [
            'message' => 'The telephone is required',
            'pattern' => '/\+44 [0-9]+/',
        ]
    )
);

$validation->add(
    'telephone',
    new StringLength(
        [
            'messageMinimum' => 'The telephone is too short',
            'min'            => 2,
        ]
    )
);

But if you dislike the Phalcon\Validation\Validator\* validator logic already provided by Phalcon, and need the most control, the way to customize it fully would be to extend Phalcon\Validation\Validator and toss in a method public function validate(\Phalcon\Validation $validator, $attribute)

From there, you'd use it like it were Phalcon\Validation\Validator\Email or Phalcon\Validation\Validator\Regex etc.

Alternatively, you could also use Phalcon\Validation\Validator\Callback which is very powerful in that you can do anything you need within the callback function, writing your own custom logic.



5.5k

I am sorry but I don't get how I can integrate a custom element (which inherits from Element) with a validation? or is it always standalone?

edited Dec '17

Hi @falkdav like said @Ultimater you must to create form custom elements and then add your validators in it. You can create too your own validators if you need.

Documentation: Custom form elements and Custom validators

Good luck