Symfony2 custom validation - validation

I have a "product" entity and i want to validate a property(for example price) of this class with a custom callback function.
My custom validation is more complex than the defaults validation provided by sf2(minLength, max, etc). I wish to do something like this:
class Product
{
/**
* #Assert\NotBlank()
* #Assert\CallbackValidationFunction('validatePrice', 'Your price is not the expected')
*/
private $price;
}
function validatePrice($priceValue){
$x = " i want";
return $priceValue == "the value".$x;
}
then, in the errors the message 'Your price is not the expected' is related withthe property $price in Product after a $form->isValid() or a product validation via $this->get('validator');

You'd be better off writing a custom validation constraint. See http://symfony.com/doc/current/cookbook/validation/custom_constraint.html for instructions.

Related

Customise Laravel Nova destructive action modal

I am defining a new destructive action in Laravel Nova using the documentation, and I am wondering if it is possible to customise the modal message, which says "Are you sure you want to run this action".
So far, all I have been able to do is replace this message with a field by doing the following:
public function fields()
{
return [
Text::make('This is a test field')
];
}
But this is bringing up a text field for the user to fill out. How can I just have text here, without having a user input field please?
According to Laravel Nova release notes, from version 2.5.0 onwards you can customise action modal's confirmation.
Override the $confirmText attribute in your Action class.
For example:
class YourAction extends Action
{
/**
* The text to be used for the action's confirmation text.
*
* #var string
*/
public $confirmText = 'Your confirmation text?';
}

Magento 2.3 graphql get custom attribute option values

I’m pretty frustrated with the graphql api of Magento. How is it not possible to receive the values of a custom attribute. I mean it’s easy to make a custom select-option attribute and making it available through graphql but you are only getting the integer value.
What’s the best procedure to following
1. Query the complete attribute to get the value
2. Extending the graphql schema (this involves a developer every time the client changes something)
3. Am I missing some functionality inside the graphql endpoints to Fix this?
From Native GraqpQl we cannot get the expected result.But we can write a custom module with the graphql and achieve it.What my suggestion is get the product detail by sku using _productRepository->get($sku);. This will return the entire product details.So you can get the attribute data by using $attributes = $_product->getAttributes(); Here is my data provider file.
/**
* #params string $sku
* this function return all the product data by product sku
**/
public function getProductBySku($sku)
{
return $this->_productRepository->get($sku);
}
/**
* #params int $id
* this function return all the word of the day by id
**/
public function getAttributesBySku( $sku ){
$_product = $this->getProductBySku($sku);
$attributes = $_product->getAttributes();// All Product Attributes
$attributes_data = [];
$x=0;
foreach ($attributes as $attribute) {
if($attribute->getIsUserDefined()){ // Removed the system product attribute by checking the current attribute is user created
$attributeLabel = $attribute->getFrontend()->getLabel();
$attributeValue = $attribute->getFrontend()->getValue($_product);
if($attribute->getAttributeCode()=="language"){
$attributeLabelAndValue = $attributeLabel." - ".$attributeValue;
$attributes_data[$x]['atr_data'] = $attributeLabelAndValue;
}
}
$x++;
}
return $attributes_data;
}
This code will return the need
$attribute->getFrontend()->getLabel(); this will return the label and
$attribute->getFrontend()->getValue($_product); this will return the value.
To check the entire graphQl query and resolver file kindly view How to get product attribute value, label using GraphQl in Magento 2.3?
Hope this will help you.
You can try creating custom graphql attribute with custom resolver.
$product = $this->_productRepository->getById($value['entity_id']);
$data = array();
foreach ($args['fields'] as $fi) {
$att = $product->getResource()->getAttribute($fi);
if (isset($att) && $att) {
if (in_array(
$att->getFrontendInput(),
['multiselect', 'select']
)) {
$data[$fi . '_label'] = $product->getAttributeText($fi);
}
$data[$fi] = $product->getData($fi);
}
}
return json_encode((object) $data);
which should display all provided attributes with their labels.
"sku": "testProduct",
"fabric": 60,
"work": 65,
"dynamicAttributes": "{
"fabric_label":"Pure Silk",
"fabric":"60",
"work_label":"Tanchoi",
"work":"65"
}",
Check out entire module here

get the current discount for a given user in laravel-spark

I'm using laravel-spark to handle stripe payments on my website, the users can apply coupons on their subscriptions so they will have discounts, now I want to show user discount on the profile page.
I searched on the spark documentation about how to retrieve the current discount and they didn't mention that anywhere.
I found that laravel-spark has this feature implemented in the CouponController method current()
CouponController :
/**
* Get the current discount for the given user.
*
* #param Request $request
* #param string $userId
* #return Response
*/
public function current(Request $request, $userId)
{
$user = Spark::user()->where('id', $userId)->firstOrFail();
if ($coupon = $this->coupons->forBillable($user)) {
return response()->json($coupon->toArray());
}
abort(204);
}
I tried to call this method in my view with Vue as the following:
axios.get('/coupon/user/'+ this.user.id).then(response => {
console.log("response : ", response);
coupon = response.data.data;
if (coupon) {
//do something
}
}, error => {
console.log("some error occurred", error);
});
but the response is always empty with 204 status code, even if the user has a valid coupon on his stripe account.
any suggestions ?!
Finally I found that this method inside the coupon controller get the valid coupons that applied to customers not to subscriptions,
so all you need is to apply the coupons per customer not per subscription on stripe.
or you can write your logic to retrieve discounts via stripe API's , you can find all details here : https://stripe.com/docs/api#discounts
Hope that this will help you .

symfony2 (doctrine) currency constraint not working

I have an entity set up like this:
use Symfony\Component\Validator\Constraints as Assert;
...
/**
* #var decimal $amount
* #Assert\Currency
* #ORM\Column(name="amount", type="decimal")
*/
private $amount;
If I submit my form with blank in the amount field nothing happens. Shouldn't my form automatically throw an error or what am I missing?
You need a NotBlank assertion (#Assert\NotBlank) to validate that that field wasn't submitted with empty data. If that passes, then your other assertions should run on any actual data submitted.

Magento - Dynamically disable payment method based on criteria

I have an extremely simple module that allows a customer to "Purchase On Account". The module doesn't do anything special really (it was simply modified from a Cash On Delivery module.)
The problem is I only want to offer this payment method to logged in customers.
So far my module looks like this:
BuyOnAccount/
etc/
config.xml
system.xml
Model/
PaymentMethod.php
The content of PaymentMethod.php is:
class MyCompany_BuyOnAccount_Model_PaymentMethod extends Mage_Payment_Model_Method_Abstract
{
protected $_code = 'buyonaccount';
protected $_isInitializeNeeded = true;
protected $_canUseInternal = false;
protected $_canUseForMultishipping = false;
}
The config and system xml files contain the usual sort of thing (please let me know if you would like to see the code and i'll edit)
So bascically I need to disable the module if the user is not logged in (but obviously only for the current customer session!)
Any ideas?
Thanks
You can just add a method to your payment model called isAvailable(Mage_Sales_Model_Quote $quote) that returns a bool. For example, in your situation you could add something like:
public function isAvailable($quote = null) {
$isLoggedIn = Mage::helper('customer')->isLoggedIn();
return parent::isAvailable($quote) && $isLoggedIn;
}
The Mage_Payment_Model_Method_Free payment method that ships with Magento is an example of a payment method that employs this -- it'll only show if the basket total is zero.

Resources