setReadOnly problem with fields bound in FieldGroup

Hi,
in a fieldGroup there are two TextField, bound to a beanItem. One of them is set as readOnly, but it is still editable.
I read in the forum about problems on readonly propagation and move setReadOnly after binding, but my idField is still editable. Is it a bug?

[code]
public MyModelFormLayout(MyModel myModel) {
BeanItem beanItem beanItem=new BeanItem(myModel);
FieldGroup fieldGroup = new FieldGroup(beanItem);

TextField idField=new TextField("id");
idField.setReadOnly(true);

TextField nomeMyModelField=new TextField("nome myModel");

addComponent(idField);
addComponent(nomeMyModelField);

fieldGroup.bind(idField,"id" );
fieldGroup.bind(nomeMyModelField,"nomeMyModel" );
idField.setReadOnly(true); //repeat setReadOnly after binding

}
[/code]Thank you, Francesco

Any suggestion?
Thank you, Francesco

When binding, your FieldGroup instance will override the readOnly property of your field with the corresponding value from the property datasource or with the readOnly property of the FieldGroup instance itself, i.e., idField.setReadOnly(true) before fieldGroup.bind("idField", "id") is overriden. This is why you are repeating your setReadOnly after binding.

I’d suggest that:

  • you either leave your fieldGroup declaration with the beanItem as such but use Field<?> idField = fieldGroup.buildAndBind("idField", "id"), followed by idField.setReadOnly(true), or
  • you declare fieldGroup as FieldGroup fieldGroup = new FieldGroup()', [b] impose your readOnly condition on the beanItem directly [/b], i.e., beanItem.getItemProperty(“id”).setReadOnly(true), build and bind your idField, [b] and eventually you do fieldGroup.setItemDataSource(beanItem)`
    [/b].

HTH

Okihara,
beanItem.getItemProperty(“id”).setReadOnly(true) solved my problem!
Thank you a lot, Francesco

Could you please post your complete solution?

Orel, here you are my solution. The trick is to setReadOnly the beanItem propery non only when yoy create the fieldGroup, but everytime you rebind yout bean to the form. Check the readOnlyFields() in my code.
Ciao, Francesco

public interface mioForm {
    /**
     * esegue la commit del fieldGroup
     * @throws CommitException
     */
    public void commit() throws CommitException;

    
    /**
     * isModified del fieldGroup
     * @return
     */
    public boolean isModified();
    /**
     * isValid del fieldGroup
     * @return
     */
    public boolean isValid();
    
    public void rebind(GenericModel bean);
    
    /**
     * Costruisce il beanItem in base al model<br>
     * Setta i campi readOnly<br>
     * Costruisce il FieldGroup <br>
     * Costruisce i Field con le loro proprietà<br>
     * Posiziona i Filed sul Layout<br>
     * effettua il binding fra i field e il beanItem<br>
     * 
     * @param genericModel
     */
    public void creaFieldGroup(GenericModel genericModel);
    
    /**
     * setta la proprietà readOnly sul beanItem prima di assegnarlo al fieldGroup
     */
    public void settaCampiReadOnly();
    
    public void setBeanItem(BeanItem<?> beanItem) ;
    
    public BeanItem<?> getBeanItem();
    
    public FieldGroup getFieldGroup();

    public void setFieldGroup(FieldGroup fieldGroup);
    
    /**
     * restituisce il componentContainer che costituisce il layout della form
     * @return
     */
    public ComponentContainer getLayout();
    
    /**
     * setta componentContainer come layout della form
     * @return
     */
    public void setLayout(ComponentContainer componentContainer);

}
public abstract class MioFormAbstract  implements mioForm {

    private FieldGroup fieldGroup;
    
    public MioFormAbstract(BeanItem  beanItem) {
        super();
        if (beanItem!=null)
            creaFieldGroup(beanItem);
    }
    

    @Override
    public void commit() throws CommitException{
        getFieldGroup().commit();
    }
    
    @Override
    public boolean isModified(){
        return getFieldGroup().isModified();
    }


    @Override
    public boolean isValid(){
        return getFieldGroup().isValid();
    }

    @Override
    public void rebind(GenericModel bean) {
        if(bean == null) {
            throw new IllegalArgumentException("Bean can't be null");
        }
        if (getFieldGroup()==null)
            creaFieldGroup(bean);
        else{
            setBeanItem(new BeanItem<GenericModel>(bean));
            readOnlyFields();
            getFieldGroup().setItemDataSource(getBeanItem());    
        }
        
    }
    
    @Override
    public FieldGroup getFieldGroup() {
        return fieldGroup;
    }
    
    @Override
    public void setFieldGroup(FieldGroup fieldGroup) {
        this.fieldGroup = fieldGroup;
    }


}
public class AutorizzazioneFormLayout extends MioFormAbstract {

    
    private BeanItem<Autorizzazione> beanItem;
    private FormLayout formLayout;
    
    public AutorizzazioneFormLayout(BeanItem  beanItem) {
        super(beanItem);
        FormLayout formLayout=new FormLayout();
        setLayout(formLayout);
        
        if (beanItem!=null)
            creaFieldGroup(beanItem);
    }
    
    @Override
    public void readOnlyFields(){
        beanItem.getItemProperty("id").setReadOnly(true);
    }
    
    @Override
    public void creaFieldGroup(BeanItem  beanItem){

        
        readOnlyFields();
        
        //costruisco il FieldGroup
        setFieldGroup(new FieldGroup(beanItem));
        
        //costruisco i Field con le loro proprietà
        TextField idField=new TextField("id");
        idField.setWidth("60%");
        idField.setNullRepresentation("");
        idField.setReadOnly(true);
        
        ComboBox ruoloSecurityField=getRuoloSecurityComboBox();
        
        TextArea ta=new TextArea("condizione");
        ta.setWidth("100%");
        ta.setNullRepresentation("");
        ta.setRows(3);
        
        CheckBox abilitato=new CheckBox(app.getMessage("fun.autorizzato"));
    
        //posiziono i Fileds sul layout
        getLayout().addComponent(idField);
        getLayout().addComponent(ruoloSecurityField);
        getLayout().addComponent(ta);
        getLayout().addComponent(abilitato);
        
        
        //effettuo il binding fra i field e il beanItem
        getFieldGroup().bind(ruoloSecurityField,"ruoloSecurity" );
        getFieldGroup().bind(ta,"condizione" );
        getFieldGroup().bind(abilitato,"abilitato" );
        getFieldGroup().bind(idField,"id" );
    }
    
    @Override
    public ComponentContainer getLayout() {
        return formLayout;
    }

    @Override
    public void setLayout(ComponentContainer componentContainer) {
        formLayout=(FormLayout) componentContainer;
    }

}