Optional OptionsetField is SilverStripe 3.2 - silverstripe

In SilverStripe 3.1.* I used some forms with OptionsetFields which are optional, so not required to make a choice. This was working well. But since i updated to SilverStripe 3.2.0 the form doesn't accept this anymore. When submit it comes with a validation message, even if I don't use validation in the whole form.
See this screenshot:
This is the code of the field:
$ClassField = OptionsetField::create('Class', _t('General.CLASS', "Class"), array(
"S" => "S",
"E" => "E",
"U" => "U",
"R" => "R",
"O" => "O",
"P" => "P"
));
Any idea to make this field optional again in SilverStripe 3.2.0 ?

This seems to be a bug in 3.2, you have now to tell OptionsetField it can be empty by using setEmptyString():
$ClassField = OptionsetField::create(
'Class',
_t('General.CLASS', "Class"),
array(...)
)
->setEmptyString('none');

You might be also interested in the silverstripe-display-logic module, which allows you to display certain fields based on some conditions: https://github.com/unclecheese/silverstripe-display-logic

Related

Symfony:4 Expected argument of type "integer", "NULL" given at property path

Getting below error,
Expected argument of type "integer", "NULL" given at property path "experience".
Code is as below,
->add('experience', TextType::class, [
'constraints' => [
new NotBlank([
"message" => $this->translator->trans('Please enter experience.')
]),
new Length([
'max' => 2
]),
],
'required' => false
])
When I submit blank form (create time), It show me "Please enter experience."
Now on edit form, If I insert wrong value (more data like 3434) It show me value should not more than 2 character
Problem arise when I send blank data on edit time.
Submitting with blank experience (its compulsory field in form), It gives below error
Expected argument of type "integer", "NULL" given at property path "experience".
I had tried different options from internet like setting "empty_data" to null or '' but its not working.
Your setter method should allow null values if you accept ones, judging by the form:
public function setExperience(?int $experience): void
{
$this->experience = (int) $experience;
}

Schema.org properties "logo", "mainEntityOfPage", "dateModified" give error in Google SDTT

Following a tutorial I integrated the structured data in my WordPress website but when I run the google sd testing tool I continue to get an error that I can not fix on the logo and other minor things.
Here is my code (for what regards the "Article").
$payload["#type"] = "Article";
$payload["url"] = $post_url;
$payload["author"] = array(
"#type" => "Person",
"name" => $author_data->display_name,
);
$payload["headline"] = $post_data->post_title;
$payload["datePublished"] = $post_data->post_date;
list($width, $height) = getimagesize( $post_thumb );
$payload["image"] = array(
"#type" => "ImageObject",
"url" => $post_thumb,
"height" => "350",
"width" => "590");
$payload["ArticleSection"] = $category[0]->cat_name;
$payload["Publisher"] = "MyWebsite";
All works good but I got this error/warnings:
logo: mancante e obbligatorio (missing and required
dateModified: mancante e consigliato (Missing and recommended)
mainEntityOfPage: mancante e consigliato (Missing and recommended)
Can someone suggest me how to complete my code?
It seems that Google has recently changed the requirements for Article rich snippets.
See this link for more information : https://productforums.google.com/forum/#!topic/webmasters/ltbw0gUvReM
And this one for documentation : https://developers.google.com/structured-data/rich-snippets/articles#article_markup_properties
Hope it helps.
Edit for logo :
Follow this link to know how to set up the logo : http://schema.org/logo
The messages from the Google SDTT tell you everything you need to know.
Google requires the logo property (details) and recommends to provide the dateModified (details) and the mainEntityOfPage (details) properties.
Note that Google requires/recommends this only for their search result features. From the Schema.org perspective, it’s perfectly fine not to provide these properties.

Symfony 2 : render string as choice in a form

I have a entity, Comment. and when a user adds a comment, besides the comment he gives, he can choose between "good", "OK", "bad" etc.
I know how to do it by using two related entities, one for Comment, another for the choice ("good", "bad", etc). then create a relation between these two entities.
But it seems like an overkill for this. Anyone knows a simple way to achieve it? I wish I can save the choice as a string, but render it as a choice in the comment form.
Thanks!
You could just save it as a string along with the comment. In your Comment FormType, just add a field for the rating, something like:
$builder->add('rating', 'choice', array(
'choices' => array(
'good' => 'Good',
'ok' => 'OK',
'bad' => 'Bad',
)
));
And in your Comment Entity, just add the corresponding field and setter/getter.

Symfony Unit Testing - Set Value for Javascript Rendered Elements

In Unit Testing, How can I set a value to the Select box options in which drop down options are rendered from Javascript ?
When I set a value, I am getting Invalidargument exception.
Note: Form is a general HTML Form
Referred links: symfony unit tests: add/modify form action
Thanks for # Matteo comments.
In Unit testing,
For setting values for the Select box, which are not available in the Drop down,
Use Posting the data instead of submitting the form,
$this->client->request('POST', $postUrl, $formValueArray);
$formValueArray = array('data' => 'value');
or
$formValueArray = array(
'myform' => array(
'data' => 'value'
))
);
Note: It can be used to set all the form fields which are not available in the forms.

Field api, defining new fields and getting/setting values

I'm able to create a field, using
$form['title'] = array( '#type' => 'text', '#value' => 'Drupal');
1) But I want to be able to get/set the value from the database. How would I do it?
2 )P.S. what's the point of having a '#" in front of type and value ?
First of all I don't know what Drupal version are you using.
Secondly try to get some more info from the my answer:
1) Use Database API in order to get values from database.
2) Read more about Form API.
Hope this helps.

Resources