Welcome Guest, Not a member yet? Register   Sign In
%s values in custom error messages
#1

[eluser]wdm*[/eluser]
I'm displaying the form validation errors individually inline with the form with

Code:
<?= form_error('my_field') ?>

So the field name is not needed in the error message, it should rather just read e.g.

Quote:must be at least %s characters in length

The problem I have is that the %s gets replaced with the field name, not the number.

Looking at the original string in language/english/form_validation_lang.php it looks like the first %s is always the name, the second %s always the number

Is there any way to change this?
#2

[eluser]xwero[/eluser]
Short answer no.

The only way i can think of to do this with the least amount of code is
Code:
$at_least = 3;
$this->validation_form->set_rules('field','Field name','at_least['.$at_least.']');
$this->validation_form->set_message('at_least','must be at least '.$at_least.' characters in length');
Of course you can put the message itself in a language file and add the right number with the sprintf funtion
Code:
$this->validation_form->set_message('at_least',sprintf(lang('at_least'),$at_least));
#3

[eluser]wr5aw[/eluser]
You could override [your system folder]/language/english/form_validation_lang.php by copying it to [your application folder]/language/english/form_validation_lang.php. Then just make the change in the copy.

From the user guide (v1.7):
Quote:Language files are typically stored in your system/language directory. Alternately you can create a folder called language inside your application folder and store them there. CodeIgniter will look first in your system/application/language directory. If the directory does not exist or the specified language is not located there CI will instead look in your global system/language folder.
#4

[eluser]xwero[/eluser]
wr5aw how will that solve his problem wanting the param to be the first/only thing that needs to be replaced in the error message?
#5

[eluser]wr5aw[/eluser]
[quote author="xwero" date="1225304002"]wr5aw how will that solve his problem wanting the param to be the first/only thing that needs to be replaced in the error message?[/quote]
You're right. I should know better than to post off the top of my head before morning coffee. :gulp:
#6

[eluser]drewbee[/eluser]
Can this not simply be taken care of by identifying the current %s in the message? CI usess sprint_f to do this, so it is a breeze...

%1$s = accesses first passed variable
%2$s = accesses second passed variable

So the error message should be...

"must be at least %2$s characters in length"


I also display my error messages in-line and this worked like a breeze.
#7

[eluser]xwero[/eluser]
nice solution drewbee!




Theme © iAndrew 2016 - Forum software by © MyBB