MRCollective / ChameleonForms

Shape-shifting your forms experience in ASP.NET Core MVC
MIT License
254 stars 56 forks source link

How to add "has-error" in an expanded bootstrap form group. #125

Closed jtheisen closed 9 years ago

jtheisen commented 9 years ago

Sometimes it's easier to expand a @s.FieldFor(m => m.Name) in its components to customize the html more directly:

        <div class="form-group">
            @f.LabelFor(m => m.Name)
            @f.FieldElementFor(m => m.Name)
            @f.ValidationMessageFor(m => m.Name)
        </div>

This still leaves the missing has-error class to be added in the case of a validation error.

Is there a convenient way to do this? I can't find something like a f.BeginEnvelopeFor(m => m.Name) that only outputs the surrounding <div class="form-group"> to allow that.

robdmoore commented 9 years ago

There should be no difference between s.FieldFor and reaching into the various f.*For methods,

Can you explain what difference you are seeing more clearly so we can try and figure out what's happening?

Thanks

jtheisen commented 9 years ago

Just to be clear, I'm not reporting a bug - couldn't figure out how to mark this as a question.

The difference is that I put out the <div class="form-group"> explicitly, whereas with an s.FieldFor(m => m.Name) it's generated.

Since it's no longer generated, I don't get the perk of having the class has-error added when needed.

I'm looking for a (pretty) way of changing the above expansion of s.FieldFor(m => m.Name) in such a way that I have the class on the outer div in the right cases.

Does ChameleonForms (or rather the bootstrap extension) already have a standard way of "expanding" s.FieldFor(m => m.Name) so that the class is added?

robdmoore commented 9 years ago

Ahh right. This javaScript does it: https://github.com/MRCollective/ChameleonForms/blob/master/ChameleonForms.Example/Scripts/jquery.validate.unobtrusive.twitterbootstrap.js

The chameleon forms Twitter bootstrap nuget package should drop that one into your scripts folder (or you can manually pop it in) and then you just need to reference it from your page.

It shouldn't matter if you manually added the doc or not since it's just looking for the form-group class.

jtheisen commented 9 years ago

Ok, I didn't use the NuGet package as I wanted to use a newer version of ChameleonForms (can't remember the feature I needed) and I'm not using js validation at all so far. I'll probably write a little helper that adds the class when needed.

But thanks for the fast responses.

robdmoore commented 9 years ago

No worries