ZF2: allow empty fieldset, but validate if least one is filled out - validation

I have defined a fieldset for phone numbers. This contains fields "type" (private, Office mobile ...) and "number". The Input filter for number is "required => true":
``
class PhoneFieldset extends BaseFieldset implements InputFilterProviderInterface
{
public function __construct()
{
parent::__construct('phones');
$this->setHydrator(new DoctrineHydrator($this->getEntityManager(), 'HtsBase\Entity\Phone'))
->setObject(new Phone());
$this->add(array(
'type' => 'DoctrineORMModule\Form\Element\EntitySelect',
'name' => 'type',
'options' => array(
'label' => 'Type',
'empty_option' => '',
'object_manager' => $this->getEntityManager(),
'target_class' => 'HtsBase\Entity\OptionlistPhoneType',
'property' => 'name',
),
'attributes' => array(
#'id' => 'type',
'class' => 'input-medium',
),
));
$this->add(array(
'name' => 'number',
'options' => array(
'label' => 'Number',
),
'attributes' => array(
'type' => 'text',
#'id' => 'number',
'class' => 'input-medium',
'maxlength' => '25',
'autocomplete' => 'off',
),
));
}
public function getInputFilterSpecification()
{
return array(
'type' => array(
'required' => false,
),
'number' => array(
'required' => true,
'filters' => array(
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'max' => 25,
),
),
),
),
);
}
``
Can i attach a validator/filter to the entire fieldset? So that if "type" AND "number" are empty the fieldset is valid, but validate if least one is filled out?

I found an easy to use solution, although I don't use the form any more I now heavily use the InputFilter and still needed the same stuff. But found an easy solution
The AbstractFilterValidator, my own implementation
abstract class AbstractFilterValidator extends AbstractValidator
{
/**
* Returns true if and only if $value meets the validation requirements
*
* If $value fails validation, then this method returns false, and
* getMessages() will return an array of messages that explain why the
* validation failed.
*
* #param mixed $value
* #return bool
* #throws Exception\RuntimeException If validation of $value is impossible
*/
public function isValid($value)
{
$this->setValue($value);
$filter = $this->buildFilter();
$filter->setData($value);
if (!$filter->isValid()) {
$this->abstractOptions['messages'] = $filter->getMessages();
return false;
}
return true;
}
/**
* #return array
*/
public function getMessages()
{
return $this->abstractOptions['messages'];
}
/**
* #return InputFilter\InputFilter
*/
abstract protected function buildFilter();
}
Old answer
Although you were using the InputFilterProviderInterface, I used Zend\InputFilter\InputFilter and wanted the same as you. If the fieldset was not filled in, validate true.
To do this I replace isValid with the following;
public function isValid()
{
$values = array_filter($this->getRawValues());
if (empty($values)) {
return true;
}
return parent::isValid();
}
It simply filters the array from all empty array keys, see docs for info about that. Then a check if the $values is empty, and if so return true. Else the validators are called.
Well I needed something again but needed a decent solution. Still no luck finding a good one, so I wrote the following code.
<?php
namespace Application\InputFilter;
use Zend\InputFilter as ZFI;
class InputFilter extends ZFI\InputFilter
{
private $required = true;
/**
* #return boolean
*/
public function isRequired()
{
return $this->required;
}
/**
* #param boolean $required
*
* #return $this
*/
public function setRequired($required)
{
$this->required = (bool) $required;
return $this;
}
/**
* #return bool
*/
public function isValid()
{
if (!$this->isRequired() && empty(array_filter($this->getRawValues()))) {
return true;
}
return parent::isValid();
}
}
github gist
You can now simply call setRequired(false) on a InputFilter

Related

Laravel Resource

