Welcome Guest, Not a member yet? Register   Sign In
[Closed] Codeigniter Validation Question
#1

[eluser]riwakawd[/eluser]
I have the general codeigniter validation setup, but I am trying to add bootstrap input has error and has success.

I have got the bootstrap has-error working. But trying to get it so i input is filed in correct it will show has-succes so input will turn green

Code:
<div class="form-group &lt;?php if(validation_errors()) echo 'has-error'; {else} echo 'has-success'; ?&gt; ">
<label for="input-username">&lt;?php echo $entry_username;?&gt;</label>
&lt;input type="text" name="username" value="&lt;?php echo set_value('username');?&gt;" placeholder="&lt;?php echo $entry_username;?&gt;" id="input-username" class="form-control" /&gt;
</div>


has-success not working.
#2

[eluser]Tim Brownlaw[/eluser]
disclaimer: I'm only answering the question on the code provided.

Ok so you are testing that validation_errors() returns a boolean result...

So you need to understand what the function you are testing is going to return...
In this case It's either a string or empty... - I've NOT looked in the code to make sure... which is something you should do to make 100% sure it's the case!

You also need to check your PHP Syntax...

** I've not tested this code - it's a "it might work" suggestion to get you started...

Code:
<div class="form-group
&lt;?php
if(validation_errors() != '')
   echo 'has-error';
else
  echo 'has-success';
?&gt;
">
<label for="input-username">&lt;?php echo $entry_username;?&gt;</label>
&lt;input type="text" name="username" value="&lt;?php echo set_value('username');?&gt;" placeholder="&lt;?php echo $entry_username;?&gt;" id="input-username" class="form-control" /&gt;
</div>

What you have there is an interesting way to do things...
I've only broken the php code out to elaborate on the actual code used.

The other way to try is...

Code:
&lt;?php echo (validation_errors() != '')? 'has-error':'has-success'; ?&gt;


Cheers
Tim
#3

[eluser]Tpojka[/eluser]
Syntax was not valid regarding your request:
Code:
&lt;?php if(validation_errors()) echo 'has-error'; {else} echo 'has-success'; ?&gt;
You just needed to move curlie braces of else word and it would work.
Tim already gave you the answer.
#4

[eluser]riwakawd[/eluser]
[quote author="Tim Brownlaw" date="1399535438"]disclaimer: I'm only answering the question on the code provided.

Ok so you are testing that validation_errors() returns a boolean result...

So you need to understand what the function you are testing is going to return...
In this case It's either a string or empty... - I've NOT looked in the code to make sure... which is something you should do to make 100% sure it's the case!

You also need to check your PHP Syntax...

** I've not tested this code - it's a "it might work" suggestion to get you started...

Code:
<div class="form-group
&lt;?php
if(validation_errors() != '')
   echo 'has-error';
else
  echo 'has-success';
?&gt;
">
<label for="input-username">&lt;?php echo $entry_username;?&gt;</label>
&lt;input type="text" name="username" value="&lt;?php echo set_value('username');?&gt;" placeholder="&lt;?php echo $entry_username;?&gt;" id="input-username" class="form-control" /&gt;
</div>

What you have there is an interesting way to do things...
I've only broken the php code out to elaborate on the actual code used.

The other way to try is...

Code:
&lt;?php echo (validation_errors() != '')? 'has-error':'has-success'; ?&gt;


Cheers
Tim[/quote]

This works but is there also just away to make has-success show up only when start to type? I am trying to find a suitable javascript to match and work with codeigniter. Can Not seem to get javascript to work with codeigniter

Code:
&lt;?php echo (validation_errors() != '')? 'has-error':'has-success'; ?&gt;

Code:
[removed]
$(document).ready(function () {

    $('#register-form').validate({
        rules: {
            firstname: {
                minlength: 2,
                required: true
            },
            lastname: {
                minlength: 2,
                required: true
            },
            password: {
                minlength: 2,
                required: true
            },
            email: {
                required: true,
                email: true
            },
            username: {
                minlength: 2,
                required: true
            }
        },
        highlight: function (element) {
            $(element).closest('.form-group').removeClass('has-success').addClass('has-error');
        },
        success: function (element) {
            element.text('OK!').addClass('valid')
                .closest('.form-group').removeClass('has-error').addClass('has-success');
        }
    });

});
[removed]
#5

