I have a form. In order to validate the fields (on the client side) I'm using a bit of HTML5 and ng-message
to display the error.
Two of the fields behave differently from the rest of the fields.
If one of these 2 fields is filled, the other should be filled as well.
On the other side, if none of the two is filled, the form should accept these 2 fields as valid.
I want a direct feedback in the form, as soon as the user enter something in one of the two field, the other should display the corresponding message.
The 2 fields in the code bellow are fWebsiteURL
and fWebsiteName
.
What is the best approach to implement such logic in my directive/form ?
Here is portion of my partial (using jade and bootstrap 2.x.x).
At the moment, only a basic required
validation is made for each field, making them mandatory for the all form.
Thanks for your help.
form.form-horizontal(name="scheduleForm")
.control-group(ng-class="{error: scheduleForm.fProjectName.$invalid}")
label.control-label.muted(for='fProjectName') Project
.controls
input.input-xxlarge(name="fProjectName", type='text', placeholder='Project name', ng-model="schedule.projectName", required)
small.help-inline(ng-messages="scheduleForm.fProjectName.$error")
div(ng-message="required")
| The project name is required.
.control-group(ng-class="{error: scheduleForm.fWebsiteName.$invalid}")
label.control-label.muted(for='fWebsiteName') Website name
.controls
input.input-xlarge(name="fWebsiteName", type='text', placeholder='Website name', ng-model="schedule.website.name", required)
small.help-inline(ng-messages="scheduleForm.fWebsiteName.$error")
div(ng-message="required")
| The website name is required if you entered a website link.
.control-group(ng-class="{error: scheduleForm.fWebsiteURL.$invalid}")
label.control-label.muted(for='fWebsiteURL') Website URL
.controls
input.input-xlarge(name="fWebsiteURL", type='text', placeholder='Website link', ng-model="schedule.website.url", required)
small.help-inline(ng-messages="scheduleForm.fWebsiteURL.$error")
div(ng-message="required")
| The website link is required if you entered a website name.
.control-group
.controls
button.btn.btn-primary(type='button', name='submit', ng-disabled="scheduleForm.$invalid", ng-click="update()") Save
button.btn(type='button', name='Cancel', ng-click="cancel()") Cancel