locked
text area validation and class issues RRS feed

  • Question

  • User379720387 posted

    I have two issues, related a text area. One issue is that the validation does not work.

    The first code block is for a regular input box, validation works.

    The second code block is for a text area, the validation does not work.

    The second issue is that I have complaints from VS:

    <text>class...  / element text is not supported in HTML5

    Validation.ClassFor ("formDescr.... / the attribut name must be following by an equal sign. If the value is in quotation marks, the quotation marks must match

    No clue.


    Validation.RequireField("formDescription", "Description is required.");
    Validation.RequireField("formDescription1", "Description is required.");


    <tr><td>Description of your trip:@if (!ModelState.IsValidField("formDescription")) {<text>class="error-label"</text>}</td> <td><input type="text" name="formDescription" id="formDescription" size="100" maxlength="100" class="@Validation.ClassFor("formDescription")" @Validation.For("formDescription") /> @Html.ValidationMessage("formDescription")</td> </tr> <tr><td>Description of your trip:@if (!ModelState.IsValidField("formDescription1")) {<text>class="error-label"</text>}</td> <td><textarea rows="4" cols="25" name="formDescription1" id="formDescription1" class="@Validation.ClassFor("formDescription1") @Validation.For("formDescription1")" > </textarea> @Html.ValidationMessage("formDescription1")</td> </tr>



    Tuesday, August 13, 2013 12:27 PM

Answers

All replies

  • User-1496281956 posted

    I have two issues, related a text area. One issue is that the validation does not work.
    The first code block is for a regular input box, validation works.
    The second code block is for a text area, the validation does not work.
    
    The second issue is that I have complaints from VS:
    <text>class...  / element text is not supported in HTML5
    Validation.ClassFor ("formDescr.... / the attribut name must be following by an equal sign. If the value is in quotation marks, the quotation marks must match
    
    No clue.
    
    
    Validation.RequireField("formDescription", "Description is required.");
    Validation.RequireField("formDescription1", "Description is required.");
    
    
    <tr><td>Description of your trip:@if (!ModelState.IsValidField("formDescription"))
                                         {<text>class="error-label"</text>}</td>
            <td><input type="text" name="formDescription" id="formDescription" size="100" maxlength="100"
            class="@Validation.ClassFor("formDescription")" @Validation.For("formDescription")  /> @Html.ValidationMessage("formDescription")</td>
        </tr>
            
    <tr><td>Description of your trip:@if (!ModelState.IsValidField("formDescription1")) {<text>class="error-label"</text>}</td>
            <td><textarea rows="4" cols="25" name="formDescription1" id="formDescription1"
            class="@Validation.ClassFor("formDescription1") @Validation.For("formDescription1")"  > </textarea>  @Html.ValidationMessage("formDescription1")</td>
        </tr>

    Just do this:

    Validation.RequireFields("fieldone", "fieldtwo");
    

    When you want to validate do this:

    if (Validation.IsValid())
    {
     code to execute..
    }

    When you output the error message it's like this:

    <textarea></textarea>
    @Html.ValidationMessage("fieldone")

    The <text> message you can ignore it's part of razor. 



    Tuesday, August 13, 2013 12:48 PM
  • User-1496281956 posted

    Ah sorry I misunderstood, 

    you're missing quotation mark:

    class="@Validation.ClassFor("formDescription1")"
    Tuesday, August 13, 2013 12:53 PM
  • User379720387 posted

    Same error.

    Ah sorry I misunderstood, 
    
    you're missing quotation mark:
    class="@Validation.ClassFor("formDescription1")"

    Tuesday, August 13, 2013 1:17 PM
  • User-1496281956 posted

    I can't spot anything else, will have to wait for someone else, make sure you cleared cache..

    Tuesday, August 13, 2013 1:19 PM
  • User-821857111 posted

    CriticalError is almost right - you aren't missing a quote, you have placed it after @Validation.For("formDescription1") in the textarea markup instead of just after the class declaration.

    Not sure what you are trying to achieve with the <text>class="error-label£</text> stuff - that will render class="error-label" directly to the browser. Should it be placed before the closing angle bracket in the <td> tag?

    Tuesday, August 13, 2013 2:32 PM
  • User379720387 posted

    I don't know. Pieces of code cobbled together.

    Is this what you meant?

    <tr>
            <td>Description of your trip:@if (!ModelState.IsValidField("formDescription1")) {<text class="error-label"></text>}</td>
            <td><textarea rows="4" cols="25" name="formDescription1" id="formDescription1"
            class="@Validation.ClassFor("formDescription1")" @Validation.For("formDescription1") /></textarea>

    @Html.ValidationMessage("formDescription1")</td>
     </tr>

    Tuesday, August 13, 2013 2:44 PM
  • User-821857111 posted

    Yes, but as I said, I suspect it should be like this:

    <td @if (!ModelState.IsValidField("formDescription1")) {<text class="error-label"></text>}>Description of your trip:</td>
            <td><textarea rows="4" cols="25" name="formDescription1" id="formDescription1" 
            class="@Validation.ClassFor("formDescription1")" @Validation.For("formDescription1") /></textarea>

    @Html.ValidationMessage("formDescription1")</td>

    Personally, I strongly recommend the use of the Html form helpers. You get all the Validation.For and Validation.ClassFor stuff built in:

    @Html.TextArea("formDescription1", Request["formDescription1"], new {rows = 4, cols = 25})

    Tuesday, August 13, 2013 3:13 PM
  • User379720387 posted

    I so want to get rid of what I have, but I have too much code invested in what I have presented here.

    Have you seen a fully worked out tutorial showing the entire workflow utilizing the helper.

    This is what I have now. Complains about not having a closing tag and not liking that the text tag has atrributes.

    <tr>
        <td @if (!ModelState.IsValidField("formDescription1")) {<text class="error-label"></text>}> Description of your trip:</td>
        <td><textarea rows="4" cols="25" name="formDescription1" id="formDescription1" class="@Validation.ClassFor("formDescription1")"/></textarea>
            @Html.ValidationMessage("formDescription1")</td>
        </tr>


    Tuesday, August 13, 2013 4:01 PM
  • User-821857111 posted

    I've written a tutorial about validation: http://www.mikesdotnetting.com/Article/191/Validation-In-Razor-Web-Pages-2

    class="@Validation.ClassFor("formDescription1")"/></textarea>

    Get rid of the / at the end of the opening <teaxtarea> tag.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 13, 2013 4:17 PM