I have multiple resources and mostly resource content few fields that are same for all other resource and it's very difficult to modify all the resource in case I need to update/add key/value in the resource.
Is there any way that I can create one main resource that will contain all common fields and then call the main resource in my another resource and add few additional fields.
Here is my controller where I am calling CitizenFeedResource file.
if ($events->total()) {
return CitizenFeedResource::collection($events);
}
This is my CitizenFeedResource file.
use Illuminate\Http\Resources\Json\JsonResource;
class CitizenFeedResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* #param \Illuminate\Http\Request $request
* #return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'title' => $this->title,
'description' => $this->description,
'start_timestamp' => optional($this->start_timestamp)->toDateTimeString(),
'end_timestamp' => optional($this->end_timestamp)->toDateTimeString(),
'location' => [
'name' => $this->location,
'landmark' => $this->landmark,
'coordinates' => $this->coordinates,
'city' => $this->city,
],
'open_event' => $this->open_event,
'full_day_event' => $this->full_day_event,
'banner' => $this->banner,
'url' => $this->path(),
'web_url' => $this->webUrl(),
'categories' => $this->categories,
'timestamp' => $this->created_at->toDateTimeString(),
'timestamp_ago' => $this->created_at->diffForHumans(),
'statistics' => $this->statistics,
'additional_details' => $this->additionalDetails(),
'municipal_details' => $this->municipal_details,
'user' => optional($this->user)->getProfile($this->channel, '1.1'),
'complaint_id' => $this->complaint_id,
'volunteers' => (isset($this->volunteers) && $this->volunteers) ? $this->user->getVolunteerProfile($this->volunteers, '1.1') : array(),
'share_count' => (isset($this->statistics) && isset($this->statistics['share_count'])) ? array_sum($this->statistics['share_count']) : 0,
'volunteer_status' => $this->getVolunteerStatus($request),
'editable' => $this->editable(),
'type' => 'Event',
];
}
}
You don't have to extend directly from JsonResponse, so you can create one main object let's say like this:
use Illuminate\Http\Resources\Json\JsonResource;
class BaseResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* #param \Illuminate\Http\Request $request
* #return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'title' => $this->title,
'description' => $this->description,
];
}
}
and then
class CitizenFeedResource extends BaseResource
{
/**
* Transform the resource into an array.
*
* #param \Illuminate\Http\Request $request
* #return array
*/
public function toArray($request)
{
$data = parent::toArray($request);
$data['custom_field'] = $this->custom_field;
// ...
return $data;
}
}

Customize Validation Messages in Laravel Requests

How do i customize my Validation Messages in My REQUESTS FILE?
how do i add messages next to the rules?
What i want is to put customized messages just like the common validation. Is it possible? to do just the normal way of validation in the Requests?
<?php
namespace App\Http\Requests;
use App\Http\Requests\Request;
class ArticleRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'title' => 'required|min:5',
'content' =>'required',
'user_id' => 'required|numeric',
'category_id' => 'required|numeric',
'published_at' => 'required|date'
];
}
}
You can define a messages() method with validation rules for that form request only:
class StoreArticleRequest extends Request
{
//
public function messages()
{
return [
'title.required' => 'The title is required.',
'category_id.numeric' => 'Invalid category value.',
];
}
}
It takes the form of the field name and the rule name, with a dot in between, i.e. field.rule.
You may customize the error messages used by the form request by
overriding the messages method. This method should return an array of
attribute / rule pairs and their corresponding error messages:
public function messages()
{
return [
'title.required' => 'A title is required',
'body.required' => 'A message is required',
];
}
https://laravel.com/docs/5.3/validation#customizing-the-error-messages
I use this solution to translate the field labels:
...
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'title' => 'required|min:5',
'content' =>'required',
'user_id' => 'required|numeric',
'category_id' => 'required|numeric',
'published_at' => 'required|date'
];
}
/**
* Get the validation attributes that apply to the request.
*
* #return array
*/
public function attributes()
{
return [
'title' => __('app.title'),
'content' => __('app.content'),
'user_id' => __('app.user'),
'category_id' => __('app.category'),
'published_at' => __('app.published_at')
];
}

How to build rule exist in or equal to a number in cakephp 3?

I have table comments with column parent_id.
And this is content of CommentsTable.php:
namespace App\Model\Table;
use App\Model\Entity\Comment;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
/**
* Comments Model
*/
class CommentsTable extends Table
{
/**
* Initialize method
*
* #param array $config The configuration for the Table.
* #return void
*/
public function initialize(array $config)
{
$this->table('comments');
$this->displayField('id');
$this->primaryKey('id');
$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'joinType' => 'INNER'
]);
$this->belongsTo('Posts', [
'foreignKey' => 'post_id',
'joinType' => 'INNER'
]);
$this->belongsTo('ParentComments', [
'className' => 'Comments',
'foreignKey' => 'parent_id'
]);
$this->hasMany('ChildComments', [
'className' => 'Comments',
'foreignKey' => 'parent_id'
]);
}
/**
* Default validation rules.
*
* #param \Cake\Validation\Validator $validator Validator instance.
* #return \Cake\Validation\Validator
*/
public function validationDefault(Validator $validator)
{
$validator
->add('id', 'valid', ['rule' => 'numeric'])
->allowEmpty('id', 'create')
->requirePresence('body', 'create')
->notEmpty('body')
->requirePresence('path', 'create')
->notEmpty('path')
->add('status', 'valid', ['rule' => 'numeric'])
->requirePresence('status', 'create')
->notEmpty('status')
->add('created_at', 'valid', ['rule' => 'datetime'])
->requirePresence('created_at', 'create')
->notEmpty('created_at')
->add('updated_at', 'valid', ['rule' => 'datetime'])
->requirePresence('updated_at', 'create')
->notEmpty('updated_at');
return $validator;
}
/**
* Returns a rules checker object that will be used for validating
* application integrity.
*
* #param \Cake\ORM\RulesChecker $rules The rules object to be modified.
* #return \Cake\ORM\RulesChecker
*/
public function buildRules(RulesChecker $rules)
{
$rules->add($rules->existsIn(['user_id'], 'Users'));
$rules->add($rules->existsIn(['post_id'], 'Posts'));
$rules->add($rules->existsIn(['parent_id'], 'ParentComments'));
return $rules;
}
}
I want to build rule for field parent_id: exist in ParentComments or equal to 0.
Can you help me?
Thank you very much.
Rules are just callable functions or callable classes. The existsIn() function is just an alias for the ExistsIn class. We can use the to our advantage:
...
use Cake\ORM\Rule\ExistsIn;
class CommentsTable extends Table
{
...
public function buildRules(RulesChecker $rules)
{
...
$rules->add(
function ($entity, $options) {
$rule = new ExistsIn(['parent_id'], 'ParentComments');
return $entity->parent_id === 1 || $rule($entity, $options);
},
['errorField' => 'parent_id', 'message' => 'Wrong Parent']
);
return $rules;
}
}

