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 rule always prepends {field} to the error message? #221

Open
johnshepherd opened this issue Oct 23, 2017 · 8 comments
Open

Custom rule always prepends {field} to the error message? #221

johnshepherd opened this issue Oct 23, 2017 · 8 comments

Comments

@johnshepherd
Copy link

Hello!

Having a little issue when adding a custom rule. Line 1127 of Validator.php does this:

'message' => '{field} ' . $message

This means when I add a custom rule like:

Valitron\Validator::addRule('postcode', function($field, $value, array $params, array $fields) {
	return false;
}, 'Please enter a valid postcode');

The error message is:

Postcode Please enter a valid postcode

Happy to submit a PR for this if it is incorrect?

@willemwollebrants
Copy link
Collaborator

There is a (somewhat illogical) way around this. If you define the message where you use the rule, {field} is not automatically prepended:

Valitron\Validator::addRule('postcode', function($field, $value, array $params, array $fields) {
	return false;
}, 'Please enter a valid postcode');

$v = new Valitron\Validator(['pc'=>3070]);
$v->rule('postcode', 'pc')->message('Please enter a valid postcode');

I'm not a big fan of the automatic prepending myself, and there have been some steps taken to fix it a little bit, but removing it would be a breaking change I'm afraid

@johnshepherd
Copy link
Author

Understood, thanks for the quick response. I'll probably just reword my error for now so it's like "Postcode must be valid".

@jatubio
Copy link
Contributor

jatubio commented Nov 2, 2017

@johnshepherd, you can also use something like: "Please enter a valid {field}".

After this Pullrequest: #210, if word {field} exists in message string, it's not prepended again.

@ajoah
Copy link

ajoah commented Dec 21, 2017

@johnshepherd I think this problem is still relevant. It's annoying if we want to add generic message.
Like the example in readme:

Valitron\Validator::addRule('alwaysFail', function($field, $value, array $params, array $fields) {
    return false;
}, 'Everything you do is wrong. You fail.');

The message will be : [fieldname] Everything you do is wrong. You fail.

I'm not a big fan of the automatic prepending myself, and there have been some steps taken to fix it a little bit, but removing it would be a breaking change I'm afraid

@willemwollebrants If we add a setting like this for example: (which was true by default)

Validator::prependErrorMessages(false)

and check this setting before add the prepend, we can remove the prepend without break anything, isn't it ?

@willemwollebrants
Copy link
Collaborator

My first reaction is: great idea, that might just work :)

Maybe we should also make it possible to override this in the constructor so that it works the same as $lang and $langDir.

@ajoah
Copy link

ajoah commented Dec 21, 2017

Yes, good idea :)

@erickloyalty
Copy link

I created a PR for this, it applies the prepend toggle to all fieldnames, #309

@gregorme
Copy link

I would like to suggest a better implementation of the logic for setPrependLabels().
Instead of adding the placeholder for {field} whenever it is missing and removing it from the output, the following solution would be better:

Method: rule()
Extend the condition to add the placeholder

if ($notContains && $this->prepend_labels) {
     $message = '{field} ' . $message;
} 

Method: checkAndSetLabel()
And the output always ensures that the placeholder is replaced if it is available.
Hyphens should also be taken into account.
$message = str_replace('{field}', ucwords(str_replace(['_','-'], ' ', $field)), $message);

So we have the option to use the {field} placeholder or not.

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

No branches or pull requests

6 participants