Validating Form using codeigniter - validation

I have the following form:
<label>One</label>
Product ID:<input type="text" name="productid[]" value="">
Product Quantity: <input type="text" name="quantity[]" value=""> <br>
<label>Two</label>
Product ID:<input type="text" name="productid[]" value="">
Product Quantity: <input type="text" name="quantity[]" value=""> <br>
<label>Three</label>
Product ID:<input type="text" name="productid[]" value="">
Product Quantity: <input type="text" name="quantity[]" value=""> <br>
<!-- there may be more inputs like above (users can create new inputs
as many as they want) // I have a jquery function to create new rows-->
<input type="submit" value="submit">
Now My question is how to validate the form using Codeigniter when I have input names like this- name="productid[]" instead of name="productid" in my form.
Usually I use to validate my form like this way but this time it won't work for the form above.
How to validate it?

You would use the literal field names, with brackets:
$this->form_validation->set_rules('product_id[]', 'Product', 'required');
$this->form_validation->set_rules('quantity[]', 'Quantity', 'required');
This will run the validation on every field with that name. If you have to validate only a specific index, once again - use the literal field name (and specify the index in your HTML):
// <input name="product_id[3]">
$this->form_validation->set_rules('product_id[3]', 'Product', 'required');
It's all in the documentation for Codeigniter's Form Validation class:
Using Arrays as Field Names
The Form Validation class supports the use of arrays as field
names. Consider this example:
<input type="text" name="options[]" value="" size="50"
/>
If you do use an array as a field name, you must use the EXACT
array name in the Helper Functions that
require the field name, and as your Validation Rule field name.
For example, to set a rule for the above field you would use:
$this->form_validation->set_rules('options[]', 'Options',
'required');
Or, to show an error for the above field you would use:
<?php echo form_error('options[]'); ?>
Or to re-populate the field you would use:
<input type="text" name="options[]" value="<?php echo
set_value('options[]'); ?>" size="50" />
You can use multidimensional arrays as field names as well. For
example:
<input type="text" name="options[size]" value="" size="50"
/>
Or even:
<input type="text" name="sports[nba][basketball]" value=""
size="50" />
As with our first example, you must use the exact array name in the
helper functions:
<?php echo form_error('sports[nba][basketball]'); ?>
If you are using checkboxes (or other fields) that have multiple
options, don't forget to leave an empty bracket after each option, so
that all selections will be added to the POST array:
<input type="checkbox" name="options[]" value="red" />
<input type="checkbox" name="options[]" value="blue" />
<input type="checkbox" name="options[]" value="green" />
Or if you use a multidimensional array:
<input type="checkbox" name="options[color][]" value="red"
/> <input type="checkbox" name="options[color][]"
value="blue" /> <input type="checkbox"
name="options[color][]" value="green" />
When you use a helper function you'll include the bracket as
well:
<?php echo form_error('options[color][]'); ?>

Did You try this ...? A few lines below in the guide ...

Related

Laravel 5: validation error return values to fields

When I am creating a new entry and leave at least one field to error out then submit the form, upon validation the field values are blank.
Is there a way to return the values to their corresponding field when submitting? Something similar to CodeIgniter's set_value()?
On your form html, set the default value to
old('field_name')
Example if you are using laravel's blade:
<input class="form-control" name="name" type="text" value="{{ old('name') }}" id="name">
Example without blade:
<input class="form-control" name="name" type="text" value="<?php echo old('name'); ?>" id="name">
You can also use:
Input::old('field_name')

How to click on a nested checkbox using Capybara

I want to check the first checkbox with id=user_accepts_terms. This is the HTML:
<div class="check-group">
<div class="checkbox">
<input type="hidden" value="0" name="user[accepts_terms]">
</input>
<input id="user_accepts_terms" type="checkbox" value="1" name="user[accepts_terms]">
</input>
<label class="" for="user_accepts_terms">
</label>
</div>
<div class="checkbox">
<input type="hidden" value="0" name="user[subscribed]">
</input>
<input id="user_subscribed" type="checkbox" value="1" name="user[subscribed]">
</input>
<label class="m-focus" for="user_subscribed">
</label>
</div>
I want to check the first checkbox with id=user_accepts_terms. Tried this among other things, but no luck:
find('.check-group').all('.checkbox')[0].find("#user_accepts_terms").set(true)
The .find("#user_accepts_terms").set(true) doesn't work, it says unable to find the css.
This piece works as follows:
2.1.0 :097 > find('.check-group').all('.checkbox')[0].text
=> "I accept the terms of use and privacy policy"
The .all('.checkbox')[0] portion is already finding the checkbox you want, and the .find("#user_accepts_terms") portion is trying to find another element below that, which doesn't exist. Either of the following should work, provided the syntax is correct (I'm unfamiliar with it)
find('.check-group').find("#user_accepts_terms").set(true)
find('.check-group').all('.checkbox')[0].set(true)

passing array to view codeigniter

