Skip to main content
4 votes
Accepted

Craft Commerce: Custom address model validations

You can do either or both of: Use javascript validation on the front end (with e.g. parsley) Create a plugin with a method that is registered as a listener on this event: https://craftcommerce.com/...
Jeremy Daalder's user avatar
4 votes
Accepted

Validation on username?

I don't know if there is anything natively in Craft to do this, but you could achieve it quite easily with a single file plugin: public function init() { craft()->on('users.onBeforeSaveUser', ...
Alec Ritson's user avatar
  • 4,539
4 votes
Accepted

Title field character length

You can create a custom validation at the EVENT_BEFORE_SAVE event Event::on(Entry::class, Entry::EVENT_BEFORE_SAVE, function(ModelEvent $event){ /** @var Entry $entry */ $...
Robin Schambach's user avatar
4 votes

How to validate a unique field in Craft 3 element?

Here's my findings after some experimentation: Elements and Records are two entirely separate entities. The Element is basically the core Craft database entry, while the Record is the additional ...
Lazlo's user avatar
  • 185
3 votes

Craft Commerce — Billing / Shipping Address validation / required Fields

Address validation is, in Commerce v1 at least, generally handled via a plugin. (I'll assume you already have a business logic plugin on the go, but if not head over to https://pluginfactory.io and ...
Jeremy Daalder's user avatar
3 votes

Craft Commerce — Billing / Shipping Address validation / required Fields

It will be possible in the next release (commerce 2-beta.5): https://github.com/craftcms/commerce/issues/342
Luke Holder's user avatar
  • 6,837
3 votes
Accepted

Front-end registration validation

You could use Regex (Regular Expression), /^[a-zA-Z]{3,}$/ would for instance allow a minimum of 3 characters behind the comma would be the max value. As for numbers it would be something like this /^...
Bram B's user avatar
  • 353
3 votes

Programmatically update entry field

Craft 3 $entry->setFieldValue('my_field_handle', 'new_value'); $app->elements->saveElement($entry);
Simon Epskamp's user avatar
3 votes
Accepted

How do I include record relationships in validation?

Yii/Active Record won't do this by default. You'd need to manually add an exists validation rule to the typeId attribute to get it to behave like you're looking for.
Brad Bell's user avatar
  • 67.5k
2 votes

Front-end entry field validation problem

You'll need to make sure the entries title is set otherwise that will throw an error. You can do this using a field in your form or set it in the Control Panel to automatically be set using another ...
Luke Pearce's user avatar
  • 3,863
2 votes
Accepted

How do I write custom model validators for my plugin that refer to other fields?

Looks like I can just refer to the other field name: public function validateUsername($attribute) { $value = $this->$attribute; $otherFieldsValue = $this->otherField; if ($value &...
danieltalsky's user avatar
2 votes
Accepted

Password validation for resetting password

I wasn't validating againts the field newPassword, only validating against password. Hence why I wasn't getting any feedback. Duplicating the validation for newPassword seems to have resolved this.
Terry Upton's user avatar
  • 1,894
2 votes

How do I include record relationships in validation?

Thanks to Brad for pointing me in the right direction. I added this rules method to my record. public function rules() { $rules = parent::rules(); $rules[] = [ 'typeId', 'exist',...
Jamie Pittock's user avatar
2 votes
Accepted

How can I validate a Redactor rich text field in a front-end form using the VeeValidate Vue.js plugin?

Solution Add the Redactor Sync callback function to the Vue.js instance to synchronize code/data between Redactor’s visual layer (UI) and the form’s <textarea> element. As per the Redactor ...
Andrea DeMers's user avatar
2 votes

Craft Commerce: make address phone telephone field required without JavaScript

The way to do address validation with Commerce in gneral is using a plugin and listening to the onBeforeSaveAddress event. Set up a listener with something like this: craft()->on('...
Jeremy Daalder's user avatar
2 votes
Accepted

Do field values get serialized before validation?

First of all: this validator requires an active record as target in order to know what table it should check to search for uniqueness. Since Craft has not such a thing you would need to create a ...
Robin Schambach's user avatar
2 votes

Craft Rules and Validation

I just figured out the answer so I'm going to post it if anyone else is asking this question. Yii accepts aliases as well as function/class names when evaluating the rules function. When using number ...
Mat Weaver's user avatar
2 votes
Accepted

Field formatting

What about using a table field with a column for HH/MM/SS, with each one restricted to numbers? Alternatively you could use https://github.com/fruitstudios/craft-validateit with a custom regex rule, ...
James Greig's user avatar
2 votes

Frontend form validation

I ended up using the Ad Hoc Validation method of Yii2, here is the link for documentation: https://www.yiiframework.com/doc/guide/2.0/en/input-validation#ad-hoc-validation My code for validation: ...
Alex89's user avatar
  • 183
2 votes

Server-side address validation in Craft Commerce 3

According to Luke, this is the preferred method for Commerce 3. In a custom module or plugin: Event::on(\craft\commerce\models\Address::class, \craft\base\Model::EVENT_DEFINE_RULES, static function(\...
GTerrill's user avatar
  • 311
2 votes

Craft Commerce: Frontend Validation on Address Fields

Would removing required via JS be an option?
Matthias Redl-Mann's user avatar
2 votes

How to access 'setRouteParams' model in form validation

In the twig template, myModel should be available as a regular twig variable. Try this: {{ myModel.id }}
Sam Hernandez's user avatar
1 vote

How to validate a unique field in Craft 3 element?

I know, this is an old question, but it might help others in the future. You can add the method defineRules() to your element class, here is a sample from category element: * @inheritdoc */ ...
phaetons's user avatar
  • 705
1 vote

Craft 3 Custom Plugin: model validation

Since Craft 3.4.0 you should be using defineRules() over rules() in your model classes. Models should override this method instead of [[rules()]] so [[EVENT_DEFINE_RULES]] handlers can modify the ...
Ben Croker's user avatar
  • 7,536
1 vote
Accepted

How can I set a custom URI for the confirmation link of account email update from a front-end form?

I can't think of a clean way to do this because of this function public function actionVerifyEmail(): Response { // .. // snip // .. if (Craft::$app->getUsers()->...
Robin Schambach's user avatar
1 vote

Craft Commerce: make address phone telephone field required without JavaScript

You could just create javascript/jQuery validation for your form(s). And whenever the form is submitted or the submit button clicked, validate your fields however you like, and don't submit if they're ...
Jack Sutherland's user avatar
1 vote
Accepted

Show all form errrors using amforms

I'm not sure if this is the intended way, but I rigged this up and so far it works well. {% set yankee = craft.amForms.getForm('hotel') %} {% macro foxtrot(errors) %} {% if errors %} <...
Ryan's user avatar
  • 1,952
1 vote
Accepted

AttributeType::Number giving validation error "Must be an integer"

Not really documented anywhere, but AttributeType::Number can accept an array of options, including min, max and decimal. You're looking for the last one. AttributeType::Number => array('min' =&...
Brad Bell's user avatar
  • 67.5k
1 vote

Validation on username?

I ran into username validation because I had to migrate user names containing spaces (not allowed by default). I ended up creating a custom Username validator and let composer's autoloader use that ...
24HOURSMEDIA's user avatar
1 vote
Accepted

onBeforeSaveUser event does not fire if basic user fields do not validate

I agree it's unexpected behavior, but for Craft 2 it won't change. Craft 3 had a major overhaul of its event system and validation workflow where it works as you'd expect, though.
Brad Bell's user avatar
  • 67.5k

Only top scored, non community-wiki answers of a minimum length are eligible