In my update method, I am using FormRequest to validate the input fields. I also need to pass and additional parameter for unique validation rule.
I am trying to fetch the $subscriber variable from the url.
SubscriberController.php
class SubscriberController extends Controller
{
public function update(UpdateSubscriberRequest $request, $subscriber)
{
...
}
}
UpdateSubscriberRequest.php
class UpdateSubscriberRequest extends FormRequest
{
private $data = [];
private $subscriber = null;
public function __construct()
{
$this->data = request()->all();
$this->subscriber = request()->route('subscriber');
}
public function rules()
{
return [
...
'email' => [
'required',
...
"unique:subscribers,email,{$this->subscriber},token,company_id,"
. session()->get('COMPANY_ID')
],
'mobile' => [
'required',
...
"unique:subscribers,mobile,{$this->subscriber},token,company_id,"
. session()->get('COMPANY_ID')
],
...
];
}
}
Although the code is working fine. But, is this the right way to do this..?
Related
I have an input, and I want to extract several numbers separated by , and store each number on each new line in the database.
model - CouponDocument
<?php
namespace App\Models;
use App\Http\Controllers\CouponDocumentController;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
class CouponDocument extends Model
{
use HasFactory, SoftDeletes;
protected $table = "coupons_document";
protected $fillable = [
"id",
"cpf",
"coupon_id"
];
protected $hidden = [
'deleted_at',
'created_at',
'updated_at'
];
public function coupon()
{
return $this->belongsTo(Coupon::class, 'coupon_id', 'id')->withTrashed();
}
}
Controller
class CouponDocumentController extends Controller
{
static public function store($request)
{
$data = [];
foreach ($request->input('cpf') as $cpf) {
$data[] = [
'cpf' => trim($cpf),
'coupon_id' => 61
];
}
foreach ($data as $item) {
$couponDocument = new CouponDocument();
$couponDocument->fill($item);
$couponDocument->save();
}
}
}
Resources
class CouponsDocument extends Resource
{
use SearchesRelations;
public static $displayInNavigation = false;
public static $model = \App\Models\CouponDocument::class;
public static $title = 'id';
public static $search = [
'id',
'cpf',
'coupon_id'
];
public static function label()
{
return __('modules.couponDocument.button');
}
public static function singularLabel()
{
return __('modules.couponDocument.button');
}
public function fields(Request $request)
{
return [
Text::make(__('fields.couponDocument.name'), "cpf")
->sortable(),
];
}
where the resource is called
HasMany::make
__("fields.couponDocument.name"),
"documentRelationship",
CouponsDocument::class
I tried to do it with the standard functions of nova, but it ended up not working, I would like to know if there is any other solution
I have a custom validator, extending Zend AbstractValidator. The thing is, i want to include Doctrine EntityManager, but i keep failing! I tried to make a Factory for my Validator, but it doesn't seem to work. Help!! What am I doing wrong?
Validator:
$this->objectRepository stays empty, while i expect content.
namespace Rentals\Validator;
use Rentals\Response;
use Zend\Validator\AbstractValidator;
use Zend\Stdlib\ArrayUtils;
class ExistentialQuantification extends AbstractValidator
{
const NO_ENTITY_ID = 'noEntityId';
const ENTITY_NOT_FOUND = 'entityNotFound';
const INVALID_ID = 'invalidId';
protected $messageTemplates = [
self::NO_ENTITY_ID => 'The input does not contain an entity id.',
self::ENTITY_NOT_FOUND => 'The entity could not be found.',
self::INVALID_ID => 'The input does not contain an entity id.',
];
protected $objectRepository;
public function __construct(array $options)
{
$this->objectRepository = $options['object_repository'];
parent::__construct($options);
}
public function isValid($value)
{
if ($value === null) {
return true;
}
if (! isset($value->id)) {
$this->error(self::NO_ENTITY_ID);
return false;
}
$entityClass = $this->getOption('entity_class');
$controller = new Controller();
$entity = (new FactoryInterface)(EntityManager::class)->find($entityClass, $entity->id);
if (! $entity instanceof $entityClass) {
$this->error(self::ENTITY_NOT_FOUND);
return false;
}
if (! $entity->getId()) {
$this->error(self::NO_ENTITY_ID);
return false;
}
return true;
}
}
Factory:
namespace Rentals\Validator;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\MutableCreationOptionsInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Stdlib\ArrayUtils;
class ExistentialQuantificationFactory implements FactoryInterface, MutableCreationOptionsInterface
{
protected $options = [];
public function setCreationOptions(array $options)
{
$this->options = $options;
}
public function createService(ServiceLocatorInterface $serviceLocator)
{
if (! isset($this->options['object_manager'])) {
$this->options['object_manager'] = 'doctrine.entitymanager.orm_default';
}
$objectManager = $serviceLocator->get($this->options['object_manager']);
$objectRepository = $objectManager->getRepository($this->options['entity_class']);
return new ExistentialQuantification(ArrayUtils::merge(
$this->options, [
'objectManager' => $objectManager,
'objectRepository' => $objectRepository
]
));
}
}
Module config:
<?php
return [
'service_manager' => [
'factories' => [
'Rentals\\Validator\\ExistentialQuantification' => 'Rentals\\Validator\\ExistentialQuantificationFactory'
]
]
];
?>
What if you change your config entry like the following example?
return [
'validators' => [
'factories' => [
ExistentialQuantification::class => ExistentialQuantificationFactory::class,
],
],
];
This change will result in further changes for your factory, because the service locator for the entity manager differs from the one you injected.
namespace Application\Validator\Factory;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\MutableCreationOptionsInterface;
use Zend\ServiceManager\MutableCreationOptionsTrait;
use Zend\ServiceManager\ServiceLocatorInterface;
class ExistentialQuantificationFactory implements FactoryInterface, MutableCreationOptionsInterface
{
use MutableCreatinOptionsTrait;
public function createService(ServiceLocatorInterface $serviceLocator)
{
$parentLocator = $serviceLocator->getServiceLocator();
if (! isset($this->creationOptions['object_manager'])) {
$this->creationOptions['object_manager'] = 'doctrine.entitymanager.orm_default';
}
$objectManager = $parentLocator->get($this->creationOptions['object_manager']);
$objectRepository = $objectManager->getRepository($this->creationOptions['entity_class']);
return new ExistentialQuantification(ArrayUtils::merge(
$this->options, [
'objectManager' => $objectManager,
'objectRepository' => $objectRepository
]
));
}
}
What I 've done here? First I implemented the MutableCreationOptionsTrait class. This trait implements the needed functions for working with creation options. But this is just a little hint for avoiding unnecessary work.
Because of setting the validator class as validator in the config, we have to use the parent service locator for getting the entity manager. The inherited service locator just provides access to validators.
Now you can try to access your validator in your controller like in the following examaple.
$validator = $this->getServiceLocator()
->get('ValidatorManager')
->get(ExistentialQuantification::class, [
'entity_class' => YourEntityClass::class,
]);
\Zend\Debug\Debug::dump($validator, __METHOD__);
The validator manager should return your validator so that you can test it.
I have a form that has a fieldset with a file upload field in it. When I do a var_dump on $form->getData() I am being shown an array of data for the file field:
array (size=13)
'logo' =>
array (size=5)
'name' => string 'my-image.gif' (length=12)
'type' => string 'image/gif' (length=9)
'tmp_name' => string 'C:\xampp\htdocs\images\my-image.gif' (length=35)
'error' => int 0
'size' => int 391
//... other fields here
How do get the element to return only the name when I call getData?
e.g.
array (size=13)
'logo' => string 'my-image.gif' (length=12)
//... other fields here
I am using the form for other things and have already overridden getData so would like to keep the answer located in the fieldset.
You can override the getData() method in your form.
public function getData()
{
$data = parent::getData();
$logo = $data['logo'];
$data['logo'] = $logo['name'];
return $data;
}
Add all necessary precautions to ensure the existence of the keys in the arrays.
Supplements for a fieldset
Using a fileset, you can use a Filter to change the return file structure :
namespace your\namespace;
use Zend\Filter;
class FilterFileName extends Filter\AbstractFilter
{
public function filter($value)
{
if (! is_scalar($value) && ! is_array($value)) {
return $value;
}
if (is_array($value)) {
if (! isset($value['name'])) {
return $value;
}
$return = $value['name'];
} else {
$return = $value;
}
return $return;
}
}
Your fieldset class must implement InputFilterProviderInterface
use your\namespace\FilterFileName;
class YourFieldset extends ZendFiedset implements InputFilterProviderInterface
{
public function __construct()
{
// your code ... like :
parent::__construct('logo');
$file_element = new Element\File('my-element-file');
$file_element->setLabel('Chooze')
->setAttribute('id', 'my-element-file')
->setOption('error_attributes', [
'class' => 'form-error'
]);
$this->add($file_element);
}
public function getInputFilterSpecification()
{
return [
'element-file' => [
'name' => 'my-element-file',
'filters' => [
['name' => FilterFileName::class]
]
]
];
}
}
You can chain multiple filters, eg to rename the file before.
I am trying to extends my form, but I dont know how to do it ...
The problem
My parent formType depends of a option. But I want provide that option from the child form. In symfony documentation they explain a method to add dynamic fields that depends on submit data. But if It have a field with DataTransformer?, because in FormInterface I can't add it.
The code
class TransactionApiType extends AbstractApiType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$app = $options['app'];
$builder
->add('gamer', TextType::class)->addModelTransformer(new GamerExternalIdToStringCreateIfNotExistTransformer($em, $app))
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => Transaction::class,
))
->setAllowedTypes('app', ['AppBundle\Entity\App'])
;
}
}
class TransactionMultiAppConfByAppApiType extends TransactionApiType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('app_id',
EntityType::class,
[
'property_path' => 'app',
'required' => true,
'description' => 'App id',
'class' => App::class,
])
;
// I need pass $options['app'] here to work (App will be submitted),
// How can do? or other possibilities
parent::buildForm($builder, $options);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => Transaction::class,
)
;
}
}
Thanks in advance :-)
Add fields with DataTransformers in formEvents are not enabled https://github.com/symfony/symfony/issues/9355
But there is a "hack"
related here Symfony2 form events and model transformers (Summary it needs create a customType...)
My solution was
class TransactionMultiAppConfByAppApiType extends TransactionApiType
{
// ...
parent::buildForm($builder, $options);
$optionsApp = $builder->get('gamer')->getAttributes()['data_collector/passed_options'];
$builder->remove('gamer');
$builder->get('app_id')->addEventListener(
FormEvents::POST_SUBMIT,
function (FormEvent $event) use ($em, $optionsApp) {
/** #var App $app */
$app = $event->getForm()->getData();
$f = $event->getForm()->getParent();
if ($app)
{
$f
->add('gamer', GamerIdWithExternalIdCustomType::class, $optionsApp + ['em' => $em, 'app' => $app])
;
}
});
// ...
}
class GamerIdWithExternalIdCustomType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->addModelTransformer(new GamerExternalIdToStringCreateIfNotExistTransformer($options['em'], $options['app']));
}
public function getParent()
{
return TextType::class;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver
->setRequired([
'em',
'app',
]);
}
}
How can I set the read-only option only for the first item in the collection when rendering a form?
My simple models:
class Main
{
public $others;
}
class Other
{
public $field1;
public $field2;
}
Simple Form Type for my models:
class MainType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('others', 'collection', array(
'type' => new OtherType(),
'allow_delete' => true,
'allow_add' => true,
'by_reference' => false,
))
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'App\MyBundle\Entity\Main',
));
}
public function getName()
{
return 'maintype';
}
}
class OtherType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('field1')
->add('field2')
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'App\MyBundle\Entity\Other',
));
}
public function getName()
{
return 'othertype';
}
}
And simple action method my controller
//...
public function indexAction($id)
{
$main = new Main();
$other1 = new Other();
$other1->field1 = 'a';
$other1->field2 = 'b';
$main->others[] = $other;
$other2 = new Other();
$other2->field1 = 'c';
$other2->field1 = 'd';
$main->others[] = $other;
$form = $this->createForm(new MainType(), $main);
//...isValid, persist, flush...
}
//...
I can make a condition when manually render the form, but I want to know if possible at the form code to enter such a restriction.
Currently it is not possible to have the rows of a collection have different options. I invite you to create a feature request on the issue tracker if you feel that this would be a valuable addition.