A field defined as readonly
is intended only to present information to the user in a way that is sent together with the data in the submission of the form. As the name itself says is a read-only field, so it is assumed that the field has the attribute value
, even if empty (because an empty value can be a valid value).
Why does he return true
in the validation?
Because it will always be valid. Values in a field readonly
will be defined by the application itself and therefore will always be valid. However, it does not return true
for this reason, in fact a field readonly
nor goes through validation.
In accordance with the W3C:
Constraint validation: If the readonly
attribute is specified on an input element, the element is barred from Constraint validation.
That is, if the attribute readonly
is specified in a field, the element is barred from the validation.
So how can I validate a field readonly
?
It doesn’t validate. Not the field, at least. You mentioned that you are applying this to display the result of an address search in the Post API from the ZIP code informed by the user, in my view you will be using the attribute incorrectly in this situation. User address information is the user’s responsibility and he may want to change it, it makes no sense to deprive him of it. It is even quite common that the API result is incorrect. Street names change, Zip codes change... believe me, you don’t want your system trusting blindly on the API.
My advice is to remove the attributes readonly
of that field. This even has a positive impact on your user experience. Let them do what they’re expected to do.
Maybe in some very specific situation it is really necessary to do this validation in the field value readonly
, but for that do before setting the value. It makes no sense for you to assign an invalid value in the field and then validate it. Validate before, still in your application and only enter in the field if it is valid.
In fact
readonly
andrequired
in the same input is strange to say the least...– hugocsl
@hugocsl I am trying to do the following.. This is one of those fields that will only be inserted the
value
as soon as you enter the zip code (there will be the query in the mail API), correct? But this information is mandatory to have. What you advise?– user148754
But why would it be
readonly
? And if the information is wrong and the user needs to correct?– Woss
You have to validate the field that feeds the readonly and not the readonly field
– hugocsl
@Andersoncarloswoss not the only
input
which changes is that of the CEP. Thereadonly
it is only for reading. However the difference ofreadonly
and ofdisabled
is that thereadonly
is forwarded to the form, so thedisabled
nay.– user148754
@hugocsl could explain to me why this and why it is strange to have the required with him?
– user148754
Yes, I didn’t understand what this interferes with what I said. If you put, for example, the street name as
readonly
user will not be able to edit, even if this information came wrong - and if you are going to use the Post Office API, believe me, this is much more common than you think.– Woss
@THIAGODEBONIS I think you already have an answer, but it is for that reason as obvious as it seems.
– hugocsl