Welcome Guest, Not a member yet? Register   Sign In
Validation Class passing field name
#1

(This post was last modified: 11-18-2021, 12:11 PM by sevmusic.)

How can I pass the field label to the validation class so I can return the error with the field label?

Previously, we could do a callback function with {field} and it would convert that into the field label to be returned with the error.

(ignore the goofy attempt at the validation rule checking for string length, I know that is already built in. This is strictly as an example)

MyController.php
PHP Code:
$this->validation->setRules([
    'first_name' => ['label' => 'First Name''rules' => 'myFieldCheck'],
]); 


MyValidationRules.php
PHP Code:
class MyValidationRules
{
    public function myFieldCheck(string $strstring &$error null): bool
    
{
        if(!strlen(trim($str)) > 0){
            $error 'The {field} field needs to be last least 1 character long.';
           return false;
       } else {
            return true;
        }
    }


How can I get the error to return: "The First Name field needs to be last least 1 character long."

Thank you for your attention.
Reply


Messages In This Thread
Validation Class passing field name - by sevmusic - 11-18-2021, 12:10 PM



Theme © iAndrew 2016 - Forum software by © MyBB