Whenever I submit the form I'm expecting for die() to get fired the Callback Validator doesn't seem to get triggered?
I can't figure out what else I'm missing as I just based this on several examples I saw online.
$inputFilter->add([
'name' => 'flag_reference',
'required' => true,
'allow_empty' => true,
'filters' => [
['name' => StripTags::class],
['name' => StringTrim::class]
],
'validators' => [
[
'name' => Callback::class,
'options' => [
'messages' => [
Callback::INVALID_VALUE => 'Not a valid Reference'
],
'callback' => function($value, $context) {
die('here');
$flagReference= $value;
var_dump('flag reference', $value);
$flag = $context['flag'];
var_dump('flag', $canonicalFlag);exit;
$isValid = false;
if($flag== 'checked') {
$isValid = $flagReference ? true : false;
}
return $isValid;
}
],
]
]
]);
I'm expecting the response to be 'here' but I just get validation errors for the other elements.
Basically I'm trying to accomplish a conditional required.
If a checkbox is checked (flag), the value for a textbox must not be empty (flag_reference).
Related
I am working with Laminas DoctrineObjectInputFilter and want to get value of other property in Callback input filter like this code is in init function of Filter class which extends DoctrineObjectInputFilter
// input filter whose value is required
$this->add([
'name' => 'name',
'allow_empty' => false,
'filters' => []
]);
// Input filter in which I want value of input name
$this->add([
'name' => 'value',
'allow_empty' => true,
'filters' => [
[
'name' => 'Callback',
'options' => [
'callback' => function ($value) {
$name = // want to get property name value here
if (key_exists($name, $this->applicationConfig) && gettype($value) === 'string') {
return trim(strip_tags($value));
}
else {
return trim($value);
}
return $value;
},
],
],
],
]);
have checked $this->getRawValues() but its returning null for all inputs.
a bit late but I guess you 're searching for $context. Since the value of name is in the same InputFilter instance, you can simply use $context in your callback function.
<?php
declare(strict_types=1);
namespace Marcel\InputFilter;
use Laminas\Filter\StringTrim;
use Laminas\Filter\StripTags;
use Laminas\Filter\ToNull;
use Laminas\InputFilter\InputFilter;
use Laminas\Validator\Callback;
class ExampleInputFilter extends InputFilter
{
public function init()
{
parent::init();
$this->add([
'name' => 'name',
'required' => true,
'allow_empty' => false,
'filters' => [
[ 'name' => StripTags::class ],
[ 'name' => StringTrim::class ],
[
'name' => ToNull::class,
'options' => [
'type' => ToNull::TYPE_STRING,
],
],
],
]);
$this->add([
'name' => 'value',
'required' => true,
'allow_empty' => true,
'filters' => [],
'validators' => [
[
'name' => Callback::class,
'options' => [
'callback' => function($value, $context) {
$name = $context['name'];
...
},
],
],
],
]);
}
}
I have the code below but it showing an error "Cannot use empty array elements in arrays".
It seems that the issue is in this line }), collect(json_decode($post['comments'], true))->map(function ($comment) {
Code:
'data' => [
'comments' =>
collect(json_decode($configs['comments'], true))->map(function ($comment) {
return [
'title' => $comment['attributes']['title'],
'message' => $comment['attributes']['message'],
];
}), collect(json_decode($posts['comments'], true))->map(function ($comment) {
return [
'title' => $comment['attributes']['title'],
'message' => $comment['attributes']['message'],
];
}),
]
If we simplify your code it seems like this;
'data' => [
'comments' =>
collect(),
collect()
]
It is not a valid syntax. You can try like this;
$comments = collect(json_decode($configs['comments'], true))->map(function ($comment) {
return [
'title' => $comment['attributes']['title'],
'message' => $comment['attributes']['message'],
];
});
$postComments = collect(json_decode($posts['comments'], true))->map(function ($comment) {
return [
'title' => $comment['attributes']['title'],
'message' => $comment['attributes']['message'],
];
});
'data' => [
'comments' => $comments->merge($postComments)->toArray()
];
I'm trying to setup different user types and their respective permissions in my AppServiceProvider.php in my project, and I get the error
explode() expects parameter 2 to be string, object given
Nowhere in my code do I have an explode() at least that I can see. Before adding the Inertia::share(function(){}) there was no such error.
This is my code:
public function register()
{
Inertia::version(function () {
return md5_file(public_path('mix-manifest.json'));
});
Inertia::share(function () {
$auth = null;
if (Auth::user()) {
$perms = [];
$user = Auth::user();
if ($user->isSuperAdmin() || $user->isAdmin()) {
$perms = [
[
'url' => '/',
'icon' => 'fa fa-home',
'name' => 'Dashboard'
],
[
//rest of permissions
],
];
}
if ($user->isUser()) {
$perms = [
[
'url' => '/',
'icon' => 'fa fa-home',
'name' => 'Dashboard'
],
[
//rest of permissions
],
];
}
$auth = [
'id' => Auth::user()->id,
'name' => Auth::user()->name,
'card' => Auth::user()->card,
'scard' => Auth::user()->scard,
'user_type_id' => Auth::user()->user_type_id,
'email' => Auth::user()->email,
'perms' => $perms
];
}
return [
'app' => [
'name' => Config::get('app.name'),
],
'auth' => [
'user' => $auth,
],
'flash' => [
'success' => Session::get('success'),
],
'errors' => Session::get('errors') ? Session::get('errors')->getBag('default')->getMessages() : (object)[],
]
});
What am I doing wrong? Where i'm getting the error it doesn't specify where the error is, just what it is, it signals the last line of the code I presented as where the error is, but all that's there is the closing parenthesis and brackets.
Knowing nothing of Inertia, it seems you are misusing the Inertia::share function. In their docs, I see 3 examples. The first two have parameter 1 being a string (eg. 'auth.user' or 'app.name'), and the last has parameter 1 being an associative array, so each element still has a unique string key.
In your code, you are passing a closure as the first parameter. I believe that you can fix it by simply adding a name as the first parameter:
Inertia::share('auth.user', function () {
$auth = null;
if (Auth::user()) {
$perms = [];
$user = Auth::user();
if ($user->isSuperAdmin() || $user->isAdmin()) {
$perms = [
[
'url' => '/',
'icon' => 'fa fa-home',
'name' => 'Dashboard'
],
[
//rest of permissions
],
];
}
if ($user->isUser()) {
$perms = [
[
'url' => '/',
'icon' => 'fa fa-home',
'name' => 'Dashboard'
],
[
//rest of permissions
],
];
}
$auth = [
'id' => Auth::user()->id,
'name' => Auth::user()->name,
'card' => Auth::user()->card,
'scard' => Auth::user()->scard,
'user_type_id' => Auth::user()->user_type_id,
'email' => Auth::user()->email,
'perms' => $perms
];
}
return [
'app' => [
'name' => Config::get('app.name'),
],
'auth' => [
'user' => $auth,
],
'flash' => [
'success' => Session::get('success'),
],
'errors' => Session::get('errors') ? Session::get('errors')->getBag('default')->getMessages() : (object)[],
];
});
I have a form. The form has a Collection whose target element is a fieldset with a checkbox and a couple of text fields. The fieldset attached as the target element to Collection looks like this (simplified to avoid too much code):
class AFieldset extends Fieldset implements InputFilterProviderInterface
{
public function __construct(HydratorInterface $hydrator)
{
parent::__construct();
$this->setHydrator($hydrator)
->setObject(new SomeObject());
$this->add([
'type' => Hidden::class,
'name' => 'id',
]);
$this->add([
'type' => Checkbox::class,
'name' => 'selectedInForm',
]);
$this->add([
'type' => Text::class,
'name' => 'textField1',
]);
$this->add([
'type' => Text::class,
'name' => 'textField2',
]);
}
public function getInputFilterSpecification()
{
return [
'selectedInForm' => [
'required' => false,
'continue_if_empty' => true,
'validators' => [
['name' => Callback::class // + options for the validator],
],
],
'id' => [
'requred' => false,
'continue_if_empty' => true,
],
'textField1' => [
'required' => false,
'continue_if_empty' => true,
'validators' => [
['name' => SomeValidator::class],
],
],
'textField2' => [
'required' => true,
'validators' => [
['name' => SomeValidator::class],
],
],
],
}
}
I'd like to validate textField1 and textField2 based on if selectedInForm checkbox is checked in the form.
How could I do this?
I though of using a Callback validator for selectedInForm checkbox like this:
'callback' => function($value) {
if ($value) {
$this->get('textField1')->isValid();
// or $this->get('textField1')->getValue() and do some validation with it
}
}
but the problem with it is that, for some reason, the posted value of textField1 value isn't attached to the input yet. Same is true for textField2.
Two option is available. One is where you started, with callback validators.
The other one is to write a custom validator, and to make it reusable I recommend this solution.
<?php
use Zend\Validator\NotEmpty;
class IfSelectedInFormThanNotEmpty extends NotEmpty
{
public function isValid($value, array $context = null): bool
{
if (! empty($context['selectedInForm']) && $context['selectedInForm']) {
return parent::isValid($value);
}
return true;
}
}
And then you can use it as every other validator:
'textField2' => [
'required' => true,
'validators' => [
['name' => IfSelectedInFormThanNotEmpty::class],
],
],
This may not be your exact case, but I hope it helped to get the idea.
You may define options to make it more reusable with a configurable conditional field in public function __construct($options = null).
I have a moneyFieldset with 2 fields, amount and currency.
class MoneyFieldset ...
{
public function __construct($name = null, $options = array())
{
parent::__construct($name, $options);
$this->setHydrator(...);
$this->add(array(
'name' => 'currency',
'type' => 'select',
'options' => array(
'value_options' => \Core\Service\Money::getAvailableCurrencies(true),
),
'attributes' => array(
'value' => \Core\Service\Money::DEFAULT_CURRENCY,
),
));
$this->add(array(
'name' => 'amount',
'type' => 'text',
));
}
}
public function getInputFilterSpecification()
{
$default = [
'amount' => [
'required' => false,
'allow_empty' => true,
'filters' => [
['name' => AmountFilter::class]
],
'validators' => [
]
],
'currency' => [
'required' => false,
'allow_empty' => true,
'filters' => [
['name' => StringToUpper::class]
],
'validators' => [
]
]
];
return \Zend\Stdlib\ArrayUtils::merge($default, $this->filterSpec, true);
}
I'm using moneyFieldset in my other fieldsets like this:
// Price Field
$this->add(array(
'name' => 'price',
'type' => 'form.fieldset.moneyFieldset',
'attributes' => array(
'required' => true,
'invalidText' => 'Please type an amount'
),
'options' => array(
...
),
));
When I set filter like this:
function getInputFilterSpecification()
{
'price' => [
'required' => true,
'allow_empty' => false,
],
}
It's not working because price has 2 fields, so how can I say price[amount] and price[curreny] is required?
You can provide the input specifications for the nested fieldset (within the context of the form) using the following array structure.
public function getInputFilterSpecification()
{
return [
// ...
'price' => [
'type' => 'Zend\InputFilter\InputFilter',
'amount' => [
'required' => true,
],
'currency' => [
'required' => true,
]
],
//...
];
}
If you are dynamically modifying the values of the input filter it might be worth considering creating the validator using a service factory class and then attaching it to a form using the object API rather than arrays.
As I said in #AlexP's comment, a field, or a group of field declared as Required like this :
function getInputFilterSpecification()
{
'price' => [
'required' => true,
'allow_empty' => false,
],
}
Not means that it will be print an html like this :
<input type="text" required="required"/>
It just check when you'll do $form->isValid() if your fields are empty and required or other checks.
To achieve that, you just have to set in attributes that you want to require those fields. As you already did. Attributes can add, same as class attribute, html code to an input.
P.S : AlexP's answer is the best answer I just give more info about it.