In my Java code im adding a POJO
rootMap.put("_Field_",field);
there is a function "getFilteredHtml" that for the time being does nothing but return the string
that is given as a parameter (the idea is later to alter this - but for test purposes it keeps the html untouched).
/**
* get filtered html for this field
* #param html
* #return
*/
public String getFilteredHtml(String html) {
return html;
}
in the Freemarker template I am using this function like this:
${_Field_.getFilteredHtml(fieldRef?eval!"-")}
which creates the following error message:
Root Cause: freemarker.template.TemplateModelException: Argument type mismatch;
can not unwrap argument #1 (class: freemarker.ext.beans.NumberModel, toString: "4")
to class java.lang.String
I tried to understand what http://freemarker.org/docs/pgui_misc_beanwrapper.html
wants to tell me but this is all greek to me. How can I avoid this Beanwrapper behaviour and make
sure the function getFilteredHtml is simply called with all this mumbo jumbo about wrapping and unwrapping?
Since no other answer showed up I'm citing my comment:
looks like modifying getFilteredHtml to public Object getFilteredHtml(Object html) does the trick. The error message is simply misleading because it talks about freemarker.ext.beans.NumberModel when trying to say that a string is not expected when the input is a long (or some other numeric type)
Related
I recently met an issue with validator (illuminate/validation) under Lumen (5.4.6) (Laravel Components 5.4.*). It seems like the integer rule doesn't work.
I used Paw(or postman) to send companyName with type string and it can pass the validation and 'here i am' can printed out. Even a boolean type, let's say (boolean)companyName=TRUE can pass the validation. Have you met the same issue? this bug is quite obvious but I didn't find the similar discussion on google.
I also tested the string rule 'required|string|min:1' and it works accordingly, neither integer nor boolean parameters can pass the validation.
code screen-shot
Laravel/Lumen makes use of PHP filter_var() function for integer rule validation:
Class Illuminate\Validation\Concerns\ValidatesAttributes:
protected function validateInteger($attribute, $value)
{
return filter_var($value, FILTER_VALIDATE_INT) !== false;
}
As mentioned in the docs:
Note that scalar values are converted to string internally before they are filtered.
So boolean true is converted to string 1 internally and therefore passes validation.
It's not considered a bug, but an implementation detail.
I have a class that returns a collection:
public function getCustomers() : Collection;
When I loop the result, PhpStorm IDE won't know what's inside the collection.
In .NET/JAVA you would write:
public <List<Customer>> getCustomers();
then IDE will knows what's inside the collection and know how to complete/hint that.
Is there any trick to introduce that to PhpStorm?
It's easy with the docblocks:
/**
* #return Customer[]|Collection
*/
public function getCustomers();
Essentially what you are doing here is telling phpStorm that the function returns an array of Customer objects and the Collection as well.
You can type hint multiple returns separating them with a pipe symbol |
I'm new to Laravel Translatable BootForms, and I was wondering something.
When I use this code :
{!!
TranslatableBootForm::text('Nom', 'name')
->required()
!!}
The render is as follows :
I don't know where this language list comes from.
I only want to list some languages specified in my database, as I do with this workaround :
#foreach($availableLangs as $availableLang)
{!!
TranslatableBootForm::text('Nom', 'name')
->renderLocale($availableLang['locale'])
!!}
#endforeach
Which gives me this :
My two questions are :
Where does this language list come from ?
How can I replace it by my own language list ?
Answering the first question may lead to an automatic answer for the second, though)
In Laravel, you should always try to read the Service Providers, they provide important clues about the project structures. Let's try to follow the trail of the function calls:
TranslatableBootForm is a facade and it resolves to and instance of translatable-bootform from the Service Container according to this line:
protected static function getFacadeAccessor() { return 'translatable-bootform'; }
Now, in the file TranslatableBootFormsServiceProvider.php we can see that translatable-bootform is an instance of TranslatableBootForm. So when you call TranslatableBootForm::text, you will be using the Facade which resolves to an instance of TranslatableBootForm
Opening the TranslatableBootForm class, we cannot find the text method, so there should be a __call method. The __call method always returns whatever is returned from the method render. So that's where the action is happening.
Reading the code there, you will find that it gets the locales from a method called locales and it will intersect it with the func_get_args() function to get whatever languages you pass to it. So renderLocale or simply render will do the same thing.
The method locales just returns an array which is by default empty in the class. If we return back to the TranslatableBootFormsServiceProvider we will see that there's an important line:
$formBuilder->setLocales($this->getLocales());
Which gets the locales from Translatable\TranslatableWrapper which is just a wrapper around this file in another package: https://github.com/dimsav/laravel-translatable/blob/master/src/Translatable/Translatable.php
Looking at the configuration file in the laravel-translatable package, we can see the languages:
https://github.com/dimsav/laravel-translatable/blob/master/src/config/translatable.php
Solutions
Now, you can simply copy the file translatable.php in your config folder and set your locales.
Or, you create a new service provider MyTranslatableBootFormsServiceProvider
class MyTranslatableBootFormsServiceProvider extends TranslatableBootFormsServiceProvider
{
/**
* Get Translatable's locales.
*
* #return array
*/
protected function getLocales()
{
// You can return a config key
// return config('yourconfig.locales');
// Or directly the array containing the languages
return ['en', 'fr', 'nl'];
}
}
Then, you will use this provider in your config/app.php instead of the original TranslatableBootFormsServiceProvider
Disclaimer:
I didn't try the code, you might have a bug, but you get the idea now how to find your way around Laravel packages.
I made an extension with the Extension Builder in Typo3 6.2 using Fluid 6.2 and Extbase 6.2.
I made Appointment-Objects with a date property.
I want to enter the date in the format "dd.mm.yyyy".
So I tried this:
And it gives this error:
I'm clueless as I'm not familiar with this and I want to solve this in a nice way.
My createAction code is simply generated by the extension builder and therefore looks like this:
/**
* action create
*
* #param \JH\Appmgmt\Domain\Model\Appointment $newAppointment
* #return void
*/
public function createAction(\JH\Appmgmt\Domain\Model\Appointment $newAppointment) {
$this->addFlashMessage('The object was created. Please be aware that this action is publicly accessible unless you implement an access check. See Wiki', '', \TYPO3\CMS\Core\Messaging\AbstractMessage::ERROR);
$this->appointmentRepository->add($newAppointment);
$this->redirect('list');
}
Now I realize that if I change something here in order for the format to work I would have to do the same thing in the updateAction and maybe others that I don't know about yet.
I also desperately tried to format it to the desired format somehow in the partial but that was bound to fail - same result:
<f:form.textfield property="appointmentDate" value="{appointment.appointmentDate->f:format.date(format:'Y-m-d\TH:i:sP')}" /><br />
So that's where I need your help - I don't know where and how to globally allow this date format since I will be needing it for other fields as well.
The only other thing I can think of is changing something in the domain model:
/**
* appointmentDate
*
* #var \DateTime
*/
protected $appointmentDate = NULL;
but I don't know how I should approach this. :(
Anyone an idea?
You send a date that is not formatted correctly as a date Object.
that's exactly what the error says.
What you can do is re-format the date you send so that it arrives at your controller action as a valid argument for your object. this is done with an initialize action that invokes a property mapping.
a clear example can be found here:
http://www.kalpatech.in/blog/detail/article/typo3-extbase-datetime-property-converter.html
the part that you need is:
// Here we convert the property mapping using the property mapper
public function initializeAction() {
if ($this->arguments->hasArgument('newCalendar')) {
$this->arguments->getArgument('newCalendar')->getPropertyMappingConfiguration()->forProperty('startdate')->setTypeConverterOption('TYPO3\\CMS\\Extbase\\Property\\TypeConverter\\DateTimeConverter',\TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::CONFIGURATION_DATE_FORMAT,'d-m-Y');
}
}
You could also disable the validation of the date arguments to your controller and create a valid date Object from your 'date' and then use setAppointmentDate($yourNewDateObject).
You then go round the extbase validation, what is not a good practise.
I'm using 2.4.7 and I want to include some validation for two fields which take in prices (e.g. €1 or 2 for €3). Originally I thought that perhaps I would need to resort to validating user input but as the answer suggests it was a database issue.
The encoding within SilverStripe was defaulting to ASCII which converted the symbols such as the euro symbol. In the end I need to add
$this->response->addHeader("Content-Type", "application/json; charset='utf-8'");
to the init method in the controller. This corrected the encoding issue and prevented a hacky workaround taking place. Many thanks for the insight on this one.
Thanks
I'm assuming you want to do this in the CMS. If that's the case, the easiest way is probably to create a new class which extends the TextField class and adds a public function validate() method which performs your validation (see the CreditCardField class for an example).
class FancyCurrencyField extends TextField {
public function validate($validator) {
// Do your validation
if ($invalid) {
$validator->validationError(
$this->name,
"Please don't use currency symbols.",
"validation",
false
);
return false;
}
return true;
}
}
Once you've created your class, you can modify the form fields in your getCMSFields() function on the DataObject and use the new class in place of TextField.
Having said that, it feels like output encoding and not input validation is the root cause of your problem. I'd check to make sure that everything is setup to use UTF-8.
I encountered a problem with the PHP function substr. I just used mb_substr instead and it solved my issue.
Please view http://www.silverstripe.org/general-questions/show/11797#post369831