Skip to main content
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
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
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,897
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
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
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

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

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

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

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

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

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 show flash message for Commerce order when changing status on listing page?

I haven't tried this yet, but I think you can try to use "Server Side Event" with "Control Panel JS plugin for Craft CMS": https://github.com/doublesecretagency/craft-cpjs/blob/v2/README.md Listen to ...
Parry Huang's user avatar
1 vote

Normalize then validate unique entry in a custom module field

If your model somehow extends Yii's Model (which I think it does because you use Yii validators), you can use the beforeValidate() method to change attribute values before validation: public function ...
Paul's user avatar
  • 6,348
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,796
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
Accepted

How do I show errors for a new front end entry form?

Thanks to a teammate, got this working! Needed to change the conditional wrapping my error messages from {% if entry is defined %} to {% if entry is defined and entry is not null %}
Katie Fritz's user avatar
1 vote

How to validate a form request

Yii 2 (which is what Craft 3 is built upon) has similar looking form/model validation: https://www.yiiframework.com/doc/guide/2.0/en/input-validation https://www.yiiframework.com/doc/guide/2.0/en/...
Brad Bell's user avatar
  • 67.6k
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 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,972
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.6k
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.6k
1 vote

Front-end registration validation

The MN Match Input plugin is a fieldtype for fields that match a regular expression. It sound to me as if it will do what you want.
Marion Newlevant's user avatar

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