Serenity: Required vs ReadOnly attribute

Created on 19 Jun 2018  路  8Comments  路  Source: serenity-is/Serenity

If a field is marked both [Required] and [ReadOnly(true)] then it will not show asterisk on lable, some time it does not validate as well. It should show asterisk and validate the field even when readonly = true

question

Most helpful comment

If developer set a field as readonly, it's developers responsibility to set that field, not users.

All 8 comments

btw, I think if ReadOnly = true then this bellow code does not work too

this.form.SupplierQuoteNumber.addValidationRule(this.uniqueName, e => {
    if (Q.isEmptyOrNull(this.form.SupplierQuoteNumber.value)) {
        return "You have to enter value for SupplierQuoteNumber";
    }
});

No it works as designed, i dont think it should

If developer set a field as readonly, it's developers responsibility to set that field, not users.

Hi @volkanceylan, I think we should discuss about it
[ReadOnly] should do its work, [Required] should do its work as well. There is no reason to [Required] depends on [ReadOnly].
If I set a field as "[ReadOnly]" then it should be readonly
If I set a field as "[Required]" then it should be required
If I set both, it should be readonly and validate data for me

Maybe you think it's redundant when serenity must validate a field if it's readonly

Currently, if I need they work together, I have to set that field as [Required], and set [ReadOnly] at client side using EditorUtils
It works well for me, but anyway I don't think it's Declarative Programming

When a field is readonly it accepts no external input. This implies that the field is being set programmatically and in that logic is where you handle validation. I'm not sure what your approach is or why you would ever need to set required on a readonly field.

Perhaps, if you explain the use case in a little more detail I will understand.

When a field is readonly it accepts no external input

For my case, user can enter value for New case but it's readonly for Edit case.
We import data for that table, user can add/update record but very rare.
As I said, problem can be resolved by using EditorUtils to set readonly property, but I don't think [Required] depends on [ReadOnly].
How about if someone edit value by chrome developer mode and hit save.
Clearly, we should not trust anything that user entered.

A read-only field also need to validate ( [Required] ) even it's readonly

Required is a client side attribute, and as i said it works as designed.

You may add [NotNull] to field if you are worried about server side validation.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Akarsh03 picture Akarsh03  路  3Comments

kilroyFR picture kilroyFR  路  3Comments

gfo2007 picture gfo2007  路  3Comments

AmuthaKondusamy picture AmuthaKondusamy  路  3Comments

JohnRanger picture JohnRanger  路  3Comments