Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom fields validation messages #64

Open
kneekoo opened this issue Oct 24, 2014 · 2 comments
Open

Custom fields validation messages #64

kneekoo opened this issue Oct 24, 2014 · 2 comments

Comments

@kneekoo
Copy link

kneekoo commented Oct 24, 2014

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
Copy link

@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
Copy link
Author

kneekoo commented Oct 24, 2014

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. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants