I am validating number field below code is working fine. It is checking the characters should not be more then 2 or less, which is fine for me.
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'id' => 'required|min:2|max:2',
'title' => 'required'
];
}
But when I add more validation rule like (numeric), min and max validaiton rules get changed, now it is being checked by the numeric number should not be greater than 2... WHY? IS IT BUG?
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'id' => 'required|min:2|max:2|numeric',
'title' => 'required'
];
}
the min and max rule depend on the type of the field. If it is a string it will check the length. If it is a number it will check the value.
So if you want to have numbers only with two chars set use
'id' => 'required|min:10|max:99|numeric',
The field under validation must have a size matching the given value. For string data, value corresponds to the number of characters. For numeric data, value corresponds to a given integer value. For files, size corresponds to the file size in kilobytes.
Related
How can I make a custom rule using Validation, so that the field can be nullable 'since' call function the result is true, otherwise, the field becomes required.
Of course I tried to use the 'nullable', but even if the field is empty, the Validation should execute the checkAreasDiff() function to validate that the field can be empty during the update.
In my controller, I created a function:
private function validator_update(array $data) {
\Validator::extend('areas_diff', function($attribute, $value, $parameters, $validator) {
return checkAreasDiff();
}, 'VALIDATOR AREAS_DIFF OK.');
/**
* RULES
*/
$rules = [
'fiscalizoarea' => 'areas_diff',
];
/**
* Return \Validator
*/
return \Validator::make($data, $rules, $msgs);
}
If I understand the question correctly, you want one field to be required only if another is not null?
There is a Laravel rule for that already: required_with.
required_with:foo,bar,...
The field under validation must be present and not empty only if any
of the other specified fields are present.
Or, if I'm getting your logic back to front: required_without
required_without:foo,bar,...
The field under validation must be present and not empty only when any
of the other specified fields are not present.
In my Admin I'v defined this:
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
/* ... */
->add('camps', 'sonata_type_model', array(
'btn_add' => false, //Specify a custom label
'choices' => $this->tema_service->getCampsDefinicio($this->getSubject()),
'sortable' => true,
'multiple' => true,
), array(
'placeholder' => 'No selected'
))
;
/* ... */
}
Function getCampsDefinicio returns an array of items ordered. Inspite of it, the options appear sorted according its id.
What can I do to get it respect the order given?
It does not care if I have to override any template. In that case which files I have to look for/override?
Finally I solved this.
I was a little confused.
The order in the choises array is respected only for selectionable items.
The order for the SELECTED items is defined by te ArrayCollection provided in the entity function
/**
* Get camps
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getCamps()
{
return $this->camps;
}
I have input text field whith validation rule :
public function rules()
{
return [
'field' => 'url',
];
}
It`s not required (field can be empty) but validation return an error.
Solve problem use "nullable":
public function rules()
{
return [
'field' => 'nullable|url',
];
}
Add sometimes rule to the validation. This means to validate it for a URL when the value is not given or null.
public function rules()
{
return [
'field' => 'sometimes|url',
];
}
when we submmitting values from js, like through a FormData, the value null could be submitted as string containing 'null', which may pass a nullable rule, cause further type check fail. so be sure to make this value be posted as '', literaly nothing, no a 'null' string
When you use formData (js) to submit your request, "null" is assigned by default for all empty fields. This will pass through Laravel "nullable" validaton and indicate as invalid input. So, please, use something like below in your validation rules.
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
$rules = [];
if($this->filled('field') && $this->field!= 'null'){
$rules['field'] = 'url';
}
return $rules;
}
In order to do this use laravel's form requests. https://laravel.com/docs/8.x/validation#creating-form-requests
public function rules() {
return [
'num_of_devices' => 'integer'
];
}
I want to validate an integer field if and only if the field is entered. But above rule validates it for integer even if the field is empty. I used somtimes, but no result. But when I var_dump($num_of_devices) it is string.I am using Laravel 5.2. I think It was working fine in 5.1.
From version 5.3 you can use nullable
public function rules() {
return [
'num_of_devices' => 'nullable | integer'
];
}
Add a rule to array, if input is not empty. You could collect all your validation rules to $rules array and then return the array.
if( !empty(Input::get('num_of_devices')) ){
$rules['num_of_devices'] = 'integer';
}
You’re looking for the sometimes validation rule. From the documentation:
…run validation checks against a field only if that field is present in the input array.
How can we change the position (Sort Order) of admin form element?
TL;DR:
You can specify a caret ^ as an optional fourth argument when defining your field:
$fieldset->addField('my_element','text',array(
'name' => 'my_element',
'label' => Mage::helper('customer')->__('My Element')
),'^');
More Information
See Varien_Data_Form_Abstract::addField:
/**
* Add child element
*
* if $after parameter is false - then element adds to end of collection
* if $after parameter is null - then element adds to befin of collection
* if $after parameter is string - then element adds after of the element with some id
*
* #param string $elementId
* #param string $type
* #param array $config
* #param mixed $after
* #return Varien_Data_Form_Element_Abstract
*/
public function addField($elementId, $type, $config, $after=false)
{
if (isset($this->_types[$type])) {
$className = $this->_types[$type];
}
else {
$className = 'Varien_Data_Form_Element_'.ucfirst(strtolower($type));
}
$element = new $className($config);
$element->setId($elementId);
if ($element->getRequired()) {
$element->addClass('required-entry');
}
$this->addElement($element, $after);
return $element;
}
The comments tell you how to order your elements. So, to position your field, specify a fourth argument $after of somefield or whatever is the ID of the field you wish to place yours after.
This is useful when you are extending the customer account form within your own class. Here's an example that places a new form element above the first in the form:
class Yourcompany_Adminhtml_Block_Customer_Edit_Tab_Account extends Mage_Adminhtml_Block_Customer_Edit_Tab_Account {
...
public function initForm() {
parent::initForm();
$form=$this->getForm();
$fieldset=$form->getElement('base_fieldset');
$fieldset->addField('my_element','text',array(
'name' => 'my_element',
'label' => Mage::helper('customer')->__('My Element')
),'^');
return $this;
}
...
}
You may notice something strange. My fourth argument to the addField call is a caret ('^'). This places the element at the very top of the form, which is what you appear to be asking in your question.
But wait! Why doesn't this match up to the code documentation shown above?! Well, it looks like a disconnect in consistent coding convention during development. If you follow the code, you will find that addField makes it way to the Varien_Data_Form_Element_Collection::add method. It is in here that we see a 2nd argument, $after, which notes in the comments:
/**
* Add element to collection
*
* #todo get it straight with $after
* #param Varien_Data_Form_Element_Abstract $element
* #param boolean|'^'|string $after
* #return Varien_Data_Form_Element_Abstract
*/
public function add(Varien_Data_Form_Element_Abstract $element, $after=false)
Here we find that false appends the element to the fieldset, a ^ character prepends the element, and passing in an element ID will place the element after the one specified.
UPDATE: How to Deal With Existing Fields
What if you want to re-order an existing field? Unfortunately, ordering takes places at the time of adding the field, and cannot conveniently be changed after that. But I can offer 2 approaches to get the job done.
Method 1: Re-Order by Removing and Adding Again
$fieldset->removeField('core_element');
$fieldset->addField('core_element','text',array(
'name' => 'core_element',
'label' => Mage::helper('customer')->__('Core Element')
),'^');
This is a simple solution, but requires you to manually re-define that field. In some cases this will be easy enough. In others, not so. For those scenarios, see next.
Method 2: Re-Order by Cloning
$element=clone $form->getElement('core_element');
$fieldset->removeField('core_element');
$fieldset->addElement($element,'^');
I think this is the best solution, because it preserves the element definition as it was originally written. This is especially important in the case of attributes whose fields are generated in part by their frontend_input_renderer classes. Learn more about that here.
Check the 'attribute_id' & 'sort_order' fields in 'customer_eav_attribute' table.