Welcome Guest, Not a member yet? Register   Sign In
Protype ajax validation problem
#1

[eluser]Michael;[/eluser]
I've been working on this for a day and a half now and I'm about to pull my hair out. I know it has to be something simple, or I would have been able to work it out by now.

Basically, I'm using ajax to validate form fields inline and am hiding the submit button until all fields that are required are filled in (and validated when required), the problem is that $this->input->post('field') will absolutely *NOT* validate if empty. In fact, rather the opposite, like so:

Code:
function required() {
    if(!preg_match('/^.$/', $this->obj->input->post('value'))) {
      print "This field is required.";
    }
  }

The above will return a "valid" response if you tab through the field and leave nothing in it, if you put '123' into the field it returns invalid. If you remove the '!' ( as in preg_match ) it weill still return a valid response if the field is empty.

I have tried everything from "empty()" and "strlen()" to the above regex and "== ''". NOTHING is working.

Now, here's the really odd thing... if I use the ajax outside of CI it works perfectly.

Code:
// validation javascript
function ValidateField(input, container, validation) {
  new Ajax.Request('validator.php', {
    parameters: {validation: validation, value: $F(input)},
      onComplete: function(AjaxResult){
        if(AjaxResult.responseText){
          $(input).setStyle({border:'1px #C00 solid'});
          $(container).update(' '+AjaxResult.responseText);
        }else{
          $(input).setStyle({border:'1px solid'});
          $(container).update('Valid');
        }
      }
    });
  }

and the html markup:

Code:
First Name: <input type="text" name="first_name" maxlength="25"
value="<?=$this->validation->first_name;?>" size="25"
onblur="ValidateField(this, 'first_name', 'required')"/>
<span id="first_name" style="color:#C00">* Required</span>

Everything else validates just fine; from email addresses and phone numbers to db checks for present data ... it's as if $this->input->post() has a "positive value" no matter what is ( or is not ) actually *IN* the field.

I am completely stuck here and this is kind of a deal breaker for my project ... and if I have to start over outside of CI I'm going to tear my hair out.
#2

[eluser]mdowns[/eluser]
Have you tried this pattern?
Code:
function required() {
    if(!preg_match('^\.+$', $this->obj->input->post('value'))) {
      print "This field is required.";
    }
  }
#3

[eluser]Michael;[/eluser]
[quote author="mdowns" date="1218483936"]Have you tried this pattern?
Code:
function required() {
    if(!preg_match('^\.+$', $this->obj->input->post('value'))) {
      print "This field is required.";
    }
  }
[/quote]

Yes, I have. I even tried '^\/S+$' trying to match any white space. I'm going to post this under bugs at this point in time because there is no reason that myself or severals other can come up with that this should not be working.
#4

[eluser]mdowns[/eluser]
Have you checked what the actual value of $this->obj->input->post('value') is?

Also, have you tried using trim instead of preg_match (seems a bit overkill for just required field validation)?

Code:
function required(){
    $myinput = trim($this->obj->input->post('value'));
    if(empty($myinput) || $myinput == "")
    {
        print "This field is required.";
    }
}
#5

[eluser]Michael;[/eluser]
[quote author="mdowns" date="1218485956"]Have you checked what the actual value of $this->obj->input->post('value') is?

Also, have you tried using trim instead of preg_match (seems a bit overkill for just required field validation)?

Code:
function required(){
    $myinput = trim($this->obj->input->post('value'));
    if(empty($myinput) || $myinput == "")
    {
        print "This field is required.";
    }
}
[/quote]

I tried things like empty() and strlen() before moving on to preg_match... Smile

I just tried this function and it still returns "valid" if the field is empty. *IF* the field has something in it ( like "abc" ) then $this->obj->input->post('value') will return "abc" if you print it, like so:

Code:
function required(){
    $myinput = trim($this->obj->input->post('value'));
    print $this->obj->input->post('value');
    if(empty($myinput) || $myinput == "")
    {
        print "This field is required.";
    }
  }
#6

[eluser]mdowns[/eluser]
Sorry...I didn't catch what was returned by $this->obj->input->post('value') if the field has nothing in it. Looking at the user guide, it should return FALSE. Is that the case?
#7

[eluser]Michael;[/eluser]
[quote author="mdowns" date="1218487946"]Sorry...I didn't catch what was returned by $this->obj->input->post('value') if the field has nothing in it. Looking at the user guide, it should return FALSE. Is that the case?[/quote]

Currently it returns nothing ... as in ''.

I did leave something out before, when using empty() I get the following error:

Fatal error: Can't use method return value in write context

Code:
function required() {
    if(empty($this->obj->input->post('value'))) {
      print "This field is required.";
    }
  }
#8

[eluser]mdowns[/eluser]
I don't think empty can check the return value of functions. You should use a variable instead.

Code:
$text = trim($this->input->post('value'));
if(empty($text))
{
    echo 'This field is required.';
}

Also how is this function being used? It's not returning anything.
#9

[eluser]Michael;[/eluser]
Looked up this error:

Fatal error: Can’t use method return value in write context

So, since empty apparently cannot check the value of a function, I change the code to this:

Code:
function required() {
    if(empty($_POST['value'])) {
      print "This field is required.";
    }
  }

and this:

Code:
function required() {
    $value = $this->obj->input->post('value');
    if(empty($value)) {
      print "This field is required.";
    }
  }

But neither fixed the issue ... still validates on an empty field.
#10

[eluser]Michael;[/eluser]
[quote author="mdowns" date="1218489673"]I don't think empty can check the return value of functions. You should use a variable instead.

Code:
$text = trim($this->input->post('value'));
if(empty($text))
{
    echo 'This field is required.';
}

Also how is this function being used? It's not returning anything.[/quote]

It's being used as an ajax call... " onblur="ValidateField(this, 'first_name', 'required')" ", so onblur it calls the javascript function ValidateField, which then makes a call to my ajax controller, which then accesses the validation library which is where this function is (" required "). Then the "print" is captured as AjaxResult.responsetext.

Evrything else works, I can validate numbers, email, even run things through the db ... ONLY empty fields is giving me issues.




Theme © iAndrew 2016 - Forum software by © MyBB