Search code examples
phpzend-frameworkzend-form-element

Default value on Zend_Form_Element_Select


There is still a part of me which things this Zend thing is a horrible mess that some how forgives it self at a later stage, But it follows no structure and the documentation is horrible, Which always leads me to a fortunate place which is stackoverflow.

So my problem is, That im trying to initialize a <select> and i want to have a default value of Please Choose One, If a user end up choosing Please Choose One as a value, I want to throw an error. The way im doing it dosent work because i cant negative match the regex, Or Can I?

$pubs = array(
        "Please Choose One" => "Please Choose One",
        "BBC" => "BBC",
        "Channel 4" => "Channel 4"
);

$channels = new Zend_Form_Element_Select("publication");
$channels->setLabel("Channels")
    ->setRequired(true)
    ->addMultiOptions($pubs)
    ->addValidator('regex', true, array(
            "pattern" => "/Please Choose One/",
            "messages" => array("regexNotMatch" => "Please choose a publisher")
        )
    )
    ->setValue("Please Choose One"); 

P.S I dont mind changing the thing around and use a better methodology.


Solution

  • What about using Zend_Validate_InArray instead? It would avoid the need to negate the regexp (which is always a bit tricky).

    In your case, it would be something like

    $channels->addValidator('inArray', true, array("BBC" => "BBC", "Channel 4" => "Channel 4"));

    Hope that helps,