How do you programatically create decimal fields in Drupal forms? - forms

I simply need to be able to have a text field and validate if input is a decimal number like 100.00, 0.1, 10.6, etc.
I'm using Drupal 7.
Updated question to indicate 'programatic' solution (using Form API).

You could use #element_validate option for your field and write your own validation function. Maybe you could use is_float() or is_number() in your validation function.
So your code
$form['yourField'] = array(
....
'#element_validate'=>'myValidation'
);
....
function myValidation($element, &$form_state, $form){
if (!is_float( $element['#value']) )){
form_error($element, t('This field is not decimal.'));
}
}

Related

How do i poulate a field with a parameter from previous page in a multipage form in gravityforms?

I want to build a multipage from.
The first page asks for first name and last name.
I want to greet the user with his first name in the second page.
The best way to do this is to use Live Merge Tags with Populate Anything:
https://gravitywiz.com/documentation/gravity-forms-populate-anything/#live-merge-tags
If you collected the user's first name in a Name field on page 1, you could great him in the field label for a field on page 2 like so:
Hello, #{Name (First):1.3}
(In this example, the field ID for the Name field is 1. The 3 refers to the first name input of a Name field and will always be 3).
If avoiding another plugin (as useful as that one is), you can use either the pre_submission_filter or pre_submission hooks to do this.
If their name was field 1 and lets say the field you'd like to show is field 2...
// THESE FOUR FILTERS WORK TOGETHER TO PRE-POPULATE ALL SORTS OF STUFF, AND YOU CAN ADD TO THIS AS NECESSARY. MINE IS ABOUT 1500 LINES LONG AND IS USED BY SEVERAL FORMS.
add_filter('gform_pre_render', 'populate_forms');
add_filter('gform_pre_validation', 'populate_forms');
add_filter('gform_pre_submission_filter', 'populate_forms', 10);
add_filter('gform_admin_pre_render', 'populate_forms');
function populate_forms($form) {
$form_id = $form['id'];
$current_form = 2; // pretending the form id you are working on is 2.
$future_form = 10; // imaginary form you'll create later for another purpose.
switch($form_id) {
case $current_form:
$first_name = !empty(rgpost('input_1_3')) ? rgpost('input_1_3') : null; // gets the value they entered into the first-name box of field 1.
foreach ($form['fields'] as &$field) {
if ($field->id === '2') { // Make as many of these as necessary.
if ($first_name) { // make sure there's actually a value provided from field 1.
$field->placeholder = $first_name; // not necessary, just habit since sometimes you'd need to have a placeholder to reliably populate some fields.
$field->defaultValue = $first_name; // this is the piece that will actually fill in the value like you'd expect to see in your question.
}
}
}
break;
//case $future_form: do more stuff.
//break;
}
return $form;
}
That should be a decent start for your functionality plugin where you can populate the current and future forms without much hassle. This can also be done with the gform_field_value hook; I've always found the language a bit clumsy with that one, personally.
The plugin mentioned earlier is definitely neat, but I found myself wanting to rely on that stuff less and less.

How to create floating point validation with two decimals in Yii2?

I have tried creating a validation for a field which stores unit price of a product, I come across validation showing how to check for integer, but I can't found one for floating point number something with a format like 2032.95 in YII2. Thanks in advance.
*
After Abilay instructions I tried
[['quantity','round_off'],'number','numberPattern'=>'[-+]?[0-9]*.[0-9]+|[0-9]+'],
but it shows error in console.
I think, redefining of numberPattern of NumberValidator class will help in your case.
If you use AJAX Validator on your form:
In Model:
[['quantity','round_off'], 'number',
'numberPattern' => '/^\d+(.\d{1,2})?$/'],
In View:
Ensure that you enabled AJAX Validation when created form
$form = ActiveForm::begin([
'id' => 'my-form',
'enableAjaxValidation' => true,
]);
and change field to default input
$form->field($model, 'quantity')
In Controller:
Include files:
use yii\web\Response;
use yii\widgets\ActiveForm;
Past this code at the beginning of action, after
$model is loaded or created
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ActiveForm::validate($model);
}

laravel 4 form helpers - including additional attributes

Within Laravel 4 I can create a select box in a form with this helper:
Form::select('name', $data)
where $data represents an array of the select options.
However I want to now add an extra attribute of 'Required' to help with form validation on the client side.
I can do this if I use normal php and markup but I'd like to use the laravel helper if I can.
I've tried adding:
Form::select('name', $data, array('required'=>'required'))
This doesn't appear to add anything to the final mark up. I've tried the following but this doesn't work either:
Form::select('name', $data, array('required'))
Is there an easy way of doing this or accept that I should revert to another method without a helper
If you check the FormBuilder class the select field is declared as
public function select($name, $list = array(), $selected = null, $options = array())
So what you are passing is the third parameter which should be the default selected option from the list.
In order to achieve what you need you have to pass the array of $options as the fourth parameter (if you don't have default selection just leave the third parameter null)
Form::select('name', $data, null, array('required'=>'required'))

Populate values to checkbox while editing in zend framework

I am trying to populate the values into check box. I want check box to be checked when there is value stored in database.
This is my code in form:
$form ['test_1'] = new Zend_Form_Element_Checkbox('test_1');
$form['test_1']->setLabel('test1')->setCheckedValue('1');
$form ['test_2'] = new Zend_Form_Element_Checkbox('test_2');
$form['test_2']->setLabel('test2')->setCheckedValue('2');
If there is value 1 in database i want first check box to be checked and if its 2 then 2nd checkbox needs to be checked.
What do i need to do in the controller.
Could anyone please help me on this issue.
The easiest way would be to fetch the values from the database as an array that maps to the form input elements, e.g. return a row like
array('test_1' => 'value of checkbox', 'test_2' => 'value of checkbox');
You could then simply call $form->populate($values) and let do Zend_Form do the setting, e.g. in your controller do
public function showFormAction()
{
$form = $this->getHelper('forms')->get('MyForm');
$data = $this->getHelper('dbGateway')->get('SomeTable');
$form->populate($data->getFormData());
$this->view->form = $form;
}
Note: the helpers above do not exist. They are just to illustrate how you could approach this. Keep in mind that you want thin controllers and fat models, so you should not create the form inside the controller, nor put any queries in there.

limiting the no. of characters entered in textbox html helper element in asp.net mvc 2.0

I am using a order form as follows, <%using (Ajax.BeginForm("AddToCart", "../ShoppingCart", null, new AjaxOptions { OnSuccess = "handleUpdate", OnBegin = "ajaxValidate" }, new { #class = "AddToCartForm" }))
{ %>
<%=Html.Label("Quantity")%>
<%=Html.TextBoxFor(model => Model.Count)%>
<% } %>
Now if i entered a large no of t-shirts like 999999999999999999999999999999999999 the Form was unresponsive.So i should limit the no of characters entered in the textbox.So please tell me how to set this limitation so as to not having integer overflow.
Thanks in advance,
And i also restrict they cannot order more than 2*10^31-1 or 32-bit signed integers can hold.
The best way to do this in MVC 2 is to take advantage of the inbuilt validation by using data annotations.
For the Quantity field you should use the Range annotation.
Here is a great post to walk you through it, and if you follow it to the end he covers using jquery to do the client side validation too.
Use MaxLength="Number of characters you wish user should enter" in your code ..
as
<%=Html.TextBoxFor(model => Model.Count, new {MaxLength="10"})%>