2022 Developer Survey is open! Take survey.
7 votes
Accepted

How can I add custom validation rules to my plugin settings?

The gist is, you will need to give your plugin a dedicated Settings model class, where you will have complete control over its validation rules and errors. Here’s how to set that up: Create a new ...
user avatar
6 votes

Programmatically update entry field

I got around this issue by looping through all fields that were of BaseElementFieldType re-setting them to their current value. $fields = $entry->getFieldLayout()->getFields(); $...
user avatar
5 votes
Accepted

How to return errors from a model back to the template?

If you use $model->getErrors() instead, it should return a multidimensional array of errors indexed by attribute name like so: array( 'email' => array( 'First email error.', ...
user avatar
  • 66.4k
5 votes
Accepted

Entry validation not working when Publish live changes permission is not checked

As carlcs said, this is already working as expected. Craft only enforces content validation rules for enabled entries (not including drafts), and most people prefer that behavior. If you want to ...
user avatar
4 votes
Accepted

Merge element type errors? (title + hard-coded field)

You can pass the element off to ContentService::validateContent() in the event that it doesn't pass the record’s validation, just to get those errors mixed into the element as well. if (!$record->...
user avatar
4 votes

Programmatically update entry field

You're very close. Try this: $entry->getContent()->setAttribute('metaStatus', 'Declined'); craft()->entries->saveEntry($entry);
user avatar
  • 7,228
4 votes

Entry validation not working when Publish live changes permission is not checked

As other answers have touched on, the behaviour you're seeing is by design. I've written a plugin called Always Validate which will make Craft validate all disabled entries and categories – including ...
user avatar
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/...
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', ...
user avatar
  • 4,509
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 */ $...
user avatar
3 votes
Accepted

Contact Form Plugin: Validate if a checkbox is checked?

There's no clean way doing this without making changes to the plugin itself. The contactForm.beforeSend event makes it possible to prevent the email from being sent from another plugin, but you're ...
user avatar
  • 35.8k
3 votes
Accepted

A plugin to validate a field depending on another field's value

In your FieldType you have $this->element available, representing the Element the field is in. If you use $this->element->lightSwitchHandle you can access its value.
user avatar
3 votes
Accepted

How do I get the last uri segment from within a plugin (php)

Have a look at the segments property (shortcut for getSegments() method) of the HttpRequestServices class, which returns an array of uri segments. craft()->request->segments; To get the last ...
user avatar
3 votes

Can a model or record have conditional requirements?

I can think of a couple of options and I'll just throw them all out there. Have a separate model per partial form and each model performs its own validation. Have a single model, but have it override ...
user avatar
  • 66.4k
3 votes

"Custom" validation for plugin settings page

It sounds like you're on the right track. You could create a custom validator, as detailed here. <?php namespace Craft; use Carbon\Carbon; class MyPlugin_ExampleModel extends BaseModel { ...
user avatar
3 votes
Accepted

Repopulate a static select input

No, I don't think there's a better way, you need to add that conditional to each select option. But what you could do is to refactor a bit and move that logic to a Twig macro. {% macro stateOption(id,...
user avatar
  • 35.8k
3 votes

You don’t have the proper credentials to access this page

Be sure that you're testing as a logged-out user on the front-end of the site. If you have an active Craft session, you could be getting a false positive error message.
user avatar
  • 66.4k
3 votes

Programmatically update entry field

I'm not completely sure of the technical reasons (I guess something to do with relation fields not have a column in the craft_content table), but you also need to re-save the existing ids for any ...
user avatar
  • 4,361
3 votes
Accepted

Logged in after email validation

I believe you're looking for the aptly named autoLoginAfterAccountActivation config setting.
user avatar
  • 66.4k
3 votes

Entry validation not working when Publish live changes permission is not checked

That's the idea behind Drafts, to store entries not yet ready to publish, unfinished ones so to speak. I think it does make sense to not validate the fields. Good feature!
user avatar
  • 35.8k
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.
user avatar
  • 66.4k
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 /^...
user avatar
  • 353
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
user avatar
  • 6,637
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 ...
user avatar
3 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 ...
user avatar
  • 175
2 votes

You don’t have the proper credentials to access this page

I ran into this issue too, but there is another cause to this error. Go into Settings > Users > Settings in the Control Panel and ensure "Allow public registration?" is checked, it's unchecked by ...
user avatar
  • 403
2 votes
Accepted

Using the required field validation in custom element types/plugins?

Custom field validation happens automatically for you when you pass your element into craft()->elements->saveElement(). That function will return true or false, largely depending on whether ...
user avatar
2 votes

Customise errors when validating model data?

This might be similar to Brad Bell's recommendation, but if you are using Yii's built-in validation rules, you should just override the rules() method in your model or record, specify the rule there, ...
user avatar
2 votes

Customise errors when validating model data?

In addition to Brad’s answer (which is what I was looking for), you could also use template-specific messages using conditionals if for some reason you only needed to override the global error ...
user avatar
  • 1,128
2 votes
Accepted

Entry Form Validation on Front End Form

You're correct. Craft will not run validation upon saving disabled elements. This is not a bug – it's just designed that way (as far as I can tell from looking at the relevant source code in the ...
user avatar

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