#1333
Answered
6/27/24, 3:40 PM
MartyB
Answers: 1

Custor error message

Hi there!

I definitely love the Jet Form system! Work with it is sooo smooth! However, I need to create custom error message for some of the form fileds. And more ... This message must be customized. I know how to define standard error messages, but how to define error fully customizable error message?

Thanks in advance!

Answers (1)

#1460
6/28/24, 8:55 PM
PHPJetTeam
1

Hi :-)

I am glad you like it.

And for sure, it is possible to define customizable error message. Let's see example, it is better than 1000 words ;-)


$URLs_field = new Form_Field_Input(name'URL',label'URL:');

$URLs_field->setErrorMessages([
    
'not_unique_URL' => 'URL %URL% is not unique. It is already used by %URL_USEAGE%',
    
'invalid_URL' => 'URL %URL% is not valid'
]);
$URLs_field->setValidator( function() use ($URLs_field) : bool {
    
$URL $URLs_field->getValue();

    if(!
$URL) {
        
$URLs_field->setErrorForm_Field::ERROR_CODE_EMPTY );
        return 
false;
    }

    if( 
URL_Checker::URLUsed$URL$URL_usage ) ) {
        
$URLs_field->setError('not_unique_URL', [
            
'URL' => $URL,
            
'URL_USEAGE' => $URL_usage
        
]);

        return 
false;
    }

    if(!
filter_var('https://'.$URLFILTER_VALIDATE_URL)) {
        
$URLs_field->setError('invalid_URL', [
           
'URL' => $URL
        
]);
        return 
false;
    }

    return 
true;
} );

As you can see, it is possible to define custom error messages with custom codes


$URLs_field->setErrorMessages([
    
'not_unique_URL' => 'URL %URL% is not unique. It is already used by %URL_USEAGE%',
    
'invalid_URL' => 'URL %URL% is not valid'
]);

and then set these errors and define values for the error message:


$URLs_field->setError('not_unique_URL', [
            
'URL' => $URL,
            
'URL_USEAGE' => $URL_usage
        
]);
Your Answer