How to override validators in Wagtail StructBlock - django-models

my code:
from django.core.validators import RegexValidator
URL_VALIDATOR_MESSAGE = 'Not a valid URL'
URL_VALIDATOR = RegexValidator(regex='http\D+', message=URL_VALIDATOR_MESSAGE)
class SezPubBlock(blocks.StructBlock):
sez = blocks.ListBlock(
blocks.StructBlock(
[
("title", blocks.CharBlock(classname="full title", icon="title", required=True)),
("url", blocks.URLBlock(required=True, validators=[URL_VALIDATOR])),
], label="Link", icon="link"
), label="Link sezione"
)
link i need to insert: http://intranet/example/
the link is without dots and match regular expression 'http\D+'
but when i save the page get the default error "Enter a valid URL."
if i add ".com" http://intranet.com/example/, works.
i think the default validator is not overrided

i found a solution. i changed URLblock with CharBlock and it works
class SezPubBlock(blocks.StructBlock):
nome_sezione = blocks.CharBlock(required=True, verbose_name="My custom label")
sez = blocks.ListBlock(
blocks.StructBlock(
[
("title", blocks.CharBlock(classname="full title", icon="title", required=True)),
("url", blocks.CharBlock(required=True, validators=[URL_VALIDATOR], icon="link")),
], label="Link", icon="link"
), label="Link sezione"
)

Related

Hook into elementor widget?

I am trying to find a hook that will let me add my own code into an existing elementor widget. For example they have the "post widget" which lets you display a list of posts based on the conditions/categories you set.
I would like to add my own code into this "block" but am unable to find any specific hooks for hooking into an existing widget (specifically the posts widget)
Any help would be much appreciated. Is there a hook for this? If not what is my next best option?
Thanks!
This depends on what you want to achieve but in general there are hooks.
I am not sure about the posts-widget but I can show you some examples in general.
If you want to add controls to a widget use this (you can find additional information to the names and stuff in their documentation https://developers.elementor.com/add-controls-to-widgets/)
add_action( 'elementor/element/heading/section_title/before_section_end', function( $element, $args ) {
$element->add_control( 'title_color',
[
'label' => 'Color' ,
'type' => \Elementor\Controls_Manager::SELECT,
'default' => 'red',
'options' => [
'red' => 'Red',
'blue' => 'Blue',
],
'section' => 'section_title',
'tab' => 'content',
]
);
}, 10, 2);
the widget in the example is the heading. You can find out the registered names by inspecting the editor or the blocks inside the plugin directory
If you want to change the rendered content of a widget you can use this
add_action( 'elementor/widget/render_content', function( $content, $widget ){ // $content (string) = the rendered content of the widget; widget = the data object
if ($widget->get_name() === 'heading') { // your targeted widget
$settings = $widget->get_settings(); // every that is stored in this method. Titles, captions, margins and so on. Usually this is all you need
// eg if you simply want to wrap your widgets content you can do something like this
$content .= '<div class="i-am-a-wrapper">'.$content.'</div>';
}
return $content;
}, 10, 2 );
I hope this helps :)

Elementor custom category not showing up in edit panel

Im trying to build a custom Elementor widget in my own category, but when I created a custom category, it was not displayed in the edit panel, which means I have to search my widget name to find it.
Does any know how to fix it?
The code:
function add_elementor_widget_categories() {
\Elementor\Plugin::instance()->$elements_manager->add_category(
'category-name',
[
'title' => __( 'Test Category', 'plugin-name' ),
'icon' => 'fa fa-plug',
]
);
}
add_action( 'elementor/elements/categories_registered', 'add_elementor_widget_categories' );
This code is from Elementor sample code, but I don't know why it doesn't work.

Form elements not being submitted

