Announcement Announcement Module
Collapse
No announcement yet.
disabled attribute in field:input Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • disabled attribute in field:input

    Hi,
    I want to generate a form with one of the input fields to be readonly.
    But readonly is not supported. If I use "disabled" attribute, I get the readonly effect but the value is not passed during submission.
    Here is how the field looks like:

    <field:input disabled="true" field="clusterId" id="myId" max="10" min="1" required="true" z="user-managed"/>

    If I use readonly/readOnly="true", I get "Attribute invalid error for tag input".

    I am looking for a work around the "disabled" attribute.
    Thanks.
    Last edited by chhava80; Feb 23rd, 2011, 08:30 PM.

  • #2
    Hi,

    but the value is not passed during submission
    Do you mean the existing value is lost when updating the entity?

    I found a similar behaviour when using the render="false" I think I'll open a JIRA.

    If I use readonly/readOnly="true", I get "Attribute invalid error for tag input".
    The readonly attribute doesn't exist, as you can see in the input.tagx file

    Franck

    Comment


    • #3
      I changed it to a display field and as long as the data is valid the form submitted fine in the Pet Clinic example script:

      Original from create.jspx:
      Code:
              <field:input field="name" id="c_com_springsource_petclinic_domain_Pet_name" min="1" required="true" z="ZY+k75JeSo9RmejYZRFNIvs2aBg="/>
      Copied the name field from show.jspx:
      Code:
                     <field:display field="name" id="s_com_springsource_petclinic_domain_Pet_name" object="${pet}" z="jifVeFdVtAczqHwRlAW8gynS40U="/>
      To test it I put a default value on the name in Pet.java :
      Code:
          @NotNull
          @Size(min = 1)
          private String name="Test";
      Started it up and created a new pet, name field was readonly and populated with Test, added the rest and submitted and it saved correctly.

      Make the same changed in the update.jspx and it should work.

      Comment


      • #4
        Don't you think Roo might regenerate the field:input tag sometimes?

        Comment


        • #5
          Originally posted by fraveau View Post
          Don't you think Roo might regenerate the field:input tag sometimes?

          Sorry the direct cut and paste from show doesn't work because the ID for the field is different.

          In the Show File Roo is looking for:
          Code:
          s_com_springsource_petclinic_domain_Pet_name
          while the create/update it's looking for:
          Code:
          c_com_springsource_petclinic_domain_Pet_name
          so if you change the "s" to a "c" in the id of the field, roo will start picking it up as a user-managed field (z= will turned to user-managed instead of the Roo internal identifier)

          Comment


          • #6
            Originally posted by btlife View Post
            I changed it to a display field and as long as the data is valid the form submitted fine in the Pet Clinic example script:

            Original from create.jspx:
            Code:
                    <field:input field="name" id="c_com_springsource_petclinic_domain_Pet_name" min="1" required="true" z="ZY+k75JeSo9RmejYZRFNIvs2aBg="/>
            Copied the name field from show.jspx:
            Code:
                           <field:display field="name" id="s_com_springsource_petclinic_domain_Pet_name" object="${pet}" z="jifVeFdVtAczqHwRlAW8gynS40U="/>
            To test it I put a default value on the name in Pet.java :
            Code:
                @NotNull
                @Size(min = 1)
                private String name="Test";
            Started it up and created a new pet, name field was readonly and populated with Test, added the rest and submitted and it saved correctly.

            Make the same changed in the update.jspx and it should work.
            This worked for display but the update did not apply. It still fails to update the record.

            Comment


            • #7
              It seems to be working for me, I am using the petclinic sample, and I added a Pet, then updated it, and the name field was read-only in both cases, and the create and update both worked.

              update.jspx
              Code:
              <?xml version="1.0" encoding="UTF-8" standalone="no"?>
              <div xmlns:field="urn:jsptagdir:/WEB-INF/tags/form/fields" xmlns:form="urn:jsptagdir:/WEB-INF/tags/form" xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0">
                  <jsp:output omit-xml-declaration="yes"/>
                  <form:update id="fu_com_springsource_petclinic_domain_Pet" modelAttribute="pet" path="/pets" z="Mf6hgl56upE3YLbAERI4AsYVPk8=">
                      <field:checkbox field="sendReminders" id="c_com_springsource_petclinic_domain_Pet_sendReminders" z="uPpMX+IWb0KONpvd11fpG8x4/4Q="/>
                      <field:display field="name" id="c_com_springsource_petclinic_domain_Pet_name" object="${pet}" z="user-managed"/>
                      
                      <field:input field="weight" id="c_com_springsource_petclinic_domain_Pet_weight" min="0" required="true" validationMessageCode="field_invalid_number" z="cOD5zE/z7gy+RZu5kVSPuxCa+/I="/>
                      <field:select field="owner" id="c_com_springsource_petclinic_domain_Pet_owner" itemValue="id" items="${owners}" path="/owners" z="fGzswAP4XXvhPhowJKsRVve929c="/>
                      <field:select field="type" id="c_com_springsource_petclinic_domain_Pet_type" items="${pettypes}" path="pettypes" required="true" z="+hDCnUp+Y+A1RlT+AjH07sgipOo="/>
                  
                  </form:update>
              </div>
              create.jspx
              Code:
              <?xml version="1.0" encoding="UTF-8" standalone="no"?>
              <div xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:field="urn:jsptagdir:/WEB-INF/tags/form/fields" xmlns:form="urn:jsptagdir:/WEB-INF/tags/form" xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:spring="http://www.springframework.org/tags" version="2.0">
                  <jsp:output omit-xml-declaration="yes"/>
                  <form:create id="fc_com_springsource_petclinic_domain_Pet" modelAttribute="pet" path="/pets" render="${empty dependencies}" z="lgvEyAlAYOudDmaPjwU0ABseTIk=">
                      <field:checkbox field="sendReminders" id="c_com_springsource_petclinic_domain_Pet_sendReminders" z="uPpMX+IWb0KONpvd11fpG8x4/4Q="/>
                      <field:display field="name" id="c_com_springsource_petclinic_domain_Pet_name" object="${pet}" z="user-managed"/>
                      <field:input field="weight" id="c_com_springsource_petclinic_domain_Pet_weight" min="0" required="true" validationMessageCode="field_invalid_number" z="cOD5zE/z7gy+RZu5kVSPuxCa+/I="/>
                      <field:select field="owner" id="c_com_springsource_petclinic_domain_Pet_owner" itemValue="id" items="${owners}" path="/owners" z="fGzswAP4XXvhPhowJKsRVve929c="/>
                      <field:select field="type" id="c_com_springsource_petclinic_domain_Pet_type" items="${pettypes}" path="pettypes" required="true" z="+hDCnUp+Y+A1RlT+AjH07sgipOo="/>
              		
                  </form:create>
                  <form:dependency dependencies="${dependencies}" id="d_com_springsource_petclinic_domain_Pet" render="${not empty dependencies}" z="kThDNIW+69h9nI/69ynY1WyUieo="/>
              </div>

              Comment


              • #8
                btlife: With the solution you posted earlier, I will not be able to generate any 'pets' with any name other than 'test'. Am I correct here? If so, is there a way to get around this issue?

                Comment


                • #9
                  Originally posted by chhava80 View Post
                  btlife: With the solution you posted earlier, I will not be able to generate any 'pets' with any name other than 'test'. Am I correct here? If so, is there a way to get around this issue?
                  That's correct, the issue was how to get a read-only field to work, so I hardcoded a default value to the read-only field I was testing.

                  I would assume the read-only field is populated either internally or through some other form that has the field editable. For example, admin_create.jspx, has the field editable while create.jspx has it read-only.

                  Or have an AdminController which has a create where the field is editable, and the PetController has the field read-only.

                  Or PetController, loads the correct form based on permissions, or even create a new jspx tag file that can switch back and forth, based on permissions or a model attribute, passed from the controller to the create/update.jspx

                  Comment


                  • #10
                    Something like this could be in place for a modelattribute readOnly (boolean)passed in from the controller:

                    Code:
                           <c:choose>
                            <c:when test="${readOnly}">
                              <field:display field="name" id="c_com_springsource_petclinic_domain_Pet_name" object="${pet}" z="user-managed"/>
                            </c:when>
                            <c:otherwise>
                              <field:input field="name" id="c_com_springsource_petclinic_domain_Pet_name" min="1" required="true" z="ZY+k75JeSo9RmejYZRFNIvs2aBg="/>
                            </c:otherwise>
                          </c:choose>
                    Or a change to the input.tagx:
                    Code:
                    <jsp:root xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:fn="http://java.sun.com/jsp/jstl/functions" xmlns:spring="http://www.springframework.org/tags" xmlns:form="http://www.springframework.org/tags/form" xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0">
                      <jsp:output omit-xml-declaration="yes" />
                    
                      <jsp:directive.attribute name="id" type="java.lang.String" required="true" description="The identifier for this tag (do not change!)" />
                      <jsp:directive.attribute name="field" type="java.lang.String" required="true" description="The field exposed from the form backing object" />
                      <jsp:directive.attribute name="label" type="java.lang.String" required="false" description="The label used for this field, will default to a message bundle if not supplied" />
                      <jsp:directive.attribute name="required" type="java.lang.Boolean" required="false" description="Indicates if this field is required (default false)" />
                      <jsp:directive.attribute name="disabled" type="java.lang.Boolean" required="false" description="Specify if this field should be enabled" />
                      <jsp:directive.attribute name="validationRegex" type="java.lang.String" required="false" description="Specify regular expression to be used for the validation of the input contents" />
                      <jsp:directive.attribute name="validationMessageCode" type="java.lang.String" required="false" description="Specify the message (message property code) to be displayed if the regular expression validation fails" />
                      <jsp:directive.attribute name="validationMessage" type="java.lang.String" required="false" description="Specify the message to be displayed if the regular expression validation fails" />
                      <jsp:directive.attribute name="min" type="java.lang.String" required="false" description="Specify the minimum length of the input contents" />
                      <jsp:directive.attribute name="max" type="java.lang.String" required="false" description="Specify the maximum length of the input contents" />
                      <jsp:directive.attribute name="decimalMin" type="java.lang.String" required="false" description="Specify the minimum size of the input contents" />
                      <jsp:directive.attribute name="decimalMax" type="java.lang.String" required="false" description="Specify the maximum size of the input contents" />
                      <jsp:directive.attribute name="disableFormBinding" type="java.lang.Boolean" required="false" description="Set to true to disable Spring form binding" />
                      <jsp:directive.attribute name="type" type="java.lang.String" required="false" description="Set field type (default 'text', or 'password')" />
                      <jsp:directive.attribute name="render" type="java.lang.Boolean" required="false" description="Indicate if the contents of this tag and all enclosed tags should be rendered (default 'true')" />
                      <jsp:directive.attribute name="readOnly" type="java.lang.Boolean" required="false" description="Indicate if the contents of this tag is display only (default 'false')" />
                      <jsp:directive.attribute name="z" type="java.lang.String" required="false" description="Used for checking if element has been modified (to recalculate simply provide empty string value)" />
                    
                      <c:if test="${empty render or render}">
                      
                        <c:if test="${empty type}">
                          <c:set value="text" var="type" />
                        </c:if>
                    
                        <c:if test="${empty disabled}">
                          <c:set value="false" var="disabled" />
                        </c:if>
                    
                        <c:if test="${empty readOnly}">
                          <c:set value="false" var="readOnly" />
                        </c:if>
                        <c:if test="${empty label}">
                          <spring:message code="label_${fn:toLowerCase(fn:substringAfter(id,'_'))}" var="label" />
                        </c:if>
                    
                        <c:if test="${empty required}">
                          <c:set value="false" var="required" />
                        </c:if>
                        
                        <c:set var="sec_field">
                          <spring:escapeBody javaScriptEscape="true" >${field}</spring:escapeBody>
                        </c:set>
                    
                        <div id="_${fn:escapeXml(id)}_id">
                          <label for="_${sec_field}_id">
                            <c:out value="${fn:escapeXml(label)}" />
                            :
                          </label>
                    	<c:choose>
                    		<c:when test="${readOnly}">
                    			<spring:eval expression="object[field]" />
                    		</c:when>
                    	<c:otherwise>
                          <c:choose>
                            <c:when test="${disableFormBinding}">
                              <input id="_${sec_field}_id" name="${sec_field}" type="${fn:escapeXml(type)}" />
                            </c:when>
                            <c:otherwise>
                              <c:choose>
                                <c:when test="${type eq 'password'}">
                                  <form:password id="_${sec_field}_id" path="${sec_field}" disabled="${disabled}" />
                                </c:when>
                                <c:otherwise>
                                  <form:input id="_${sec_field}_id" path="${sec_field}" disabled="${disabled}" />
                                </c:otherwise>
                              </c:choose>
                              <br />
                              <form:errors cssClass="errors" id="_${sec_field}_error_id" path="${sec_field}" />
                            </c:otherwise>
                          </c:choose>
                          <spring:message arguments="${label}" code="field_invalid" var="field_invalid" />
                          <c:choose>
                            <c:when test="${required}">
                              <spring:message code="field_required" var="field_required" />
                              <spring:message argumentSeparator="," arguments="${label},(${field_required})" code="field_simple_validation" var="field_validation" />
                            </c:when>
                            <c:otherwise>
                              <spring:message argumentSeparator="," arguments="${label}, " code="field_simple_validation" var="field_validation" />
                            </c:otherwise>
                          </c:choose>
                          <script type="text/javascript">
                            <c:set var="sec_field_validation">
                              <spring:escapeBody javaScriptEscape="true">${field_validation}</spring:escapeBody>
                            </c:set>
                            <c:set var="sec_field_invalid">
                              <spring:escapeBody javaScriptEscape="true">${field_invalid}</spring:escapeBody>
                            </c:set>
                            Spring.addDecoration(new Spring.ElementDecoration({elementId : '_${sec_field}_id', widgetType : 'dijit.form.ValidationTextBox', widgetAttrs : {promptMessage: '${sec_field_validation}', invalidMessage: '${sec_field_invalid}', required: ${required}}})); 
                          </script>
                    	</c:otherwise>
                    </c:choose>
                    	  </div>
                        <br />
                      </c:if>
                    </jsp:root>

                    Bolded lines are the ones I changed, basically added a new attribute to the tagfile called readOnly, it defaults to false.

                    Next if the readOnly is set to true, it just displays the field (cut and pasted the logic from display.tagx), otherwise it does whatever it did before.

                    I wrapped the Validation logic in the input.tagx in the otherwise call so the form wouldn't try and validate the display field.

                    I haven't tested this code, but barring cut & paste errors it should work, or at least give you an idea of what I am talking about.

                    Comment


                    • #11
                      tried it with <field:textarea .. tag

                      when readOnly="true", throws an exception when loading the page

                      SEVERE: Servlet.service() for servlet jsp threw exception
                      org.springframework.expression.spel.SpelEvaluation Exception: EL1007Epos 0): Field or property 'object' cannot be found on null

                      edited textarea.jspx with the bold text

                      Am I missing something ??
                      Last edited by manojsamaraweera; May 26th, 2011, 11:16 PM.

                      Comment

                      Working...
                      X