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 ...
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', ...
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 */
$...
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 ...
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
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 /^...
3
votes
Programmatically update entry field
Craft 3
$entry->setFieldValue('my_field_handle', 'new_value');
$app->elements->saveElement($entry);
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.
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 ...
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('...
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, ...
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 ...
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 ...
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(\...
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:
...
2
votes
Craft Commerce: Frontend Validation on Address Fields
Would removing required via JS be an option?
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 }}
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 ...
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 ...
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 ...
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()->...
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 %}
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/...
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
*/
...
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 ...
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 %}
<...
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' =&...
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 ...
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.
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.
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
validation × 80plugin-development × 27
form × 16
plugin-craftcommerce × 8
models × 8
craft3 × 5
custom-field × 5
users × 4
datetime × 4
controller × 4
registration × 4
frontend-form × 4
entry × 3
error-message × 3
user-profile × 3
yii × 3
records × 3
entries × 2
emails × 2
modules × 2
entries-field × 2
frontend × 2
custom-fieldtype × 2
ecommerce × 2
plugin-craftcommerce3 × 2