Magento 1.9: How to create order programmatically

I found similar topics, but they are not up to date.
I have to generate around 1000 orders with random products. I found good article about creating orders http://inchoo.net/magento/programmatically-create-order-in-magento/ but it's kinda old and it's not working on version 1.9.
I get following errors:
array (size=5)
0 => string 'Please specify the product's option(s).' (length=39)
1 => string 'Please specify the product's option(s).' (length=39)
2 => string 'Please specify the product's option(s).' (length=39)
3 => string 'Please specify the product's option(s).' (length=39)
4 => string 'Please specify the product's option(s).' (length=39)
#0 E:\xampp\htdocs\magento\app\code\core\Mage\Adminhtml\Model\Sales\Order\Create.php(1631): Mage::throwException('')
#1 E:\xampp\htdocs\magento\app\code\core\Mage\Adminhtml\Model\Sales\Order\Create.php(1523): Mage_Adminhtml_Model_Sales_Order_Create->_validate()
As you can see in my create method I add options to product, so why do I get this errors?
Here is my class
class MyNamespace_MyModule_Model_Generate_Sample {
protected $orderData;
protected $product;
public function run()
{
//$customer is a random customer object
$product = Mage::getModel('catalog/product')->load(342);
$this->product = $product;
$this->orderData = array(
'session' => array(
'customer_id' => $customer->getId(),
'store_id' => Mage::app()->getStore('default')->getId(),
),
'payment' => array(
'method' => 'checkmo',
),
'add_products' =>array(
$product->getId() => array('qty' => 1),
),
'order' => array(
'currency' => 'USD',
'account' => array(
'group_id' => $customer->getGroupId(),
'email' => $customer->getEmail()
),
'billing_address' => array(
'customer_address_id' => $customer->getCustomerAddressId(),
'prefix' => '',
'firstname' => $customer->getFirstname(),
'middlename' => '',
'lastname' => $customer->getLastname(),
'suffix' => '',
'company' => '',
'street' => array($customer->getStreet(),''),
'city' => $customer->getCity(),
'country_id' => $customer->getCountryId(),
'region' => '',
'region_id' => $customer->getRegionId(),
'postcode' => $customer->getPostcode(),
'telephone' => $customer->getTelephone(),
'fax' => '',
),
'shipping_address' => array(
'customer_address_id' => $customer->getCustomerAddressId(),
'prefix' => '',
'firstname' => $customer->getFirstname(),
'middlename' => '',
'lastname' => $customer->getLastname(),
'suffix' => '',
'company' => '',
'street' => array($customer->getStreet(),''),
'city' => $customer->getCity(),
'country_id' => $customer->getCountryId(),
'region' => '',
'region_id' => $customer->getRegionId(),
'postcode' => $customer->getPostcode(),
'telephone' => $customer->getTelephone(),
'fax' => '',
),
'shipping_method' => 'flatrate_flatrate',
'comment' => array(
'customer_note' => 'This order has been programmatically created via import script.',
),
'send_confirmation' => 0
),
);
$this->create();
}
/**
* Creates order
*/
public function create()
{
$orderData = $this->orderData;
if (!empty($orderData)) {
$this->_initSession($orderData['session']);
$this->_processQuote($orderData);
if (!empty($orderData['payment'])) {
$this->_getOrderCreateModel()->setPaymentData($orderData['payment']);
$this->_getOrderCreateModel()->getQuote()->getPayment()->addData($orderData['payment']);
}
$item = $this->_getOrderCreateModel()->getQuote()->getItemByProduct($this->product);
foreach($this->product->getAttributes() as $option)
{
if ($option->getIsVisibleOnFront()) {
$item->addOption(new Varien_Object(
array(
'product' => $this->product,
'code' => $option->getAttributeCode(),
'value' => $option->getFrontend()->getValue($this->product)
)
));;
}
}
Mage::app()->getStore()->setConfig(Mage_Sales_Model_Order::XML_PATH_EMAIL_ENABLED, "0");
$_order = $this->_getOrderCreateModel()
->importPostData($orderData['order'])
->createOrder();
$this->_getSession()->clear();
Mage::unregister('rule_data');
return $_order;
}
return null;
}
protected function _processQuote($data = array())
{
/* Saving order data */
if (!empty($data['order'])) {
$this->_getOrderCreateModel()->importPostData($data['order']);
}
$this->_getOrderCreateModel()->getBillingAddress();
$this->_getOrderCreateModel()->setShippingAsBilling(true);
/* Just like adding products from Magento admin grid */
if (!empty($data['add_products'])) {
$this->_getOrderCreateModel()->addProducts($data['add_products']);
}
/* Collect shipping rates */
$this->_getOrderCreateModel()->collectShippingRates();
/* Add payment data */
if (!empty($data['payment'])) {
$this->_getOrderCreateModel()->getQuote()->getPayment()->addData($data['payment']);
}
$this->_getOrderCreateModel()
->initRuleData()
->saveQuote();
if (!empty($data['payment'])) {
$this->_getOrderCreateModel()->getQuote()->getPayment()->addData($data['payment']);
}
return $this;
}
/**
* Retrieve order create model
*
* #return Mage_Adminhtml_Model_Sales_Order_Create
*/
protected function _getOrderCreateModel()
{
return Mage::getSingleton('adminhtml/sales_order_create');
}
/**
* Retrieve session object
*
* #return Mage_Adminhtml_Model_Session_Quote
*/
protected function _getSession()
{
return Mage::getSingleton('adminhtml/session_quote');
}
/**
* Initialize order creation session data
*
* #param array $data
* #return Mage_Adminhtml_Sales_Order_CreateController
*/
protected function _initSession($data)
{
/* Get/identify customer */
if (!empty($data['customer_id'])) {
$this->_getSession()->setCustomerId((int) $data['customer_id']);
}
/* Get/identify store */
if (!empty($data['store_id'])) {
$this->_getSession()->setStoreId((int) $data['store_id']);
}
return $this;
}
}
Thanks for any help