So I've got a weird problem that I'm having a hard time figuring out. I've got a simple form with a few elements that are not being submitted, all of these elements have only one thing in common, they're select elements:
echo $this->Form->control("spirit_type_id", [
"label" => false,
"type" => "select",
"options" => $spirit_types,
"empty" => "Spirit Type"
]);
echo $this->Form->control("country_id", [
"label" => false,
"type" => "select",
"options" => $countries,
"empty" => "Country"
]);
echo $this->Form->control("region_id", [
"label" => false,
"type" => "select",
"options" => $regions,
"empty" => "Region"
]);
And in my controller I have:
public function add() {
$spirit = $this->Spirits->newEntity();
$spirit_types = $this->Spirits->SpiritTypes->find("list");
$countries = $this->Spirits->Countries->find("list");
$regions = $this->Spirits->Regions->find("list");
if ($this->request->is("post")) {
debug($this->request->getData());
die();
$spirit = $this->Spirits->patchEntity($spirit, $this->request->getData());
$spirit->user_id = $this->Auth->user("id");
if ($this->Spirits->save($spirit)) {
$this->Flash->success("Your spirit was successfully saved.");
$this->redirect(["action" => "index"]);
} else {
$this->Flash->error("Your spirit could not be saved.");
}
}
$this->set(compact("spirit", "spirit_types", "countries", "regions"));
}
The important part is that debug statement. It shows this when I insert data using the form.
[
'name' => 'Longrow Peated',
'image' => 'imageLocation',
'brand' => 'Springbank',
'age' => '',
'cost' => '55'
]
Those are all text and/or number elements in my form, and they all come out just fine. It gets a little weirder though. I have validation in my table to require those id fields:
public function validationDefault(Validator $validator) {
$validator->requirePresence(
"name", "brand", "spirit_type_id", "country_id", "region_id", "age", "cost", "image"
)
->notEmpty("name", "We require a name")
->notEmpty("brand", "We require a brand or distillery")
->notEmpty("spirit_type_id", "We require a type of alchohol")
->notEmpty("country_id", "We require a country of origin")
But this doesn't ever seem to get triggered when I insert the data using patchEntity, it's only caught when I actually call the save function and I try inserting into the database.
If $this->request->getData() is not showing all of your fields, the most likely cause would be some sort of problem with your form; there are not a lot of ways for CakePHP to discard your data from here. You can narrow it down by using browser tools (built into most of them now) to inspect the data actually being sent from your browser in the page request.
If it turns out that the fields really aren't being sent across at all, the problem is almost certainly in your form. For example, you might be closing it early, or there might be HTML errors that confuse the browser. Make sure that all of your input tags are between the <form> and </form>, and if they are then try an HTML validator to check your code. There are lots of options online, and even the inspectors built into browsers can often help you spot these sorts of issues.
This is the most common problem:
If you check debug($this->request->getData()); before $spirit = $this->Spirits->newEntity(); you then see all submitted data!
Next go to Spirit Entity and double check if your fields "spirit_type_id,.." accessible!
/**
* Fields that can be mass assigned using newEntity() or patchEntity().
*
* Note that when '*' is set to true, this allows all unspecified fields to
* be mass assigned. For security purposes, it is advised to set '*' to false
* (or remove it), and explicitly make individual fields accessible as needed.
*
* #var array
*/
protected $_accessible = [
'*' => true, // quick fix
'id' => false,
];
or better way:
protected $_accessible = [
'spirit_type_id' => true,
'country_id' => true,
// etc ...
];
Edit
debug
$spirit = $this->Spirits->patchEntity($spirit, $this->request->getData());
debug($spirit); exit();
see if any errors.

creatable props not working in react-virtualized-select component

I am using 'Creatable' props of react-virtualized-select. When I write a custom option(e.g. Test code 4), It shows the text 'create option "Test code 4". But, When I click on that option, dropdown becomes blank and that option also not added in option list.
Library Link to demo : https://bvaughn.github.io/react-virtualized-select/
Library Link to demo Source-code : https://github.com/bvaughn/react-virtualized-select/blob/master/source/VirtualizedSelect/VirtualizedSelect.example.js
Options: Below list is coming from database.
dataSource = [
{name: "Test1", label: "Test code 1", type: "text"},
{name: "Test2", label: "Test code 2", type: "text"},
{name: "Test3", label: "Test code 3", type: "text"}
]
Component:
import { Creatable } from 'react-select'
handleOptionChange( selectedValue ) {
this.setState({
selectedValue: selectedValue
});
}
<VirtualizedSelect
labelKey='label'
clearable={ clearable }
disabled={ disabled }
multi={multi}
handleOptionChange={this.handleOptionChange}
options={ dataSource }
searchable={ searchable }
selectedValue={ selectedValue }
selectComponent={Creatable}
valueKey='name'
/>
I could not figure out what else I am missing here ?
Thanks in advance.
I had the same issue. In my case the problem was caused by the version of react-select. I upgraded react-select from 0.9.2 to v1.2.1 and this did the trick:
"react-select": "v1.2.1"
I also added following imports which are present in some examples (I don't know if this is crucial):
import React, { Component, PropTypes } from 'react';

Custom label for FieldPanel in Wagtail

Is there any way to set custom label to FieldPanel in Wagtail? I want to do something like:
class BlogPage(Page):
intro = models.CharField(max_length=255)
panels = [
FieldPanel('intro', label = "My custom label")
]
The model field accepts a verbose_name property, which will be picked up for use as a form field label:
class BlogPage(Page):
intro = models.CharField(max_length=255, verbose_name="My custom label")
panels = [
FieldPanel('intro')
]

Resources