Before the problem came out… I was inputting some values into the simple form fields and after saving form, they were inserted/updated to the model.
For example in the field "Address code" user was entering 5 digits: 01180. And to the database tha string value "01180" (can start with zero) was inserted/updated.
Once I was using jQuery masking feature which is nice, so I though to apply it again. It was really nice and easy, and user was clicking the same keys, but as the result in text value he had 01-180, which is used officially in Poland (zip code). The same with other fields, example: telephone number which is written with (brackets) and "-" pauses.
And my concern is as follows:
I prefer saving this values as strings without brackets and pauses and spaces. Just pure alphanumeric strings, because it will be easier to operate on them. I would like user to have possibility to enter 01-180 on the form with masking plug-in but when I save form to the database I would like to save unmasked value "01180".
But when I applied jQuery mask plug-in and tried to save form, instantly errors occurred. Off course text field length increased:
So my question is: can I unmask these CMaskedTextField values "on the fly" before saving them? Remove the placeholders? In controller? In some filter? Would it be great effort to do so? Also what about fetching them into the masked form, for example in database I have "01180", but on form I want it to load as "01-180" (form is already masked) and vice versa I want to save 01-180 as "01180".
Should I now change in model.php length of every masked field, and also change database accordingly (resize columns) - doesn’t sound nice… also validation rules would need update
Indeed the function beforeValidate() is working (I strip placeholders in there), now I don’t get info that address_code is wrong (it passes validation because stripped length is 5), but the CMaskedTextField is cleared, unlike other fields which passed validation and stay filled with the entered values.
Can yii leave the masked value in the CMaskedTextField?
Or maybe it will be better to save the CMaskedTextField as is to database? How do you think, but then I would have to change database columns, change model code etc…
Well, eventually I resized columns in database and updated validation rules in model so masked strings could fit. Quickest solution, however not that perfect as saving stripped values. Unfortunately I could not find examples of how to apply custom validator to achieve my goal. Thanks for interest anyway.