We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Validation message from an array

Hi,

I work with forms and with validators.

For multilinguality I set options(an array with all word in the right language) to the forms initialize function. For Text-fields 'placeholder' are able to handle a field of the options-array, but the Validator 'message' isn't able.....

    public function fooAction()
    {
        $t = $this->_getTranslation();
        $form = new FooForm(new Users(), $t["FooForm"]);
        $this->view->form = $form;
    }

    class FooForm extends Form
     {
        public function initialize($objekt, $opt)
        {
            // ID
            $id = new Text('id', array(
                'placeholder' => $opt['ID']     //here the string is displayed
            ));

            $id->addValidators(array(
                 new PresenceOf(array(
                'message' => $opt['id-pres']        //here the default message is showed not my $opt[]
                ))
            ));
        }
    }

Could anybody help me please!

Thanks buchi



26.3k

I am not sure about what is wrong but try this. Go to your Users model and add this code to your initialize() method:


public function initialize(){

  $this->setup(
    array('notNullValidations'=>false)
  );

}

Is messages being shown changed? Are they now these from your $opt?

No nothing changes....



26.3k

I think that your initialize() method in your FooForm form class is lack of add(), try this:


class FooForm extends Form
 {
    public function initialize($objekt, $opt)
    {
        // ID
        $id = new Text('id', array(
            'placeholder' => $opt['ID']      //here the string is displayed
        ));

        $id->addValidators(array(
             new PresenceOf(array(
            'message' => $opt['id-pres']     //here the default message is showed not my $opt[]
            ))
        ));

        // I think you need to add the field to the form
        $this->add($id); //add this line

    }
}

Have it helped?

The add is in my code, otherwise i couldn't get the standard error message....I've also tried to set a variable $t and give it to the Message:

$t ="ID is required";

$id->addValidators(array(
         new PresenceOf(array(
        'message' => $t
        ))
    ));

Then my message will be shown, only the array isn't working as Message in the validator, as 'placeholder ' it works....



26.3k

So the value you provide as a message must be something else that you think it is. I always var_dump to check if it is what I really want.


class FooForm extends Form      {

        public function initialize($objekt, $opt)    {

            $id->addValidators(array(
                 new PresenceOf(array(
                'message' => $opt['id-pres']     //here the default message is showed not my $opt[]
                ))
            ));

            //var dump here and check what you pass, it should be string, mayby you have made some logic mistake
            var_dump($opt['id-pres']);

        }
    }


26.3k
edited Aug '14

I think that error is inside your _getTranslation method. It returns diffirent array that you expect.



26.3k
edited Aug '14

did it help?

Thanks for your tries but I think you don't read my problem really, it's a string and it works with the placeholder for the id but not for the messsage of the validator, all of your codes doesn't get inside my problem!

No nothing helped!



26.3k

:)

Your problem is that you only think that you pass a string but you don't do it. You pass something else than a string. That is your problem.

it's a string and it works with the placeholder

How to you know that $opt[id-pres] is a string? You have not used it with a placeholder.

You know that $opt[ID] and $t are strings, and so they work. But I suggest you to check for sure that $opt[id-pres] is a string as well. Because I am sure it isn't.

If var_dump($opt[id-pres]); dumps as exact string value then it must work. Or your magical server got angry on you...

var_dump return me string and the text, $opt['id-pres'] works with the placeholder I've told you, this was my first test, to set the placholder of the Textfield with $opt['id-pres']......so my magical server got angry with me or phalcon has got a bug.....



26.3k
edited Aug '14

I didn't want to offend you. Sorry for that. Wanted to help. Your problem is very unusual even if it would be Phalcon's bug.

Firstly, Another possibility come to my mind. Which PresenceOf validator do you use? Because there are two:

  • Phalcon\Mvc\Model\Validator\PresenceOf
  • Phalcon\Validation\Validator\PresenceOf - you should use this one

Secondly, is it possible you paste here the code of your _getTranslation() method?

Thirdly, I know it is not a solution but did you considered using gettext as a translation service? I don't know what are your needs exactly but I spent some time on choosing best solution for translations and decided to use gettext - but my translation needs are quite complex.

thanks for all your tries to help!!!

First: Yes I use "Phalcon\Validation\Validator\PresenceOf"

Second: Voila:

protected function _getTranslation()
{
    //Ask browser what is the best language
    $language = $this->request->getBestLanguage();

    //Check if we have a translation file for that lang
    if (file_exists("../messages/".$language.".php")) 
    {
        require "../app/messages/".$language.".php";
    }
    else
    {
       // fallback to some default
       require "../app/messages/de.php";
    }

    //Return a translation object
    return new \Phalcon\Translate\Adapter\NativeArray(array(
       "content" => $messages
    ));

}

Third: I want to use the phalcon-one put thats a possibility thanks!

greez buchi