1

I haven't found any fields that deal with numbers for a plugin. I've changed a forms.textField into a type:number which has seemed to work fine for the most part. But I can't find a way to keep someone from going under 0 or over 100. I've looked in vendor\craftcms\cms\src\templates\_includes and have come up empty.

I'm assuming I'll need to make a custom field type for this but if there is something I'd rather not reinvent the wheel.

  {{ forms.textField({
      label: 'Blanket Discount Percent',
      instructions: 'Set this if you want to offer a blanket discount to all businesses. Do not put in letters or decimals. (10 = 10%)',
      id: 'blanketDiscount',
      name: 'settings[blanketDiscount]',
      value: settings.blanketDiscount,
      type: 'number',
   })}}

I do have a rule for validation setup, I would just prefer to keep people in range at the front as well.

['blanketDiscount', 'integer', 'min' => 0, 'max' => 100],
  • Not an answer to your question, but the reason there isn't a number macro by default in Craft is that it's very US-centric formatting (commas for thousands separator, period for decimal) and that's not true everywhere. – Brad Bell Sep 21 '18 at 19:07
1

Not tested yet.

{{ forms.textField({
        label: 'Number Field',
        id: 'someId',
        name: 'someName',
        value: '',
        min: 0,
        max: 100,
        size: 1,
        type: 'number',
        value: settings['someName']
    })
    }}

I think you can use min and max

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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