Danielss89 / ZfcUserAdmin

An admin interface for ZfcUser
36 stars 45 forks source link

Custom fields validation messages #64

Open kneekoo opened 9 years ago

kneekoo commented 9 years ago

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;
    }
}
rahuldroy commented 9 years ago

@kneekoo Can you write a wiki article as you are an expert at this now :).

As mentioned on the IRC channel, you can do it this way - https://gist.github.com/rahuldroy/eb5de146a3e65da5efcb

or a better way would be to extend the validator classes override the default message templates. Once the overriding is completed, it will just need to be invoked either in your Module.php or module.config.php

kneekoo commented 9 years ago

An expert newbie, being really optimistic. :P The solution you mentioned on IRC works, but it leads to repetition:

    The input was not found in the haystack
    An invalid sex type was entered
    An invalid sex type was entered

I'll see what I can do about the validator classes override. Thanks for the suggestions. :)