New (Form) Element in Phalcon - model-view-controller

There's a dynamic list of phone numbers, so I thought it would be a good idea to abstract this in a custom element.
There is a problem, though I don't know how to reuse existing elements, or how validation should work ($form->isValid() should check if the phone numbers match a certain pattern, for example).
How would I be able to implement that element?

You can use a regex, there's an example on the official documentation:
<?php
use Phalcon\Validation;
use Phalcon\Validation\Validator\Regex;
$validation = new Validation();
$validation->add(
'telephone',
new Regex(
[
'message' => 'The telephone is required',
'pattern' => '/\+44 [0-9]+/',
'allowEmpty' => true,
]
)
);
https://docs.phalconphp.com/hu/3.2/validation#cancelling
Or you can use a better regex pattern:
$regex = "/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}[\s-]?\d{4}$/i";
https://ericholmes.ca/php-phone-number-validation-revisited/

Related

How to add validation for preventing duplicate level points in laravel query?

Here is a table named stages and below are the fields
I don't want to allow to add the same from and to points example, if points from 1 to 10 is already added means not allow them to add the same range, another one condition is don't allow to add in between points example in-between 1to 10 like 5 to 7 are also not allowed.
Tried laravel query
$isexist = Stage::whereBetween('from', [$request->from, $request->to])
->orWhereBetweenColumn('to','from', [$request->from, $request->to])->exists();
but it not satisfying all conditions.
Any one please help, thanks in advance.
you can validate the data like this
$validator = Validator::make($inputData, [
'label' => 'required|unique:tableName,label'
]);
if($validator->fails()){
//Throw validation errors
}
Or you can use
Model::query()->updateOrCreate(['label' => $request->label], [
'from' => $request->from,
'to' => $request->to
]);
Read more on unique validation reference: https://laravel.com/docs/9.x/validation#quick-writing-the-validation-logic

Laravel 5.5 form validation numeric/integer accepts non-digital characters like "+"

I want to validate the form data if it is just numeric/integer (as in just numbers). Based on Laravel's documentation there are two specific validators for that. But the problem I'm facing is that both the validators accept non-numeric characters such as "+" or "-".
numeric
The field under validation must have a numeric value.
integer
The field under validation must be numeric.
How can I make the validation to only accept numbers and not other non-numeric characters?
'main_telephone' => 'numeric',
'main_fax' => 'integer',
'direct_telephone' => 'integer',
'mobile' => 'integer',
Below is the screenshot
if anyone has come across this problem the best possible solution is to use regex. I do not know why I didn't think of this before.
'main_telephone' => 'integer|regex:/^[0-9]*$/',
'main_fax' => 'integer|regex:/^[0-9]*$/',
'direct_telephone' => 'integer|regex:/^[0-9]*$/',
'mobile' => 'integer|regex:/^[0-9]*$/',

Validate a phone number in Laravel

How would I go about validating a number in laravel.
I need the number stored in the following format 353861111111.
353 will be the prefix, and the user types in the rest. If the user types in 086, this is invalid.
You can use regex as:
'phone' => 'required|regex:/(353)[0-9]{9}/'
This checks for the pattern with starting with 353 followed by 9 digits having values from 0-9.
Or you can build a custom validator in boot method of AppServiceProvider.php:
Validator::extend('phone_number', function($attribute, $value, $parameters)
{
return substr($value, 0, 3) == '353';
});
This will allow you to use the phone_number validation rule anywhere in your application, so your form validation could be:
'phone' => 'required|numeric|phone_number|size:11'
In your validator extension you could also check if the $value is numeric and 11 characters long.
Here is how I did it on an older Laravel 4 project we have to update from time to time:
'phone' => 'required|regex:/^\d{3}-\d{3}-\d{4}$/',
Try this regex expression:
'number' => 'required|regex:^[3][5][3][\d]{8}[\d]$'

Add query parameter to existing parameters with route-helper

I use the route-helper ({{ route('routename') }}) in my Blade template files to filter and/or sort the results of the page.
What is the easiest way to attach a parameter to the previous ones?
As an example:
I visit the page /category1 and see some products. Now I use the sorting which changes the URL to /category1?sort_by=title&sort_order=asc
If I use another filtering now, I would like the parameter to be appended to the current one. So to /category1?sort_by=title&sort_order=asc&filter_by=year&year=2017 but the result is only /category1?filter_by=year&year=2017 .
I create the Urls with the route-helper like:
route('category.show', [$category, 'sort_by' => 'title', 'sort_order' => 'asc'])
route('category.show', [$category, 'filter_by' => 'year', 'year' => $year])
You could probably use something like:
$new_parameters = ['filter_by' => 'year', 'year' => $year];
route('category.show', array_merge([$category], request()->all(), $new_parameters]);
to use all previous parameters and add new ones.
Obviously you might want to use only some of them, then instead of:
request()->all()
you can use:
request()->only('sort_by', 'sort_order', 'filter_by', 'year')

Magento Adminhtml: Where do forms come from?

My question comes from seeing this question and not being able to find the correct answer.
When adding a new product, where does the actual code come from for the input fields? In the aforementioned question, the desire is to add maxlength attribute to the input box. I dug around for over an hour and did find plenty of form helpers but not the one for this exact case.
How do I find the true origin of this (or any) form in Magento?
If I'm understanding your question correctly, majority of Magento's form fields come from Varian_Data_Form However you can easily specify a maxlength property via a higher up call, as in:
$fieldset->addField('title', 'text', array(
'label' => Mage::helper('form')->__('Title3'),
'maxlength' => '30', // <-- change here
'class' => 'required-entry',
'required' => true,
'name' => 'title',
'onclick' => "alert('on click');",
'onchange' => "alert('on change');",
'style' => "border:10px",
'value' => 'hello !!',
'disabled' => false,
'readonly' => true,
'after_element_html' => '<small>Comments</small>',
'tabindex' => 1
));
Example (and modified) from:
http://www.excellencemagentoblog.com/magento-admin-form-field
Related:
Extend a Varien Form Element for a Custom Module
Is it good practice to add own file in lib/Varien/Data/Form/Element folder
I accepted B00MER's answer because it led me to what I believe to be the Real Answer...
Clicking on the links he listed, the pattern of $fieldset->addField presented itself as a key way to grep in files.
user#magento:~/www/app$ grep -rin "addField.*text" * | grep -i product
code/core/Mage/Adminhtml/Block/Catalog/Product/Edit/Tab/Super/Config/Simple.php:140: $fieldset->addField('simple_product_inventory_qty', 'text', array(
There were about a dozen results which were quickly narrowed down (we're not caring about giftcard or Attribute sets). I'm not 100% sure THIS file is the answer but it seems that some logic could be added to catch whether or not the input's name=="Name" and then a maxlength could then be added in.

Resources