We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

How to validate a Select list?

Hi there,

I have the following markup:

<select id="package_id" name="package_id">
    <option selected="selected" value="0">Please select...</option>
    <option value="1017">Taster</option>
    <option value="1018">Classic</option>
    <option value="1019">Deluxe</option>

validated as such:

    $package = new Select('package_id', $this->pw->select_id_title($entity, true));
    $package->setLabel('Available Photoshoot Packages');
    $package->addValidator(new ExclusionIn(array(
        'message' => 'A Shoots Package must be selected',
        'domain' => array('0')

The idea is that if a "real" value isn't selected from the list, it is posted back as "0" (and it is). However, this doesn't generate a validation error. Is there a well defined way to validate in this scenario?



Why don't you just do this?

$package = new Select('package_id', $this->pw->select_id_title($entity, true), array(
    'useEmpty' => true,
    'emptyText' => 'Please select...'
$package->setLabel('Available Photoshoot Packages');
$package->addValidator(new PresenceOf(array(
    'message' => 'A Shoots Package must be selected',