Welcome Guest, Not a member yet? Register   Sign In
{field}, {param} and {value} tags not working in model validation?
#1

I searched a few different terms but could not locate the answer so I apologies if this has already been covered.
I am using the CodeIgniter 4 Model validation rules 
PHP Code:
protected $validationRules = [
 
'system_group_id' => 'required',
'health_system_id' => 'required',
 
'group_id' => 'required',
 
'display_name' => 'required',
 
'short_name' => 'required|short_name',
 
'is_menu' => 'required',
 
'is_active' => 'required',
 ]; 

I created a validation rule for "short_name" and attached it to CI4 and just to test if anything was working I added all of the tags indicated on here: https://codeigniter.com/user_guide/libra...r-messages (I have provided just the short_name validation function below)


PHP Code:
public function short_name(string $strstring &$error null): bool
{
$error '{field}, {param} and {value} must only contain uppercase letters.';

return (
strtoupper($str) == $str);


I know it's working because it returns the error like it should but it does not replace the tags at all?

I am literally getting "{field}, {param} and {value} must only contain uppercase letters."

I am calling the model validation from the REST resource controller type with

PHP Code:
public function update($id null)
{
$input $this->request->getRawInput();

return (!
$this->model->update($id$input)) ? $this->failValidationErrors($this->model->errors()) : $this->respondUpdated();



Is the model class not building and/or handling the validation rules attached to it correctly or am I missing something else?

https://codeigniter.com/user_guide/model...ating-data
Reply
#2

Change your single quotes to double quotes.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(This post was last modified: 06-29-2021, 04:01 AM by dmyers.)

InsiteFX unfortunately that didn't change anything it is still showing {field}, {param} and {value} must only contain uppercase letters. in my error dialog
Reply
#4

(This post was last modified: 06-29-2021, 08:33 AM by paulbalandan.)

Try this:
PHP Code:
protected $validationRules = [
    ...,
    'short_name' => [
        'rules' => 'required|short_name',
        'errors' =>['short_name' =>  '{field}, {param} and {value} must only contain uppercase letters.'],
    ],
    ...,
];

public function 
short_name(string $strstring &$error null): bool
{
    return strtoupper($str) === $str;

Reply
#5

It looks like the tagging only works if you have the entry in the language Validation.php file in my case at ./app/Language/en/Validation.php

it totally disregards tags in the $error = '' variable reference inside the validation rule? Seems pointless to even supply that then except for the most basic errors?

"On this form 1 or more fields should contain only uppercase letters." -- I mean this jokingly of course.

Should this be filed as a Bug?
Reply
#6

Have you tried my suggestion?

Based on the code of Validation, if the error string in the custom validator function is not null, it will be fetched right away and returned immediately (No replacements of placeholders to be made). So you need to pass in your custom errors beforehand.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB