Reputation: 1065
In my form, I've a textfield that has been set with jquery-masked-input
, this is the jquery script:
<script>
jQuery(function ($) {
$("#reg-number").mask("99.999.999.9-999.999");
});
</script>
and then, the value will be saved into database without punctuation. So when I input value like this:
will be save in mysSQL database like this:
But I need to set this field as unique. So I add this validation in my model.php:
public function rules()
{
return [
[['regNumber'], 'required'],
[['regNumber'], 'string', 'max' => 32, 'min' => 15],
];
}
But it didn't work, I guess it because the value from textfield contain punctuation and in the database column doesn't contain punctuation.
Thanks
Upvotes: 0
Views: 141
Reputation: 9368
Try using Filter validator.
['regNumber', 'filter', 'skipOnArray' => true, 'filter' => function ($value) {
return preg_replace("/[^a-zA-Z 0-9]+/", "", $value);
}],
[['regNumber'], 'string', 'max' => 32, 'min' => 15],
[['regNumber'], 'unique'],
Not tested though :)
Upvotes: 1