Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add number validations #440

Closed
jasoncarreira opened this issue May 17, 2017 · 8 comments
Closed

Add number validations #440

jasoncarreira opened this issue May 17, 2017 · 8 comments

Comments

@jasoncarreira
Copy link

I'm submitting a feature request

Current behavior:

There are no number validations

Expected/desired behavior:

  • What is the expected behavior?

I'd like to have number validations such as lessThan, greaterThan, between, etc.

  • What is the motivation / use case for changing the behavior?

I have a lot of numeric properties and having to use the satisfies() method takes up a lot more space than built-in number validations

@shawty
Copy link

shawty commented May 17, 2017

+1 gets my vote too, this is such a common pattern on select based drop downs.

@jdanyow
Copy link
Contributor

jdanyow commented May 20, 2017

Would you mind sharing your custom validations for numbers? Will help us to make sure we're hitting your use cases.

@shawty
Copy link

shawty commented May 20, 2017

@jdanyow me or the op?

@jdanyow
Copy link
Contributor

jdanyow commented May 20, 2017

Both! 😀

@shawty
Copy link

shawty commented May 21, 2017

LOLZ... :-)

Well for me, I usually create my drop downs something like the following:

<select value.bind="theSelectValue">
  <option value="0">Please Select something</option>
  <option repeat.for="option of selectOptions" value.bind="option.value">${option.text}</option>
</select>

My "selectOptions" are then populated thus:

private selectOptions = [
  { value: 1, text: "Option 1" }
  { value: 2, text: "Option 2" }
  { value: 3, text: "Option 3" }
  { value: 4, text: "Option 4" }
];

Or by loading from Ajax in that format.

I then handle my rules in the VM thus:

  ValidationRules.customRule(
      'ddlValueSelected',
      (value: number, sender: ProjectDetails) => {
        if (value > 0) {
          return true;
        }

        return false;
      },
      `Drop down value MUST be selected`
    );

    ValidationRules
      .ensure((p: myVmType) => p.theSelectValue).satisfiesRule('ddlValueSelected').withMessage("Option must be selected.")
      .on(this);

Or more often, I actually create a

  private myData = {}

Then bind my controls to

  myData.foo....

and put the validation on

  .on(this.foo)

If that helps :-)

Shawty

@jasoncarreira
Copy link
Author

jasoncarreira commented May 22, 2017

Here's mine, so far:

ValidationRules.customRule(
    'numberRange',
    (value, obj, min, max, dollar : boolean = true) => {
      let number = numeral(value).value();
      return value === null || value === undefined
        || number >= min && number <= max
    },
    `\${$displayName} must be between \${'$config.min} and \${$config.max}.`,
    (min, max) => ({ min, max })
  );

  ValidationRules.customRule(
    'lessThan',
    (value, obj, max) => {
      let number = numeral(value).value();
      return value === null || value === undefined
        || number <= max
    },
    `\${$displayName} must be less than \${$config.max}.`,
    (max) => ({ max })
  );

  ValidationRules.customRule(
    'greaterThan',
    (value, obj, min) => {
      let number = numeral(value).value();
      return value === null || value === undefined
        || number >= min
    },
    `\${$displayName} must be greater than \${$config.min}`,
    (min) => ({ min })
  );

@jdanyow
Copy link
Contributor

jdanyow commented May 30, 2017

thanks!

@JoyalToTheWorld
Copy link

Any progress on this issue?

CuddleBunny pushed a commit to CuddleBunny/validation that referenced this issue Feb 20, 2019
Add common validation rules for numeric properties.

resolves aurelia#440
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants