With this extension, you can validate a form based on your conditions.
Use the variable $fields
to access fields values (eg $fields["field_id"]
). The validation succeeds only if the PHP code does not return or return false
.
If the code returns a string, then the string is returned as an error. If it returns [ $field_id, $error_message ]
, the error message will be reported for the specific field.
Examples
Return an error if the password field is < 8 characters
if( strlen( $fields['password'] ) < 8 ) {
return ['password', 'The password requires at least 8 characters' ];
}
Return an error if the date choice is before today
if( $fields['date'] < date( "Y-m-d" ) ) {
return ['date', 'You can\'t choose a date before today' ];
}
Accepts only time between 7 PM and 10 PM
if( $fields['time'] < 19 || $fields['time'] > 22 ) {
return ['time', 'You must to choose a time between 7PM and 10PM' ];
}
Return an error unless at least one of two fields is filled (Stripe and Paypal for payment in the example).
if( empty( $fields['stripe'] ) && empty( $fields['paypal'] ) ) {
return 'At least one payment is required.';
}