ardent icon indicating copy to clipboard operation
ardent copied to clipboard

[bug] _old_input empty after model touch()

Open kwladyka opened this issue 11 years ago • 3 comments

I discover strange things and lost hours to find that bug. The question is intentional or is it bug? I don't see any reason to do this intentional.

$token->tokenable->touch(); clear _old_input :/

Token is token in relationship with User model by polymorphic model.

print_r(Session::all());
$token->tokenable->touch();
print_r(Session::all());

First print_r (there is _old_input)

Array
(
    [_token] => t5zd4vVf6i4TlLQmfYdiVMkHxRhzoYS08JYTQf4y
    [flash] => Array
        (
            [new] => Array
                (
                )

            [old] => Array
                (
                    [0] => _old_input
                    [1] => errors
                )

        )

    [_old_input] => Array
        (
            [_token] => t5zd4vVf6i4TlLQmfYdiVMkHxRhzoYS08JYTQf4y
            [name] => Krzysztof
            [surname] => 
            [password] => 
            [password_confirmation] => 
            [birth] => 0000-00-00
            [phone] => 
            [/auth/register2/L7zA1MydJB5FRi6qzSALrJyAmxipWXI0ApqMSpsz8yCXKeln3s] => 
        )

    [errors] => Illuminate\Support\MessageBag Object
        (
            [messages:protected] => Array
                (
                    [surname] => Array
                        (
                            [0] => The surname field is required.
                        )

                    [phone] => Array
                        (
                            [0] => The phone field is required.
                        )

                    [gender] => Array
                        (
                            [0] => The gender field is required.
                        )

                    [birth] => Array
                        (
                            [0] => The birth is not a valid date.
                        )

                    [email] => Array
                        (
                            [0] => The email has already been taken.
                        )

                    [password] => Array
                        (
                            [0] => The password field is required.
                        )

                )

            [format:protected] => :message
        )

)

Second print_r ( _old_input disappear in magical way)

Array
(
    [_token] => t5zd4vVf6i4TlLQmfYdiVMkHxRhzoYS08JYTQf4y
    [flash] => Array
        (
            [new] => Array
                (
                    [0] => _old_input
                )

            [old] => Array
                (
                    [1] => errors
                )

        )

    [_old_input] => Array
        (
            [/auth/register2/L7zA1MydJB5FRi6qzSALrJyAmxipWXI0ApqMSpsz8yCXKeln3s] => 
        )

    [errors] => Illuminate\Support\MessageBag Object
        (
            [messages:protected] => Array
                (
                    [surname] => Array
                        (
                            [0] => The surname field is required.
                        )

                    [phone] => Array
                        (
                            [0] => The phone field is required.
                        )

                    [gender] => Array
                        (
                            [0] => The gender field is required.
                        )

                    [birth] => Array
                        (
                            [0] => The birth is not a valid date.
                        )

                    [email] => Array
                        (
                            [0] => The email has already been taken.
                        )

                    [password] => Array
                        (
                            [0] => The password field is required.
                        )

                )

            [format:protected] => :message
        )

)

Token model

<?php

class Token extends Eloquent {
    protected $table = 'tokens';

    protected $guarded = array('*');

    public function tokenable()
    {
        return $this->morphTo();
    }

    static public function generate($length = 50)
    {
        $instance = new self();
        $instance->token = str_random($length);
        return $instance;
    }
}

User model

<?php

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
use LaravelBook\Ardent\Ardent;

class User extends Ardent implements UserInterface, RemindableInterface {

    protected $table = 'users';
    protected $softDelete = true;
    protected $guarded = array('*');
    protected $hidden = array('password');

    public static $passwordAttributes  = array('password');
    public $autoHashPasswordAttributes = true;

    public $autoPurgeRedundantAttributes = true;

    public static $rules = array(
        'name' => array('required'),
        'surname' => array('required'),
        'phone' => array('required', 'min:11'),
        'gender' => array('required', 'in:m,f'),
        'birth' => array('required', 'date'),
        'email' => array('required', 'email', 'unique:users'),
        'password' => array('required', 'min:8', 'max:32', 'confirmed')
    );

    public function saveWithRules($rules)
    {
        switch ($rules) {
            case 'email':
                return $this->save( array('email' => self::$rules['email']) );
            break;
            case 'password':
                return $this->save( array('password' => self::$rules['password']) );
            break;
            case 'data':
                $tmp_rules = self::$rules;
                unset( $tmp_rules['email'] );
                unset( $tmp_rules['password'] );
                return $this->save( $tmp_rules );
            break;
            case 'without-email':
                $tmp_rules = self::$rules;
                unset( $tmp_rules['email'] );
                return $this->save( $tmp_rules );
            break;
            default:
                return $this->save();
            break;
        }
    }

    public function tokens()
    {
        return $this->morphMany('Token', 'tokenable');
    }

    public function groups()
    {
        return $this->belongsToMany('Group');
    }



/*  public function setPasswordAttribute($value)
    {
        $this->attributes['password'] = Hash::make($value);
    }*/

    public function setPhoneAttribute($value)
    {
        $this->attributes['phone'] = preg_replace('/[^0-9]/', '', $value);
    }




    public function getAuthIdentifier()
    {
        return $this->getKey();
    }

    public function getAuthPassword()
    {
        return $this->password;
    }

    public function getReminderEmail()
    {
        return $this->email;
    }

}

I posted that in forum but i guess github is better place: http://forums.laravel.io/viewtopic.php?id=15981

kwladyka avatar Dec 26 '13 23:12 kwladyka

don't know what your touch() function does but maybe this belongs to https://github.com/laravelbook/ardent/issues/150

oliverkaiser avatar Jan 01 '14 21:01 oliverkaiser

touch() is Laravel function.

Anyway touch() shouldn't fire validate function in Ardent - it makes no sense.

kwladyka avatar Jan 02 '14 07:01 kwladyka

Eloquent->touch() fires save() internally:

public function touch()
    {
        $this->updateTimestamps();

        return $this->save();
    }

Conclusion: I can't use the native touch() method with Ardent when the model has a unique validation rule...

Edit: I overwrote the touch() function in Ardent and replaced

return $this->save();

with

return $this->updateUniques();

sisou avatar Feb 27 '14 15:02 sisou

not active

kwladyka avatar Nov 01 '22 21:11 kwladyka