Following the input validation document, I have created an inline validator that works as expected from the server side. I would like to have it function client side as well. This is not for an image file upload. It is to check that the string ends in an allowable file extension.
In the components section of my configuration file (web.php) I added
I need to trigger the function’s error message when the user tabs/clcick another field prior to submission.
['product_image', 'allowProductImage', 'when' => function ($model) {
return $model->product_image != '';},
'whenClient' => "function (attribute, value) {
return $('#product_image').val() != '';
}"],
public function allowProductImage($attribute, $params){
if (!preg_match('/\.(?:jpg|tif|eps)$/', $this->$attribute))
$this->addError($attribute,'Image file must end with .eps, .tif or .jpg');
}