Auto Validator

  • Thread starter Thread starter ragtek
  • Start date Start date
R

ragtek

Guest
How is the js AUto Validator working?

That's my implemention:
PHP:
public function actionValidateField()
	{
		$this->_assertPostOnly();
                $field = $this->_getFieldValidationInputParams();

                $data = array(
                    'name'  =>  'invited_mail',
                    'value' =>  $field['value']
                );

                return $this->_validateField('Ragtek_Invite_DataWriter_Invite',
                                                $data
                                            );
	}
Code:
<form action="{xen:link 'invites/invite'}" method="post" class="xenForm AutoValidator"
	data-fieldValidatorUrl="{xen:link invites/validate-field}"
	data-normalSubmit="1">
	<xen:if is="{$errors}">
		<div class="errorPanel">
			<h3 class="errorHeading">{xen:phrase please_correct_following_errors}:</h3>
			<div class="baseHtml errors">
				<ol>
				<xen:foreach loop="$errors" value="$error">
					<li>{xen:raw $error}</li>
				</xen:foreach>
				</ol>
			</div>
		</div>
	</xen:if>

	<dl class="ctrlUnit">
		<dt><label for="ctrl_name">Name:</label></dt>
		<dd>
			<input type="text" name="username" class="textCtrl OptOut" id="ctrl_name" autofocus="true" />
			<p class="explain">Name for the e-mail</p>
		</dd>
	</dl>
	<dl class="ctrlUnit">
		<dt><label for="ctrl_mail">{xen:phrase mail}:</label></dt>
		<dd>
			<input type="text" name="mail" class="textCtrl" id="ctrl_mail" autofocus="true" autocomplete="off" />
			<p class="explain">Name for the e-mail</p>
		</dd>
	</dl>
FireBug tells me, that i'm getting this error(with an false mailadress):
Code:
{"error":{"email":"Please enter a valid email."},"templateHtml":"\n\n\n<div class=\"errorOverlay\">\n\t<a class=\"close OverlayCloser\"><\/a>\n\t\n\t\t<h2 class=\"heading\">The following error occurred:<\/h2>\n\t\t\n\t\t<div class=\"baseHtml\">\n\t\t\n\t\t\t<label for=\"ctrl_email\" class=\"OverlayCloser\">Please enter a valid email.<\/label>\n\t\t\n\t\t<\/div>\n\t\n<\/div>"}


If the mailadress is valid, i'm getting:
Code:
{"_redirectStatus":"ok","_redirectTarget":"\/xf\/upload\/","_redirectMessage":"The value 'mike@ragtek.org' was successfully validated against field 'invited_mail'."}
BUT i can't see any message in the frontend:(
 
Problem solved:)

The id have to be "email" and not mail.
But i don't know why^^
 
I belive the validation is done against the datawriter (old thread, but I'm looking at this area at the moment)

So if you look XenForo_Controller:: _validateField, it uses "$writer = XenForo_DataWriter::create($dataWriterName);"

For instance, the registraion form uses the "User" datawritter to validate against, so if you look at the User datawriter, it uses the parameter: "email" (as you found ;) )

Code:
protected function _getFields()
    {
        return array(
            'xf_user' => array(
              ....
                'email'
                    => array('type' => self::TYPE_STRING, 'maxLength' => 120, 'verification' => array('$this', '_verifyEmail'), 'requiredError' => 'please_enter_valid_email'),
                ....

You probably know more about this than me now, but thought I would add this just in case it's useful to anyone else

(but you were using a datawriter above, I've still no idea how the message in the front end is used)
 
I belive the validation is done against the datawriter (old thread, but I'm looking at this area at the moment)

So if you look XenForo_Controller:: _validateField, it uses "$writer = XenForo_DataWriter::create($dataWriterName);"

For instance, the registraion form uses the "User" datawritter to validate against, so if you look at the User datawriter, it uses the parameter: "email" (as you found ;) )

Code:
protected function _getFields()
    {
        return array(
            'xf_user' => array(
              ....
                'email'
                    => array('type' => self::TYPE_STRING, 'maxLength' => 120, 'verification' => array('$this', '_verifyEmail'), 'requiredError' => 'please_enter_valid_email'),
                ....

You probably know more about this than me now, but thought I would add this just in case it's useful to anyone else
That's actually really useful :)

I'll be doing a lot of stuff with field validators soon so this has helped me understand it a bit better.
 
I'm still trying to get my head around it, but within that datawriter you'll have:

'verification' => array('$this', '_verifyEmail')

the function _verifyEmail is also found in the XenForo_DataWriter_User and tells you what it verifies against
 
yep, I'm now having the same issue as Ragtek

I'm forcing the email field to have a different name (it has to in my case), on validation, if the email format is incorrect, the datawriter returns the response :

{"error":{"email":"Please enter a valid email."}

This is good so far, but now the message needs to be displayed to the user

... time to look at the JavaScript behind this
 
Top Bottom