Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I store time as an integer of minutes in the database/model. However, I want to display it to the user in decimal hours (and let them edit the value using a number of buttons).

At the moment I have this:

<p class="input-group">
    <input type="text" readonly="readonly" value="{{vm.time.minutes|hoursMinutes}}" class="form-control" />
    <span class="input-group-btn">
        <ix-time-picker minutes="vm.time.minutes"></ix-time-picker>
    </span>
</p>

Of the 2 elements in the p tag:

  1. The input type="text" works fine as the display mechanism. The hoursMinutes filter returns a formatted value, e.g. for 90 mins it will return '1 hour 30 mins'.
  2. The ix-time-picker directive pops up a modal window with buttons such as 15 mins, 30 mins, 45 mins, 1 hour, 1:15 hours, etc.

This works fine - except for validation. I'm using ng-messages and I can't work out how to display the validation for the required state:

<li class="help-block has-error"
    ng-if="mainForm.$submitted"
    ng-messages="mainForm.minutes.$error">
    <span ng-message="required">
        Minutes is required.
    </span>
</li>

I don't have a control on the form called minutes (at least with an ng-model) so it won't display the message. I could add a hidden input with ng-model="vm.times.minutes" but as I need to use this repeatedly through the application, I'd prefer not to do that. Or at least I'd like to build some kind of common directive that merges the ability to have an ng-model on a control with the ability to display a value that is different to the model value, if that's possible.

Any suggestions?

share|improve this question

Aha - one option is to make the input a label:

<p class="input-group">
    <label id="minutes" name="minutes" readonly="readonly" class="form-control">{{vm.time.minutes|hoursMinutes}}</label>
    <span class="input-group-btn">
        <ix-time-picker minutes="vm.time.minutes"></ix-time-picker>
    </span>
</p>

Then it displays correctly and it validates.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.