Laravel JsonResource: array_merge_recursive(): Argument #2 is not an array

I have a JsonResource of Post that should return a single post. But after joining some other data I get this error: array_merge_recursive(): Argument #2 is not an array.
This does not work:
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($slug)
{
// $post = Post::findOrFail($id);
$post = Post::where('slug', $slug)->first();
// return single post as resource
return new PostResource($post);
}
When I directly return $posts, I get a json back, almost fine. But it doesnt contain the joined data comment.
Here is the class Post extends JsonResource.
public function toArray($request)
{
// return parent::toArray($request);
$img = '.'.pathinfo('storage/'.$this->image, PATHINFO_EXTENSION);
$imgName = str_replace($img,'', $this->image);
$img = $imgName.'-cropped'.$img;
return [
'id' => $this->id,
'title' => $this->title,
'body' => $this->body,
'excerpt' => $this->excerpt,
'image' => asset('/storage/' . $this->image),
'image_small' => asset('storage/' . $img),
'author_id' => $this->author_id,
'category_id' => $this->category_id,
'seo_title' => $this->seo_title,
'slug' => $this->slug,
'meta_description' => $this->meta_description,
'meta_keywords' => $this->meta_keywords,
'status' => $this->status,
'featured' => $this->featured,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'user' => User::find($this->author_id),
'commentCount' => $this->comment->where(['status' => 1, 'id_post' => $this->id])->count(),
];
}
// **Big mistake below here**:
public function with($request)
{
// return [
// 'version' => '1.0.0',
// ];
}
Model:
class Post extends Model
{
public $primary_key = 'id';
public $foreign_key = 'id_post';
public function user()
{
return $this->belongsTo('App\User', 'id_author', 'id');
}
public function comment()
{
return $this->belongsTo('App\Comment', 'id', 'id_post');
}
}
Why do I get a warning about array_merge_recursive()?
I wan't reproduce issue with your code, but - are you sure you included everything? Looking at https://laravel.com/docs/5.6/eloquent-resources#writing-resources it's possible to define additional data data will be returned too like this:
/**
* Get additional data that should be returned with the resource array.
*
* #param \Illuminate\Http\Request $request
* #return array
*/
public function with($request)
{
return [
'meta' => [
'key' => 'value',
],
];
}
So I was able to reproduce the issue when I added to this Post resource class the following method:
public function with($request)
{
return 'test';
}
as you see it's returning just string and not array and then I was getting same error as you did.
But when I didn't have this method implemented at all or when I return just an array, everything is fine.
So to sum up - make sure you don't have with method defined that returns something else than array.

Resources