I have the problem of doing the validation of fields myself. Now there are 5-6 fields in the form. So I am checking each one in my controller, and if there is wrong i wish to load the view again and pass the error array to it.
I achieved the above functionality with this:
<html>
<head>
<title>My Form</title>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
</head>
<body>
<?php
echo $fullname;
?>
<?
echo form_open('/membership/register');
?>
<h5>Username</h5>
<input type="text" name="username" value="" size="50" />
<h5>Password</h5>
<input type="text" name="password" value="" size="50" />
<h5>Password Confirm</h5>
<input type="text" name="cpassword" value="" size="50" />
<h5>Email Address</h5>
<input type="text" name="email" value="" size="50" />
<h5>Mobile</h5>
<input type="text" name="mobile" value="" size="15" />
<h5>Home</h5>
<input type="text" name="home" value="" size="15" />
<h5>Full Name</h5>
<input type="text" name="fullname" value="" size="100" />
<br><br>
<div><input type="submit" value="Submit" /></div>
</form>
</body>
</html>
and in controller the code is:
if (preg_match('#[0-9]#',$fullname))
{
$errors['fullname'] = 'wrong name format!';
$this->load->view('register', $errors);
}
Now the real problem I have is if the many fields are wrong. I want to have $errors array passed to view and accessed there for all the values it contains. so I don't have to specify $fullname or $mobile to get the value. How can this be done? as to show the user everything missing in one go
First of all I advise using codeigniter's built in form validation class
Here is how I usually process my validation in the controller:
if ($this->input->post())
{
// process the POST data and store accordingly
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[6]|xss_clean');
// the rest of your form fields validation can be set here
if ($this->form_validation->run() == FALSE)
{
// validation hasnt run or has errors, here just call your view
$this->load->view('same_view_file_with_the_form', $data);
}
else
{
// process the POST data upon correct validation
}
}
In my view file I call each error like this:
<h5>Username</h5>
<input type="text" name="username" value="" size="50" />
<span class="error-red"><?php echo form_error("username"); ?></span>
<h5>Password</h5>
<input type="text" name="password" value="" size="50" />
<span class="error-red"><?php echo form_error("password"); ?></span>
<h5>Password Confirm</h5>
<input type="text" name="cpassword" value="" size="50" />
<span class="error-red"><?php echo form_error("cpassword"); ?></span>
Do all your checks in the controller prior to binding errors into the view.
e.g.
$errors = array();
if (preg_match('#[0-9]#',$fullname))
{
$errors['fullname'] = 'wrong name format!';
}
if ( do_something_to_validate(mobile) )
{
$errors['mobile'] = 'invalid mobile';
}
// after checking everything do this
$this->load->view('register', $errors);

Editing Magento Checkout Form

On clicking "place order" in Magento, I want to capture some of its values and feed them to the following form:
<form method="post" action="https://www.jambopay.com/JPExpress.aspx" target="_blank">
<input type="hidden" name="jp_item_type" value="cart"/>
<input type="hidden" name="jp_item_name" value="test shop"/>
<input type="hidden" name="order_id" value="455879"/>
<input type="hidden" name="jp_business" value="business#yourdomain.com"/>
<input type="hidden" name="jp_amount_1" value="51"/>
<input type="hidden" name="jp_amount_2" value="0"/>
<input type="hidden" name="jp_amount_5" value="0"/>
<input type="hidden" name="jp_payee" value="email#yourcustomer.com"/>
<input type="hidden" name="jp_shipping" value="company name"/>
<input type="hidden" name="jp_rurl" value="http://www.yourwebsite.com/testpost/Result.aspx?ii=0"/>
<input type="hidden" name="jp_furl" value="http://www.yourwebsite.com/testpost/Result.aspx?ii=1"/>
<input type="hidden" name="jp_curl" value="http://www.yourwebsite.com/testpost/Result.aspx?ii=2"/>
<input type="image" src="https://www.jambopay.com/jambohelp/jambo/rsc/paymentsbyJamboPay.jpg"/>
</form>
The values include:
Order Number,
Total Amount,
Customers Email Address,
How do i do this?
You can get value of order after save order is called you can get last order id from this methods
$orderId = Mage::getSingleton('checkout/session')->getLastOrderId();
once you have order id then load order like this
$order = Mage::getModel('sales/order')->load($orderId);
and you can get all details of order

how to finish razor section?

I have this in my view :
<input id="#Html.TextBoxFor(m => m.UserName)" type="text" placeholder="Username" autofocus required>
<input id="(#Html.PasswordFor(m => m.Password))" type="password" placeholder="Password" required>
Its working but my result is
What am I doing wrong?
Either you want to put HTML as tags:
<input id="UserName" type="text" placeholder="Username" value="#Model.UserName" autofocus required />
<input id="Password" type="password" placeholder="Password" value="#Model.Password" required />
Or let Razor to do it for you:
#Html.TextBoxFor(m => m.UserName);
#Html.PasswordFor(m => m.Password);
You are mixing it together. Your example starts with pure HTML, then it finds the Razor command which renders another input tags, inside the pure HTML one. Prefer Razor code when you need to return the model type.

Resources