Skip to content

Custom fields validation messages #64

@kneekoo

Description

@kneekoo

This can easily become Wiki material but right now it's a call for help. I added new fields to ZfcUser. One of them is sex. My problem here is with the validation messages. They work just fine but while editing users, manipulating the form's value (I changed it to "s") I got these messages:

    The input was not found in the haystack
    The input does not match against pattern '/^(m|f)$/'
    An invalid sex type was entered

Obviously, I only want to display the third message whenever the input data doesn't match my criteria. Anything else is not user-friendly. Zend's Validation Messages[1] page demonstrates how you can override messages but it doesn't say how you can restrict certain messages or only display what you want. I looked for solutions on the web and I also went to the ZF irc channel, but I didn't find what I needed. How can we output only our validation messages?

[1] http://framework.zend.com/manual/2.0/en/modules/zend.validator.messages.html

Here are the details of my custom field:

module\ZfcUser\src\ZfcUser\Form\Base.php -> __construct:

        $this->add(array(
            'type' => 'radio',
            'name' => 'sex',
            'options' => array(
                'label' => 'Sex',
                'value_options' => array(
                    'm' => ' male',
                    'f' => ' female'
                ),
            ),
        ));

module\ZfcUser\src\ZfcUser\Form\RegisterFilter.php -> __construct:

        $this->add(array(
            'name'       => 'sex',
            'required'   => true,
            'validators' => array(
                array(
                    'name' => 'regex',
                    'options' => array(
                        'pattern' => '/^(m|f)$/'
                    )
                ),
                $this->sexValidator
            ),
        ));

module\ZfcUserAdmin\config\services.config.php -> $filter = new $registerFilter(

                new SexEdit(array(
                    'mapper' => $sm->get('zfcuser_user_mapper'),
                    'key' => 'sex'
                )),

module\ZfcUserAdmin\src\ZfcUserAdmin\Validator\SexEdit.php

namespace ZfcUserAdmin\Validator;

class SexEdit extends \ZfcUser\Validator\AbstractRecord
{
    public function isValid($value)
    {
        $valid = true;

        if (!preg_match("/^(m|f)$/", $value)) {
            $valid = false;
            $this->error(self::ERROR_BAD_SEX);
        }

        return $valid;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions