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>
</select>

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')
    )));
    $this->add($package);

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?

Thanks.



308

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',
)));
$this->add($package);