Search code examples
model-view-controllerphalcon

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?


Solution

  • You can use a regex, there's an example on the official documentation:

    <?php
    
    use Phalcon\Validation;
    use Phalcon\Validation\Validator\Regex;
    
    $validation = new Validation();
    
    $validation->add(
        'telephone',
        new Regex(
            [
                'message'    => 'The telephone is required',
                'pattern'    => '/\+44 [0-9]+/',
                'allowEmpty' => true,
            ]
        )
    );
    

    https://docs.phalconphp.com/hu/3.2/validation#cancelling


    Or you can use a better regex pattern:

    $regex = "/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}[\s-]?\d{4}$/i";
    

    https://ericholmes.ca/php-phone-number-validation-revisited/