[eluser]riwakawd[/eluser]
I have it working with js now I added http://ajax.aspnetcdn.com/ajax/jquery.va...alidate.js
#6

[eluser]ivantcholakov[/eluser]
jquery.validate plugin is nice, I used it as an addition to the server-side validation. But lately I conserve my time and I only implement the PHP-side validation, because it is the secure one.

In CodeIgniter I use only the "has-error" class which is triggered when validation fails on a specific field.

Code:
<div class="form-group&lt;?php if (!empty($validation_errors['username'])) { ?&gt; has-error&lt;?php } ?&gt;">
                    <label for="username">* <i18n>ui_username</i18n></label>
                    <div class="input-group">
                        <span class="input-group-addon"><i class="fa fa-user fa-fw"></i></span>
                        &lt;input type="text" name="username" id="username" class="form-control" value="&lt;?php echo set_value('username'); ?&gt;" /&gt;
                    </div>
                </div>

$validation_errors is an array here. In CodeIgniter 3 it can be taken this way: $validation_errors = $this->form_validation->error_array();
#7

[eluser]riwakawd[/eluser]
[quote author="ivantcholakov" date="1399540972"]jquery.validate plugin is nice, I used it as an addition to the server-side validation. But lately I conserve my time and I only implement the PHP-side validation, because it is the secure one.

In CodeIgniter I use only the "has-error" class which is triggered when validation fails on a specific field.

Code:
<div class="form-group&lt;?php if (!empty($validation_errors['username'])) { ?&gt; has-error&lt;?php } ?&gt;">
                    <label for="username">* <i18n>ui_username</i18n></label>
                    <div class="input-group">
                        <span class="input-group-addon"><i class="fa fa-user fa-fw"></i></span>
                        &lt;input type="text" name="username" id="username" class="form-control" value="&lt;?php echo set_value('username'); ?&gt;" /&gt;
                    </div>
                </div>

$validation_errors is an array here. In CodeIgniter 3 it can be taken this way: $validation_errors = $this->form_validation->error_array();[/quote]

The reason why I used my javascript I found today is because it has both classes error and success. I had to add change it to work with bootstrap 3 and codeigniter. The way that @Tim Brownlaw was OK but has-success was staying there. The javascript only add class when OK.
#8

[eluser]ivantcholakov[/eluser]
@riwakawd

It is very easy the JavaScript validation to be bypassed (Firefox + Tamper plugin for example). Consider JavaScript validation as visual sugar, user experience improvement only. Validation on server-side always should be implemented.
#9

[eluser]riwakawd[/eluser]
[quote author="ivantcholakov" date="1399577213"]@riwakawd

It is very easy the JavaScript validation to be bypassed (Firefox + Tamper plugin for example). Consider JavaScript validation as visual sugar, user experience improvement only. Validation on server-side always should be implemented.[/quote]

I want to be able to do it via server side can do it with input highlighted 'has-error' but the only issue I am having, is if input is filled in correct should be highlighted 'has-success'.

Currently when use this &lt;?php echo (validation_errors() != '')? 'has-error':'has-success'; ?&gt; makes the input always highlighted 'has-success' when no error.

if there was away to addClass and removeClass using that would be good. Working like the javascript but using codeigniter validation.
#10

[eluser]ivantcholakov[/eluser]
I imagine that 'has-success' may be triggered on two conditions, when:

1. on the specific field there is no error;
2. the request method is 'POST', i.e. $this->input->method() == 'post' .

When the validation fails, the fields will be repopulated, and the wrong field will get the attribute 'has-error' (red), the correct fields will get attribute 'has-success' (green).




Theme © iAndrew 2016 - Forum software by © MyBB