Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ So we need to define these rules. In the controller add:
}
```

### Validating Password

Validating the equality of password and password confirmation can be done by using the rule below.

```Javascript
static rules = {
password_confirmation: { equality: { attribute: "password" } }
}```

Each key in rules corresponds to an attribute defined in the markup.

### HTML
Expand Down
13 changes: 12 additions & 1 deletion dist/validation-controller.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/validation-controller.js.map

Large diffs are not rendered by default.

13 changes: 12 additions & 1 deletion dist/validation-controller.m.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/validation-controller.m.js.map

Large diffs are not rendered by default.

13 changes: 12 additions & 1 deletion dist/validation-controller.umd.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/validation-controller.umd.js.map

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions spec/attributes_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ describe("Attributes", function() {
const name = this.form.querySelector("#name")
const email = this.form.querySelector("#email")
const terms = this.form.querySelector("#terms")
const password = this.form.querySelector("#password")
const password_confirmation = this.form.querySelector("#password_confirmation")

expect(this.validationElementsArray).to.have.members([name, email, terms])
expect(this.validationElementsArray).to.have.members([name, email, terms, password, password_confirmation])
})

it("only includes attr that identifier prefix", function() {
Expand All @@ -49,7 +51,7 @@ describe("Attributes", function() {
const mapKeys = Array.from(this.attributes.dataMap.keys())
const mapValues = Array.from(this.attributes.dataMap.values())

expect(mapKeys).to.have.members(["name", "email", "terms"])
expect(mapKeys).to.have.members(["name", "email", "terms", "password", "password_confirmation"])
expect(mapValues).to.all.be.an.instanceof(Attribute)
})
})
Expand Down
3 changes: 3 additions & 0 deletions spec/fixtures/form.html.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export default `<div data-controller="validations">
<input type="text" data-attr="address" id="address">
<input type="text" data-attr="my-validations.address" id="city">

<input type="password" data-attr="validations.password" id="password" data-action="blur->validations#validate">
<input type="password" data-attr="validations.password_confirmation" id="password_confirmation" data-action="blur->validations#validate">

<input type="submit" >
</form>
</div>`
15 changes: 14 additions & 1 deletion spec/validator_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ describe("Validator", function() {
validators: { agree: { attributes: ["terms"] } },
rules: {
name: { presence: { allowEmpty: false } },
email: { presence: { allowEmpty: false }, email: true }
email: { presence: { allowEmpty: false }, email: true },
password_confirmation: { equality: { attribute: "password" } }
}
}
}
Expand Down Expand Up @@ -132,6 +133,18 @@ describe("Validator", function() {

expect(this.validator.validatejsParams("name")).to.eql(params)
})

})

context("equality rule", function() {
it("returns validatajs params for password_confirmation", function() {
const params = [
{ password: "", password_confirmation: "" },
{ password_confirmation: { equality: { attribute: "password" } } }
]

expect(this.validator.validatejsParams("password_confirmation")).to.eql(params)
})
})
})
})
12 changes: 11 additions & 1 deletion src/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,17 @@ export class Validator {
}

const { value } = this.attributes.get(attribute)
return [{ [attribute]: value }, { [attribute]: this.rules[attribute] }]
const validateValue = {
[attribute]: value,
}

if (rule.equality) {
const anotherAttribute = rule.equality.attribute
const { value } = this.attributes.get(anotherAttribute)
validateValue[anotherAttribute] = value
}

return [validateValue, { [attribute]: this.rules[attribute] }]
}

get rules() {
Expand Down